blob: 93dcd908b0949aa9c601c05459b9ffe0d367d860 (
plain)
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
|
import sys
from collections import defaultdict, Counter, deque
data = sys.argv[1] if len(sys.argv) > 1 else "day11data.txt"
G = []
for line in open(data):
G.append([int(x) for x in line.strip()])
R = len(G)
C = len(G[0])
ans = 0
def flash(r, c):
global ans
ans += 1
G[r][c] = -1
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
rr = r + dr
cc = c + dc
if 0 <= rr < R and 0 <= cc < C and G[rr][cc] != -1:
G[rr][cc] += 1
if G[rr][cc] >= 10:
flash(rr, cc)
t = 0
while True:
t += 1
for r in range(R):
for c in range(C):
G[r][c] += 1
for r in range(R):
for c in range(C):
if G[r][c] == 10:
flash(r, c)
done = True
for r in range(R):
for c in range(C):
if G[r][c] == -1:
G[r][c] = 0
else:
done = False
if t == 100:
print(ans)
if done:
print(t)
break
|