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

25 lines
623 B
Python

#!/usr/bin/env python3
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")])
flashes = 0
for step in range(100):
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
flashes += np.sum(mask)
break
print(flashes)