Files
python-aoc-2020/day17/part2.py
2020-12-17 20:20:55 +01:00

25 lines
755 B
Python

#!/usr/bin/env python3
import numpy as np
from scipy.signal import convolve
DIMS = 4
lines = [[(1 if x == '#' else 0) for x in x.strip()] for x in open("input.txt")]
grid = np.array(lines, dtype=np.byte)
grid = np.expand_dims(grid, axis=tuple(range(DIMS-2)))
kernel = np.ones((3, )*DIMS, dtype=np.byte)
kernel[(1, )*DIMS] = 0
for _ in range(6):
grid = np.pad(grid, pad_width=1)
neighbors = convolve(grid, kernel, mode='same')
set_inactive = np.logical_and(grid == 1, np.floor_divide(neighbors, 2) != 1)
set_active = np.logical_and(grid == 0, neighbors == 3)
grid[set_inactive] = 0
grid[set_active] = 1
a = tuple(slice(np.min(idxs), np.max(idxs) + 1) for idxs in np.where(grid == 0))
grid = grid[a]
print(np.sum(grid))