Skip to content

Commit 326a855

Browse files
committed
Solve part 1 day 10
1 parent 18b118a commit 326a855

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/Day10.kt

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,27 @@ private const val DAY = 10
22

33
fun main() {
44
fun part1(input: List<String>): Int {
5-
return -1
5+
var syntaxErrorScore = 0
6+
input.mapIndexed { lineNumber, line ->
7+
val expectedCloseSigns = mutableListOf<Char>()
8+
line.forEach { actualChar ->
9+
when(actualChar) {
10+
'[' -> expectedCloseSigns.add(']')
11+
'(' -> expectedCloseSigns.add(')')
12+
'{' -> expectedCloseSigns.add('}')
13+
'<' -> expectedCloseSigns.add('>')
14+
']', ')', '}', '>' -> {
15+
val expected = expectedCloseSigns.removeLast()
16+
if (expected != actualChar) {
17+
syntaxErrorScore += getSyntaxErrorPoints(actualChar)
18+
return@forEach
19+
}
20+
}
21+
}
22+
}
23+
}
24+
25+
return syntaxErrorScore
626
}
727

828
fun part2(input: List<String>): Int {
@@ -11,10 +31,18 @@ fun main() {
1131

1232
// test if implementation meets criteria from the description, like:
1333
val testInput = readInput(day = DAY, useTestInput = true)
14-
check(part1(testInput) == 15)
15-
check(part2(testInput) == 1134)
34+
check(part1(testInput) == 26397)
35+
//check(part2(testInput) == 1134)
1636

1737
val input = readInput(day = DAY)
1838
println(part1(input))
1939
println(part2(input))
40+
}
41+
42+
private fun getSyntaxErrorPoints(char: Char) = when (char) {
43+
')' -> 3
44+
']' -> 57
45+
'}' -> 1197
46+
'>' -> 25137
47+
else -> throw Error("unexpected char")
2048
}

0 commit comments

Comments
 (0)