-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.py
68 lines (46 loc) · 1.5 KB
/
day11.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
#!/usr/bin/env python3
from copy import deepcopy
adj_coords = [(-1, 0), (-1, -1), (0, -1), (1, -1),
(1, 0), (1, 1), (0, 1), (-1, 1)]
def step(grid):
flashed = set()
to_flash = []
for i, row in enumerate(grid):
for j, _ in enumerate(row):
grid[i][j] += 1
if grid[i][j] > 9:
to_flash.append((i, j))
while len(to_flash) > 0:
octo = to_flash.pop()
for adj in adj_coords:
ii, ji = octo[0] + adj[0], octo[1] + adj[1]
if ii < 0 or ii > (len(grid)-1) or ji < 0 or ji > (len(grid[0]) - 1):
continue
if (ii, ji) not in to_flash and (ii, ji) not in flashed:
grid[ii][ji] += 1
if grid[ii][ji] > 9:
to_flash.append((ii, ji))
grid[octo[0]][octo[1]] = 0
flashed.add((octo[0], octo[1]))
return grid, len(flashed)
def part1(grid):
total_flashes = 0
for _ in range(100):
grid, flashes = step(grid)
total_flashes += flashes
return total_flashes
def part2(grid):
step_count = 0
flashes = 0
while flashes != len(grid) * len(grid[0]):
step_count += 1
grid, flashes = step(grid)
return step_count
def main():
with open("./input") as f:
grid = [[int(level) for level in line.strip()]
for line in f.readlines()]
print(f"part1: {part1(deepcopy(grid))}")
print(f"part2: {part2(grid)}")
if __name__ == "__main__":
main()