You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

import numpy as np
from collections import deque
file = "inputs/day11.input"
numbers = [x.decode().replace("\n", "") for x in open(file, "rb").readlines()]
numbers = [[int(x) for x in number] for number in numbers]
numbers = np.array(numbers)
def get_ones(x, y):
ones = np.zeros((numbers.shape[0] + 2, numbers.shape[1] + 2))
ones[x : x + 3, y : y + 3] = 1
return ones[1:-1, 1:-1]
steps = 100
flashes = 0
for step in range(steps):
numbers += 1
nines_x, nines_y = np.where(numbers > 9)
coords = list(zip(nines_x, nines_y))
flashed = []
while len(coords) > 0:
coord = coords.pop()
flashed.append(coord)
x, y = coord
ones = get_ones(x, y)
numbers += ones.astype("int64")
nines_x, nines_y = np.where(numbers > 9)
new_coords = list(zip(nines_x, nines_y))
for new_coord in new_coords:
if new_coord not in flashed and new_coord not in coords:
coords.append(new_coord)
flashes += len(flashed)
for flash in flashed:
x, y = flash
numbers[x, y] = 0
print(flashes)