generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay24.kt
39 lines (36 loc) · 1.35 KB
/
Day24.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
fun main() {
fun solve(input: List<String>): Pair<String, String> {
val digits = mutableMapOf<Int, Pair<Int, Int>>()
val stack = mutableListOf<Pair<Int, Int>>()
var (push, sub, dig) = Triple(false, 0, 0)
input.forEachIndexed { i, line ->
val operand = line.substringAfterLast(" ")
if (i % 18 == 4) push = operand == "1"
if (i % 18 == 5) sub = operand.toInt()
if (i % 18 == 15) {
if (push) {
stack.add(dig to operand.toInt())
} else {
val (sibling, add) = stack.removeLast()
val diff = add + sub
if (diff < 0) {
digits[sibling] = -diff + 1 to 9
digits[dig] = 1 to 9 + diff
} else {
digits[sibling] = 1 to 9 - diff
digits[dig] = 1 + diff to 9
}
}
dig++
}
}
return digits.toSortedMap().values.unzip().let { (a, b) ->
b.joinToString("") to a.joinToString("")
}
}
fun part1(input: List<String>) = solve(input).first
fun part2(input: List<String>) = solve(input).second
val input = readInput("Day24")
println(part1(input))
println(part2(input))
}