1
0
Files
Advent-of-Code/2020/src/main/kotlin/Day13.kt
T
2021-12-01 19:32:38 +01:00

49 lines
1.2 KiB
Kotlin

package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Lines
import kotlin.math.abs
private data class Bus(val index: Int, val id: Long)
@Day(13)
class Day13(@Lines val input: List<String>) {
fun part1(): Int {
val id = input[0].toInt()
val (busId, min) = input[1]
.splitToSequence(",")
.filterNot { it == "x" }
.map { it.toInt() }
.map { bus -> bus to id / bus }
.map { (bus, div) -> bus to bus * (div + 1) }
.minByOrNull { it.second }!!
return busId * abs(min - id)
}
private tailrec fun gcd(a: Long, b: Long): Long = if (b == 0L) a else gcd(b, a % b)
private fun lcm(a: Long, b: Long): Long = a / gcd(a, b) * b
fun part2(): Long {
val buses = input[1]
.splitToSequence(",")
.mapIndexedNotNull { index, bus ->
if (bus == "x") null
else Bus(index, bus.toLong())
}
.toList()
var step = 1L
var t = 0L
for ((i, id) in buses) {
while ((t + i) % id != 0L) t += step
step = lcm(step, id)
}
return t
}
}