-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11a.py
57 lines (46 loc) · 1.06 KB
/
11a.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
import numpy as np
def strtointlist(s):
return [ int(c) for c in s ]
data = np.array([ strtointlist(s) for s in open('input.txt').read().splitlines() ])
def find_neighbours(f):
n = []
r,c = f
maxr, maxc = data.shape
maxr -= 1
maxc -= 1
if r > 0:
if c > 0:
n.append([r-1, c-1])
n.append([r-1, c])
if c < maxc:
n.append([r-1, c+1])
if c > 0:
n.append([r, c-1])
if c < maxc:
n.append([r, c+1])
if r < maxr:
if c > 0:
n.append([r+1, c-1])
n.append([r+1, c])
if c < maxc:
n.append([r+1, c+1])
return n
ones = np.ones(data.shape, dtype=np.uint8)
nflash = 0
for step in range(100):
data = data + ones
fw = np.where(data > 9)
flash = list(zip(fw[0], fw[1]))
while(len(flash)):
nflash += 1
f = flash.pop()
neighbours = find_neighbours(f)
for nr,nc in neighbours:
data[nr,nc] = data[nr,nc] + 1
if data[nr,nc] == 10:
flash.append((nr,nc))
fw = np.where(data > 9)
flash = list(zip(fw[0], fw[1]))
for fr, fc in flash:
data[fr, fc] = 0
print(nflash)