From 0aaf270eac1d5c06bf81a6b66ddbf2df9b97e12e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20R=C3=B8dland?= Date: Thu, 21 Dec 2023 10:20:13 +0100 Subject: [PATCH] 2023 - Day 21 - part 1 --- .../kotlin/no/rodland/advent_2023/Day21.kt | 33 +++++++++++++++---- .../no/rodland/advent_2023/Day21Test.kt | 5 +-- 2 files changed, 29 insertions(+), 9 deletions(-) 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