-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.py
More file actions
executable file
·48 lines (43 loc) · 1.13 KB
/
11.py
File metadata and controls
executable file
·48 lines (43 loc) · 1.13 KB
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
#!/usr/bin/env python3
import numpy as np
def kernel(like, x, y):
Y,X = like.shape
ret = np.zeros((Y+2, X+2), dtype=int)
kern = np.ones((3,3), dtype=int)
ret[y:y+3, x:x+3]= kern
ret = ret[1:Y+1, 1:X+1]
assert(ret.shape == like.shape)
return ret
def iter(curr, nxt, mem):
Y,X = curr.shape
for y in range(Y):
for x in range(X):
if mem[y, x] == 1:
continue
k = kernel(curr, x, y)
look = (curr > 9)[y, x]
nxt += k * look
if look:
mem[y,x] = 1
return nxt
with open('11.input', 'r') as f:
octs = [[int(a) for a in l.strip()] for l in f.readlines()]
octs = np.array(octs)
result = 0
result_b = None
i = 0
while True:
octs += 1
mem = np.zeros_like(octs)
while np.sum((octs > 9) * (1-mem)) > 0:
nxt = octs
octs = iter(octs, nxt, mem)
octs *= 1 - mem
result += np.sum(mem)
if i == 99:
print(result)
if np.all(octs == 0):
result_b = i
break
i+=1
print(result_b+1)