-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_10.kts
33 lines (28 loc) · 973 Bytes
/
day_10.kts
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
import java.io.File
import java.util.*
private val lines = File("/in/input10.txt").readLines()
private val syntax = mapOf('(' to ')', '[' to ']', '{' to '}', '<' to '>')
private val illegalPoints = mapOf(')' to 3, ']' to 57, '}' to 1197, '>' to 25137)
private val legalPoints = mapOf(')' to 1, ']' to 2, '}' to 3, '>' to 4)
fun sol1() = lines.sumOf { l ->
val parsed = LinkedList<Char>()
l.sumOf { c ->
when {
c in syntax.keys -> (0).also { parsed.addLast(c) }
c != syntax[parsed.removeLast()] -> illegalPoints[c]
else -> 0
}
}
}
fun sol2() = lines.mapNotNull {
val parsed = LinkedList<Char>()
for (c in it) when {
c in syntax.keys -> parsed.addLast(syntax[c])
c != parsed.removeLast() -> return@mapNotNull null
}
parsed.foldRight(0L) { c, partial -> (legalPoints[c]!! + partial * 5) }
}
.sorted()
.let { it[it.size / 2] }
sol1()
sol2()