generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.kt
36 lines (30 loc) · 1.15 KB
/
Day10.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
fun main() {
val closing = "()[]{}<>".chunked(2).associate { it[0] to it[1] }
fun String.getIllegalCharAndStack(): Pair<Char?, List<Char>?> {
val stack = mutableListOf<Char>()
for (c in this) {
if (c in "({[<") stack.add(c)
else if (c == closing[stack.last()]) stack.removeLast()
else return c to null
}
return null to stack
}
fun part1(input: List<String>): Int {
val points = mapOf(')' to 3, ']' to 57, '}' to 1197, '>' to 25137)
return input.sumOf { it.getIllegalCharAndStack().first?.let { points[it]!! } ?: 0 }
}
fun part2(input: List<String>): Long {
val points = mapOf(')' to 1, ']' to 2, '}' to 3, '>' to 4)
return input.mapNotNull {
it.getIllegalCharAndStack().second?.foldRight(0L) { c, sum ->
sum * 5 + points[closing[c]!!]!!
}
}.sorted().let { it[it.size / 2] }
}
val testInput = readInput("Day10_test")
checkEquals(part1(testInput), 26397)
checkEquals(part2(testInput), 288957)
val input = readInput("Day10")
println(part1(input))
println(part2(input))
}