-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.kt
59 lines (50 loc) · 1.83 KB
/
Day13.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package aoc2020.day13
import util.lcm
fun readInputToList(input: String): List<Int> {
val list = mutableListOf<Int>()
for (v in input.split(",")) {
if (v == "x") {
list.add(-1)
} else {
list.add(v.toInt())
}
}
return list
}
fun readInputToListExcludeX(input: String): List<Int> {
return input.split(",")
.filter { it != "x" }
.map { it.toInt() }
}
fun findFirstAvailableBus(timestamp: Long, inputList: List<Int>): Int {
val minWaitTime = mutableMapOf<Int, Int>()
for (arrivalInterval in inputList) {
val waitTime = ((timestamp / arrivalInterval) + 1) * arrivalInterval - timestamp
minWaitTime[waitTime.toInt()] = arrivalInterval
}
val min = minWaitTime.keys.minOrNull()
if (min != null) {
return min * minWaitTime[min]!!
}
return -1
}
// This loops through the available buses and their arrival times finding multiples of the interval, but uses the LCM to update the interval,
// based on the multiplier found at every loop (which saves a lot of iterations), until a common multiple is found
fun findEarliestTimestamp(inputList: List<Int>): Long {
val shuttleList = mutableListOf<Shuttle>()
inputList.filter { it != -1 }
.mapTo(shuttleList) { Shuttle(inputList.indexOf(it).toLong(), it.toLong()) }
var index = shuttleList[0].index
var interval = shuttleList[0].departureInterval
for (shuttle in shuttleList.subList(1, shuttleList.size)) {
for (i in index until Long.MAX_VALUE step interval) {
if ((i + shuttle.index) % shuttle.departureInterval == 0L) {
index = i
interval = lcm(interval, shuttle.departureInterval)
break
}
}
}
return index
}
class Shuttle(var index: Long, var departureInterval: Long)