-
Notifications
You must be signed in to change notification settings - Fork 1
/
11.py
72 lines (49 loc) · 1.33 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
from collections import deque
import fileinput
G = []
for l in fileinput.input():
G.append([int(d) for d in list(l.strip())])
dr = [-1, -1, -1, 0, 1, 1, 1, 0]
dc = [-1, 0, 1, 1, 1, 0, -1, -1]
R = len(G)
C = len(G[0])
flashes = 0
for gen in range(1, 1000):
flashed = set()
# step 1
for r in range(R):
for c in range(C):
G[r][c] += 1
# step 2, two parts
q = deque()
for r in range(R):
for c in range(C):
if G[r][c] > 9:
flashed.add((r, c))
for i in range(len(dr)):
rr = r + dr[i]
cc = c + dc[i]
if 0 <= rr < R and 0 <= cc < C:
G[rr][cc] += 1
q.append((rr, cc))
while q:
(r, c) = q.popleft()
if (r, c) not in flashed and G[r][c] > 9:
flashed.add((r, c))
for i in range(len(dr)):
rr = r + dr[i]
cc = c + dc[i]
if 0 <= rr < R and 0 <= cc < C:
G[rr][cc] += 1
q.append((rr, cc))
# step 3
for r, c in flashed:
G[r][c] = 0
flashes += len(flashed)
# part1
if gen == 100:
print(flashes)
# part 2
if len(flashed) == R * C:
print(gen)
break