Skip to content

Commit deaedcd

Browse files
committed
Solution 2015-01 (Not Quite Lisp)
1 parent 521bc24 commit deaedcd

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package de.ronny_h.aoc.year15.day01
2+
3+
import de.ronny_h.aoc.AdventOfCode
4+
5+
fun main() = NotQuiteLisp().run(232, 1783)
6+
7+
class NotQuiteLisp : AdventOfCode<Int>(2015, 1) {
8+
override fun part1(input: List<String>): Int {
9+
require(input.size == 1)
10+
val instructions = input.first()
11+
val ups = instructions.count { it == '(' }
12+
val downs = instructions.count { it == ')' }
13+
return ups - downs
14+
}
15+
16+
override fun part2(input: List<String>): Int {
17+
var floor = 0
18+
input.first().forEachIndexed { i, char ->
19+
when (char) {
20+
'(' -> floor++
21+
')' -> floor--
22+
else -> error("invalid instruction: $char at position $i")
23+
}
24+
if (floor < 0) {
25+
return i + 1
26+
}
27+
}
28+
return 0
29+
}
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package de.ronny_h.aoc.year15.day01
2+
3+
import io.kotest.core.spec.style.StringSpec
4+
import io.kotest.data.forAll
5+
import io.kotest.data.row
6+
import io.kotest.matchers.shouldBe
7+
8+
class NotQuiteLispTest : StringSpec({
9+
"part 1: find the right floor by counting parenthesis" {
10+
forAll(
11+
row("(())", 0),
12+
row("()()", 0),
13+
row("(((", 3),
14+
row("(()(()(", 3),
15+
row("))(((((", 3),
16+
row(")())())", -3),
17+
) { input, result ->
18+
NotQuiteLisp().part1(listOf(input)) shouldBe result
19+
}
20+
}
21+
22+
"part 2: the fist position leading to a basement floor" {
23+
NotQuiteLisp().part2(listOf(")")) shouldBe 1
24+
NotQuiteLisp().part2(listOf("()())")) shouldBe 5
25+
}
26+
})

0 commit comments

Comments
 (0)