/
SyntaxScoring.kt
59 lines (48 loc) · 1.75 KB
/
SyntaxScoring.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package d10_SyntaxScoring
import util.Input
import util.Output
fun main() {
Output.day(10, "Syntax Scoring")
val startTime = Output.startTime()
val navSubsystem = Input.parseLines(filename = "/input/d10_syntax_chunks.txt")
.map { Input.parseToListOf<Char>(rawData = it) } as MutableList
val errorPoints = mapOf(
')' to 3,
']' to 57,
'}' to 1197,
'>' to 25137
)
val autocompletePoints = mapOf(
')' to 1,
']' to 2,
'}' to 3,
'>' to 4
)
val openers = listOf('(', '[', '{', '<')
val closers = listOf(')', ']', '}', '>')
val errorMap = mutableMapOf<Char, Int>()
val autocompleteScores = mutableListOf<Long>()
navSubsystem.forEachIndexed next@{ i, line ->
val deq = ArrayDeque<Char>()
// go char-by-char, adding to deque and finding corrupt lines
line.forEach { c ->
if (c in openers)
deq.addLast(c)
else if (closers.indexOf(c) == openers.indexOf(deq.last()))
deq.removeLast()
else { // track syntax error
errorMap.merge(c, 1) { a, b -> a + b }
return@next
}
}
// complete lines and track autocomplete char points
val scoresInOrder = mutableListOf<Int>()
deq.reversed().forEach { c ->
scoresInOrder.add(autocompletePoints[closers[openers.indexOf(c)]]!!)
}
autocompleteScores.add(scoresInOrder.fold(0L) { acc, cur -> (acc * 5) + cur })
}
Output.part(1, "Syntax Error Score", errorMap.map { errorPoints[it.key]!! * it.value }.sum())
Output.part(2, "Autocomplete Score", autocompleteScores.sorted()[autocompleteScores.size / 2])
Output.executionTime(startTime)
}