-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathy23day8part2.kt
More file actions
25 lines (22 loc) · 906 Bytes
/
Copy pathy23day8part2.kt
File metadata and controls
25 lines (22 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.math.BigInteger
fun main() {
tailrec fun gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
fun lcm(a: Int, b: Int): Long = a.toLong() / gcd(a, b) * b
fun List<Int>.lcm(): BigInteger = reduce(::gcd).toBigInteger().let { gcd ->
fold(gcd) { acc, it -> it.toBigInteger() / gcd * acc }
}
val steps = readln()
readln()
val network = generateSequence { readlnOrNull() }.map { line ->
val (node, left, right) = """(\w+) = \((\w+), (\w+)\)""".toRegex().matchEntire(line)!!.destructured
node to (left to right)
}.toMap()
network.keys.filter { it.endsWith('A') }.map { node ->
var i = 0
generateSequence(node) {
network.getValue(it).run {
if (steps[i++ % steps.length] == 'L') first else second
}
}.takeWhile { !it.endsWith("Z") }.count()
}.lcm().let(::println)
}