-
Notifications
You must be signed in to change notification settings - Fork 0
/
8a.kt
32 lines (30 loc) · 851 Bytes
/
8a.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
import kotlin.io.path.Path
import kotlin.io.path.readLines
import kotlin.math.*
fun <T> debug(container: Iterable<T>) {
container.forEach { element -> print("$element ") }
println()
}
fun main() {
val fileName = "8.in"
val input = Path(fileName).readLines()
val commands = input[0]
var paths = mutableMapOf<String, Pair<String, String>>()
for (line in input.drop(2)) {
val regex = """(\w+) = \((\w+), (\w+)\)""".toRegex()
val matchResult = regex.find(line)
if (matchResult != null) {
val (node, left, right) = matchResult.destructured
paths.put(node, Pair(left, right))
}
}
var step = 0
var node = "AAA"
while (node != "ZZZ") {
val command = commands[step%commands.length]
if (command == 'L') node = paths[node]!!.first
else node = paths[node]!!.second
step++
}
println(step)
}