From 118b56e5f31463f5a472f198adc01541d408e173 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Sat, 11 Dec 2021 12:25:47 +0100 Subject: [PATCH] Day11 2021 --- 2021/src/main/kotlin/Day11.kt | 63 +++++++++++++++++++++++++++++++ 2021/src/main/resources/day11.txt | 10 +++++ 2021/src/test/kotlin/Day11Test.kt | 22 +++++++++++ 3 files changed, 95 insertions(+) create mode 100644 2021/src/main/kotlin/Day11.kt create mode 100644 2021/src/main/resources/day11.txt create mode 100644 2021/src/test/kotlin/Day11Test.kt diff --git a/2021/src/main/kotlin/Day11.kt b/2021/src/main/kotlin/Day11.kt new file mode 100644 index 0000000..71c6665 --- /dev/null +++ b/2021/src/main/kotlin/Day11.kt @@ -0,0 +1,63 @@ +package be.vandewalleh.aoc.days + +import be.vandewalleh.aoc.utils.BaseDay +import be.vandewalleh.aoc.utils.input.Day + +@Day +class Day11 : BaseDay() { + + private data class Point(val x: Int, val y: Int) { + fun adjacents() = listOf( + copy(y = y - 1), + copy(x = x + 1), + copy(y = y + 1), + copy(x = x - 1), + copy(y = y - 1, x = x - 1), + copy(x = x + 1, y = y + 1), + copy(y = y + 1, x = x - 1), + copy(x = x + 1, y = y - 1), + ) + } + + private val map by lazy { + input.lines.value.flatMapIndexed { y, line -> + line.mapIndexed { x, value -> Point(x, y) to value.digitToInt() } + }.toMap() + } + + override fun part1(): Int { + val map = map.toMap(HashMap()) + var count = 0 + repeat(100) { count += step(map) } + return count + } + + private fun step(map: HashMap): Int { + map.keys.forEach { map[it] = map[it]!! + 1 } + val flashed = hashSetOf() + while (true) { + val new = map.entries.asSequence() + .filter { it.value > 9 } + .filter { it.key !in flashed } + .onEach { flashed += it.key } + .flatMap { it.key.adjacents() } + .filter { it in map } + .toList() + + if (new.isEmpty()) break + new.forEach { map[it] = map[it]!! + 1 } + } + + map.entries.filter { it.value > 9 } + .map { it.key } + .forEach { map[it] = 0 } + + return flashed.size + } + + override fun part2(): Int { + val map = map.toMap(HashMap()) + repeat(10000) { if (step(map) == map.size) return it + 1 } + return -1 + } +} diff --git a/2021/src/main/resources/day11.txt b/2021/src/main/resources/day11.txt new file mode 100644 index 0000000..16ac3e9 --- /dev/null +++ b/2021/src/main/resources/day11.txt @@ -0,0 +1,10 @@ +7222221271 +6463754232 +3373484684 +4674461265 +1187834788 +1175316351 +8211411846 +4657828333 +5286325337 +5771324832 diff --git a/2021/src/test/kotlin/Day11Test.kt b/2021/src/test/kotlin/Day11Test.kt new file mode 100644 index 0000000..9a45214 --- /dev/null +++ b/2021/src/test/kotlin/Day11Test.kt @@ -0,0 +1,22 @@ +package be.vandewalleh.aoc.days + +class Day11Test : BaseDayTest(11) { + override val example = """ + 5483143223 + 2745854711 + 5264556173 + 6141336146 + 6357385478 + 4167524645 + 2176841721 + 6882881134 + 4846848554 + 5283751526 + """.trimIndent() + + override val part1Example = 1656 + override val part1Answer = 1661 + + override val part2Example = 195 + override val part2Answer = 334 +}