-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
24 lines (21 loc) · 661 Bytes
/
Copy pathmain.py
File metadata and controls
24 lines (21 loc) · 661 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np
from scipy.ndimage import generic_filter
with open("input.txt") as f:
lines = [list(x.strip()) for x in f]
def f(x):
if x[len(x) // 2] == 0:
if np.sum(x) == 3: return 1
else: return 0
else:
if np.sum(x) in (3,4): return 1
else: return 0
d = 20
for dims in [3,4]:
a = (np.array(lines) == "#").astype(np.uint8)
for _ in range(dims-2):
a = np.reshape(a, (1,)+a.shape)
arr = np.pad(a, (d-a.shape[0]) // 2)
kernel = np.ones([3]*dims, dtype=np.uint8)
for i in range(6):
arr = generic_filter(arr, f, footprint=kernel, mode="constant", cval=0)
print(np.sum(arr))