-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.py
73 lines (53 loc) · 1.53 KB
/
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
from lib import *
input = read_input(2021, 11)
lines = input.splitlines()
grid = [[*map(int, line)] for line in lines]
out = 0
for _ in range(100):
flashes = []
visited = set()
for i, row in enumerate(grid):
for j, x in enumerate(row):
if x >= 9:
flashes.append((i, j))
else:
row[j] += 1
while flashes:
i, j = flashes.pop(0)
if (i, j) in visited:
continue
visited.add((i, j))
grid[i][j] = 0
out += 1
for q, p in get_neighbors(j, i, len(lines[0]), len(lines), diag=True):
x = grid[p][q]
if x >= 9:
flashes.append((p, q))
elif (p, q) not in visited:
grid[p][q] += 1
print(out)
grid = [[*map(int, line)] for line in lines]
for step in irange():
flashes = []
visited = set()
for i, row in enumerate(grid):
for j, x in enumerate(row):
if x >= 9:
flashes.append((i, j))
else:
row[j] += 1
while flashes:
i, j = flashes.pop(0)
if (i, j) in visited:
continue
visited.add((i, j))
grid[i][j] = 0
for q, p in get_neighbors(j, i, len(lines[0]), len(lines), diag=True):
x = grid[p][q]
if x >= 9:
flashes.append((p, q))
elif (p, q) not in visited:
grid[p][q] += 1
if len(visited) == len(lines) * len(lines[0]):
print(step + 1)
break