Files
python-aoc-2021/day11/part2.py
Sebastian Seedorf 2f6cbeb1db Day 11
2021-12-12 12:31:54 +01:00

25 lines
713 B
Python

#!/usr/bin/env python3
from functools import reduce
import numpy as np
from scipy.signal import convolve2d
arr = np.array([[int(n) for n in line.strip()] for line in open("input.txt")])
for step in range(1000):
arr = arr + 1
next_arr = arr
mask = np.zeros_like(arr)
changes = 0
while True:
mask[next_arr >= 10] = 1
next_arr = arr + convolve2d(mask, np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]]), boundary='fill', mode='same')
if changes == (changes := np.sum(mask)):
arr = next_arr
arr[arr >= 10] = 0
if np.sum(mask) == reduce(lambda a, b: a*b, arr.shape):
print(step + 1)
exit(0)
break