-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2021_11.py
99 lines (76 loc) · 2.02 KB
/
AoC2021_11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#! /usr/bin/env python3
#
# Advent of Code 2021 Day 11
#
import sys
from aoc.common import InputData
from aoc.common import SolutionBase
from aoc.common import aoc_samples
from aoc.grid import Cell
from aoc.grid import IntGrid
TEST = """\
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526
"""
class Flashes:
value: int
def __init__(self) -> None:
self.value = 0
def increment(self) -> None:
self.value += 1
def get(self) -> int:
return self.value
Input = list[str]
Output1 = int
Output2 = int
class Solution(SolutionBase[Input, Output1, Output2]):
def parse_input(self, input_data: InputData) -> Input:
return list(input_data)
def flash(self, grid: IntGrid, c: Cell, flashes: int) -> int:
grid.set_value(c, 0)
flashes += 1
for n in grid.get_all_neighbours(c):
if grid.get_value(n) == 0:
continue
grid.increment(n)
if grid.get_value(n) > 9:
flashes = self.flash(grid, n, flashes)
return flashes
def _cycle(self, grid: IntGrid) -> int:
for c in grid.get_cells():
grid.increment(c)
flashes = 0
for c in (c for c in grid.get_cells() if grid.get_value(c) > 9):
flashes = self.flash(grid, c, flashes)
return flashes
def part_1(self, input: Input) -> Output1:
grid = IntGrid.from_strings(input)
return sum(self._cycle(grid) for _ in range(100))
def part_2(self, input: Input) -> Output2:
grid = IntGrid.from_strings(input)
cycle = flashes = 0
while flashes != grid.size():
cycle += 1
flashes = self._cycle(grid)
return cycle
@aoc_samples(
(
("part_1", TEST, 1656),
("part_2", TEST, 195),
)
)
def samples(self) -> None:
pass
solution = Solution(2021, 11)
def main() -> None:
solution.run(sys.argv)
if __name__ == "__main__":
main()