advent2020/seventeen_nest.py

15 lines
520 B
Python
Executable File

#!/usr/bin/env python3
import numpy as np
with open(0) as f:
p = [[x == '#' for x in l] for l in f.read().split()]
h, w = len(p), len(p[0])
for dim in 3, 4:
arr = np.asarray(p, dtype=int).reshape((*(1,)*(dim-2), h, w))
for i in range(6):
old, arr = np.pad(arr, 2), np.pad(arr, 1)
for c, orig in np.ndenumerate(arr):
rubix = old[tuple(slice(i, i+3) for i in c)].sum()
arr[c] = rubix == 3 or rubix == 4 and orig
print(arr.sum())