From 1d916e940a84acc0ba8bfb4f7c121502923eb21f Mon Sep 17 00:00:00 2001 From: Benjo Date: Sat, 11 Dec 2021 10:13:11 +0100 Subject: [PATCH] day 11 --- 11/11.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 11/input | 10 ++++++++++ 11/testInput | 10 ++++++++++ 3 files changed, 69 insertions(+) create mode 100644 11/11.py create mode 100644 11/input create mode 100644 11/testInput diff --git a/11/11.py b/11/11.py new file mode 100644 index 0000000..9d67c74 --- /dev/null +++ b/11/11.py @@ -0,0 +1,49 @@ +import numpy as np + + +def readGrid(): + with open("input") as file: + lines = file.read().splitlines() + grid = np.array([[val for val in line] for line in lines], dtype=int) + return grid + + +def getAdjacent(grid, x, y): + h, w = grid.shape + adjAdd = [ + (-1, -1), + (-1, 0), + (-1, 1), + (0, 1), + (1, 1), + (1, 0), + (1, -1), + (0, -1) + ] + adj = [(x + addX, y + addY) for addX, addY in adjAdd] + return [(x, y) for x, y in adj if w > x >= 0 and h > y >= 0] + + +def solve(grid): + flashCount = 0 + i = 0 + while True: + i += 1 + grid += 1 + allFlashPositions = set() + while len(grid[grid > 9]) > len(allFlashPositions): + flashPositions = {(x, y) for y, x in np.argwhere(grid > 9)}.difference(allFlashPositions) + for x, y in flashPositions: + for ax, ay in getAdjacent(grid, x, y): + grid[ay][ax] += 1 + allFlashPositions = allFlashPositions.union(flashPositions) + for x, y in allFlashPositions: + grid[y][x] = 0 + flashCount += len(allFlashPositions) if i <= 100 else 0 + if len(allFlashPositions) == 100: + break + + return flashCount, i + + +print(solve(readGrid())) diff --git a/11/input b/11/input new file mode 100644 index 0000000..1a0fab9 --- /dev/null +++ b/11/input @@ -0,0 +1,10 @@ +4134384626 +7114585257 +1582536488 +4865715538 +5733423513 +8532144181 +1288614583 +2248711141 +6415871681 +7881531438 diff --git a/11/testInput b/11/testInput new file mode 100644 index 0000000..a3819c9 --- /dev/null +++ b/11/testInput @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 \ No newline at end of file