summary refs log tree commit diff stats
path: root/day11.py
blob: e939ed09f6d5e4f5a9995ea26323283be94bda35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python

import numpy as np
from scipy.ndimage import convolve

with open("day11.txt") as data:
    octopi = np.array([list(map(int, line.strip())) for line in data])

neighbors = np.ones((3,3), dtype=int)
step = 0
flashes = 0
while True:
    octopi += 1
    flashed = np.zeros_like(octopi, dtype=bool)
    while (flashing := ((octopi > 9) & ~flashed)).any():
        octopi += convolve(flashing.astype(int), neighbors, mode='constant')
        flashed |= flashing

    octopi[flashed] = 0
    flashes += flashed.sum()
    step += 1
    if step == 100:
        print(flashes)

    if np.all(octopi == 0):
        print(step)
        break