diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-12-11 14:13:15 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-12-11 14:13:15 +0100 |
commit | ce1aa028b265c70235dd1f79df24a1ecc2b75583 (patch) | |
tree | 8f840e17fd1e2f36968376690e8ee1704bbc2b9d /2021 | |
parent | 0d361b53679474ea9d38ea236fc620de5821b001 (diff) |
Add day 11 solutions
Diffstat (limited to '2021')
-rw-r--r-- | 2021/11/.gitignore | 1 | ||||
-rw-r--r-- | 2021/11/Makefile | 8 | ||||
-rw-r--r-- | 2021/11/input | 10 | ||||
-rw-r--r-- | 2021/11/puzzles.py | 66 |
4 files changed, 85 insertions, 0 deletions
diff --git a/2021/11/.gitignore b/2021/11/.gitignore new file mode 100644 index 0000000..60d075d --- /dev/null +++ b/2021/11/.gitignore @@ -0,0 +1 @@ +puzzle-[12] diff --git a/2021/11/Makefile b/2021/11/Makefile new file mode 100644 index 0000000..247194a --- /dev/null +++ b/2021/11/Makefile @@ -0,0 +1,8 @@ +all: + sed '/START PART 2/,/END PART 2/d' puzzles.py >puzzle-1.py + sed '/START PART 1/,/END PART 1/d' puzzles.py >puzzle-2.py + chmod +x puzzle-[12].py + +.PHONY: clean +clean: + rm -f puzzle-[12].py diff --git a/2021/11/input b/2021/11/input new file mode 100644 index 0000000..c360afa --- /dev/null +++ b/2021/11/input @@ -0,0 +1,10 @@ +1224346384 +5621128587 +6388426546 +1556247756 +1451811573 +1832388122 +2748545647 +2582877432 +3185643871 +2224876627 diff --git a/2021/11/puzzles.py b/2021/11/puzzles.py new file mode 100644 index 0000000..ad1fa05 --- /dev/null +++ b/2021/11/puzzles.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 + +# START PART 1 +from itertools import product +# END PART 1 START PART 2 +from itertools import chain, count, product +# END PART 2 + +matrix = list[list[int]] +rows, cols = -1, -1 + + +def flash(grid: matrix, i: int, j: int) -> int: + acc = 1 + grid[i][j] = -1 + + for di, dj in ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)): + ni = i + di + nj = j + dj + if 0 <= ni < rows and 0 <= nj < cols and grid[ni][nj] != -1: + grid[ni][nj] += 1 + if grid[ni][nj] > 9: + acc += flash(grid, ni, nj) + + return acc + + +def main() -> None: + global rows, cols + + with open("input", "r", encoding="utf-8") as f: + grid = list(map(lambda l: [int(n) for n in l.strip()], f.readlines())) + + rows = len(grid) + cols = len(grid[0]) + + acc = 0 + # START PART 1 + for _ in range(100): + # END PART 1 START PART 2 + for step in count(1): + # END PART 2 + for i, j in product(range(rows), range(cols)): + grid[i][j] += 1 + + for i, j in product(range(rows), range(cols)): + if grid[i][j] > 9: + acc += flash(grid, i, j) + + # START PART 2 + if sum(chain.from_iterable(grid)) == -(rows * cols): + print(step) + return + # END PART 2 + + for i, j in product(range(rows), range(cols)): + if grid[i][j] == -1: + grid[i][j] = 0 + + # START PART 1 + print(acc) + # END PART 1 + + +if __name__ == "__main__": + main() |