From 20016a3582f5cde7d59c51183871e2df367deed9 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 11 Dec 2021 12:39:56 +0100 Subject: [PATCH] day(11): add solution Signed-off-by: Matej Focko --- src/year2021/day11/Day11.kt | 88 +++++++++++++++++++++++++++++++++++ src/year2021/day11/sample.txt | 10 ++++ 2 files changed, 98 insertions(+) create mode 100644 src/year2021/day11/Day11.kt create mode 100644 src/year2021/day11/sample.txt diff --git a/src/year2021/day11/Day11.kt b/src/year2021/day11/Day11.kt new file mode 100644 index 0000000..906133f --- /dev/null +++ b/src/year2021/day11/Day11.kt @@ -0,0 +1,88 @@ +package year2021.day11 + +import readInput +import kotlin.collections.ArrayDeque + +fun validAdjacentIndices(input: List>, y0: Int, x0: Int): Iterable> = + (y0 - 1..y0 + 1) + .flatMap { y -> (x0 - 1..x0 + 1).map { x -> Pair(y, x) } } + .filter { (y, x) -> + (y != y0 || x != x0) && (y >= 0 && y < input.size) && (x >= 0 && x < input[y].size) + } + +fun getFlashes(octopuses: MutableList>): List> = + (octopuses.indices) + .flatMap { y -> octopuses[y].indices.map { x -> Pair(y, x) } } + .filter { (y, x) -> octopuses[y][x] > 9 } + .toList() + +fun step(octopuses: MutableList>): Int { + // First, the energy level of each octopus increases by 1. + for (y in octopuses.indices) { + for (x in octopuses.indices) { + octopuses[y][x]++ + } + } + + // Then, any octopus with an energy level greater than 9 flashes. + val queue = ArrayDeque(getFlashes(octopuses)) + while (queue.isNotEmpty()) { + val (y, x) = queue.removeFirst() + + for ((y_1, x_1) in validAdjacentIndices(octopuses, y, x)) { + octopuses[y_1][x_1]++ + + // == 10 means we made it flash + if (octopuses[y_1][x_1] == 10) { + queue.addLast(Pair(y_1, x_1)) + } + } + } + + // Count the flashing octopuses. + val flashes = octopuses.sumOf { row -> row.count { it > 9 } } + + // Reset energy level. + for (y in octopuses.indices) { + for (x in octopuses.indices) { + if (octopuses[y][x] > 9) { + octopuses[y][x] = 0 + } + } + } + + return flashes +} + +fun part1(input: List>): Int { + var flashes = 0 + val octopuses = input.map { row -> row.toMutableList() }.toMutableList() + + repeat(100) { + flashes += step(octopuses) + } + + return flashes +} + +fun part2(input: List>): Int { + var i = 1 + val octopuses = input.map { row -> row.toMutableList() }.toMutableList() + val count = octopuses.size * octopuses[0].size + + while (step(octopuses) != count) { + i++ + } + return i +} + +fun main() { + val sample = readInput(11, "sample").map { row -> row.map { it.digitToInt() } } + val input = readInput(11, "input").map { row -> row.map { it.digitToInt() } } + + check(part1(sample) == 1656) + println(part1(input)) + + check(part2(sample) == 195) + println(part2(input)) +} diff --git a/src/year2021/day11/sample.txt b/src/year2021/day11/sample.txt new file mode 100644 index 0000000..a3819c9 --- /dev/null +++ b/src/year2021/day11/sample.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 \ No newline at end of file