From 65c31164f5c6364b65a486ff1b9edcc98501fcaf Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sat, 11 Dec 2021 06:44:04 +0100 Subject: [PATCH] day11 --- day11.py | 50 +++++++++++++++++++++++++++++++++++++++++ inputs/input11 | 10 +++++++++ inputs/test_input11_1_0 | 10 +++++++++ inputs/test_input11_2_0 | 10 +++++++++ 4 files changed, 80 insertions(+) create mode 100644 day11.py create mode 100644 inputs/input11 create mode 100644 inputs/test_input11_1_0 create mode 100644 inputs/test_input11_2_0 diff --git a/day11.py b/day11.py new file mode 100644 index 0000000..de67962 --- /dev/null +++ b/day11.py @@ -0,0 +1,50 @@ +from tools.aoc import AOCDay +from tools.coordinate import Coordinate +from tools.grid import Grid +from typing import Any, List + + +def getOctopusGrid(lines: List[str]) -> Grid: + grid = Grid(-1) + for y, l in enumerate(lines): + for x, c in enumerate(l): + grid.set(Coordinate(x, y), int(c)) + + return grid + + +def flashGrid(grid: Grid) -> int: + flashers = set() + for c in grid.getActiveCells(): + if grid.add(c) > 9: + flashers.add(c) + + did_flash = set() + while len(flashers) > 0: + thisFlash = flashers.pop() + did_flash.add(thisFlash) + grid.set(thisFlash, 0) + + for n in grid.getNeighboursOf(thisFlash): + if n not in did_flash and grid.add(n) > 9: + flashers.add(n) + + return len(did_flash) + + +class Day(AOCDay): + test_solutions_p1 = [1656] + test_solutions_p2 = [195] + + def part1(self) -> Any: + grid = getOctopusGrid(self.getInput()) + return sum([flashGrid(grid) for _ in range(100)]) + + def part2(self) -> Any: + grid = getOctopusGrid(self.getInput()) + step_count = 0 + while grid.getSum() != 0: + step_count += 1 + flashGrid(grid) + + return step_count diff --git a/inputs/input11 b/inputs/input11 new file mode 100644 index 0000000..03eceda --- /dev/null +++ b/inputs/input11 @@ -0,0 +1,10 @@ +8548335644 +6576521782 +1223677762 +1284713113 +6125654778 +6435726842 +5664175556 +1445736556 +2248473568 +6451473526 diff --git a/inputs/test_input11_1_0 b/inputs/test_input11_1_0 new file mode 100644 index 0000000..03743f6 --- /dev/null +++ b/inputs/test_input11_1_0 @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 diff --git a/inputs/test_input11_2_0 b/inputs/test_input11_2_0 new file mode 100644 index 0000000..03743f6 --- /dev/null +++ b/inputs/test_input11_2_0 @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526