File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
main/kotlin/de/ronny_h/aoc/year15/day01
test/kotlin/de/ronny_h/aoc/year15/day01 Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ })
You can’t perform that action at this time.
0 commit comments