diff --git a/src/main/kotlin/com/geistindersh/aoc/year2024/Day11.kt b/src/main/kotlin/com/geistindersh/aoc/year2024/Day11.kt index 19eda2b..011f2fc 100644 --- a/src/main/kotlin/com/geistindersh/aoc/year2024/Day11.kt +++ b/src/main/kotlin/com/geistindersh/aoc/year2024/Day11.kt @@ -7,27 +7,34 @@ import com.geistindersh.aoc.helper.report class Day11( dataFile: DataFile, ) { - private val stones = fileToString(2024, 11, dataFile).split(" ") + private val stones = fileToString(2024, 11, dataFile).split(" ").associateWith { 1L } + private var memory = mutableMapOf("0" to listOf("1")) - private fun List.update() = - this.flatMap { - when { - it == "0" -> listOf("1") - it.length % 2 == 0 -> { - val mid = it.length / 2 - listOf(it.substring(0, mid).toLong().toString(), it.substring(mid).toLong().toString()) + private fun Map.update() = + this + .flatMap { (k, v) -> + when { + k in memory -> {} + k.length % 2 == 0 -> { + val mid = k.length / 2 + memory[k] = listOf(k.substring(0, mid).toLong().toString(), k.substring(mid).toLong().toString()) + } + else -> memory[k] = listOf((k.toLong() * 2024).toString()) } - else -> listOf((it.toLong() * 2024).toString()) - } - } + memory[k]!!.map { it to v } + }.groupingBy { it.first } + .aggregate { _, accumulator: Long?, element, _ -> (accumulator ?: 0) + element.second } - fun part1() = - generateSequence(stones) { it.update() } - .drop(25) + private fun Map.blink(times: Int) = + generateSequence(this) { it.update() } + .drop(times) .first() - .count() + .values + .sum() - fun part2() = 0 + fun part1() = stones.blink(25) + + fun part2() = stones.blink(75) } fun day11() { diff --git a/src/test/kotlin/com/geistindersh/aoc/year2024/Day11Test.kt b/src/test/kotlin/com/geistindersh/aoc/year2024/Day11Test.kt index 69996d2..b3c1531 100644 --- a/src/test/kotlin/com/geistindersh/aoc/year2024/Day11Test.kt +++ b/src/test/kotlin/com/geistindersh/aoc/year2024/Day11Test.kt @@ -8,12 +8,10 @@ class Day11Test { @Test fun part1() { assertEquals(55312, Day11(DataFile.Example).part1()) - assertEquals(-1, Day11(DataFile.Part1).part1()) } @Test fun part2() { - assertEquals(-1, Day11(DataFile.Example).part2()) - assertEquals(-1, Day11(DataFile.Part1).part2()) + assertEquals(65601038650482, Day11(DataFile.Example).part2()) } }