advent2020/seventeen.py

26 lines
692 B
Python
Executable File

#!/usr/bin/env python3
import sys
import numpy as np
def status(rubix):
orig = rubix[tuple(1 for i in rubix.shape)]
return rubix.sum() == 3 or rubix.sum() == 4 and orig
def frob(image):
old, new = np.pad(image, 2), np.pad(image, 1)
for c, _ in np.ndenumerate(new):
new[c] = status(old[tuple(slice(i, i+3) for i in c)])
return new
def cycle(image, cycles=6):
for i in range(cycles):
image = frob(image)
return image
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))
print(cycle(arr).sum())