diff --git a/src/main/kotlin/no/rodland/advent_2023/Day21.kt b/src/main/kotlin/no/rodland/advent_2023/Day21.kt index 66ee0d9..64b9bbc 100644 --- a/src/main/kotlin/no/rodland/advent_2023/Day21.kt +++ b/src/main/kotlin/no/rodland/advent_2023/Day21.kt @@ -1,26 +1,45 @@ package no.rodland.advent_2023 import no.rodland.advent.Day +import no.rodland.advent.Pos + // template generated: 21/12/2023 // Fredrik Rødland 2023 -class Day21(val input: List) : Day> { +class Day21(val input: List) : Day> { private val parsed = input.parse() - override fun partOne(): Long { - return 2 + override fun partOne(): Int { +// val start = parsed.filterValues { it == 'S' }.map { it.key }.single() + var newMap = parsed + val repeat = if (parsed.size == 121) 6 else 64 + repeat(repeat) { newMap = step(newMap) } + return newMap.count { it.value == 'O' } } + fun step(cave: Map): Map { + val previousSteps = cave.filterValues { it == 'O' || it == 'S' }.keys + val newOs = previousSteps + .flatMap { it.neighbourCellsUDLR() } + .filterNot { cave[it] == '#' } + .map { it to 'O' } + val nullOld = previousSteps.map { it to '.' } + return cave + nullOld + newOs + } + + override fun partTwo(): Long { return 2 } - override fun List.parse(): List { - return map { line -> - line - } + override fun List.parse(): Map { + return flatMapIndexed { y, line -> + line.mapIndexed() { x, c -> + Pos(x, y) to c + } + }.toMap() } override val day = "21".toInt() diff --git a/src/test/kotlin/no/rodland/advent_2023/Day21Test.kt b/src/test/kotlin/no/rodland/advent_2023/Day21Test.kt index 0726edd..aba7154 100644 --- a/src/test/kotlin/no/rodland/advent_2023/Day21Test.kt +++ b/src/test/kotlin/no/rodland/advent_2023/Day21Test.kt @@ -15,9 +15,9 @@ internal class Day21Test { private val data21 = "2023/input_21.txt".readFile() private val test21 = "2023/input_21_test.txt".readFile() - private val resultTestOne = 2L + private val resultTestOne = 16 private val resultTestTwo = 2L - private val resultOne = 2L + private val resultOne = 3814 private val resultTwo = 2L val test = defaultTestSuiteParseOnInit( @@ -29,6 +29,7 @@ internal class Day21Test { resultTwo, { Day21(data21) }, { Day21(test21) }, + numTestPart1 = 2 ) @Nested