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] flashes = 0 counter = 0 synchron_flash = False while not synchron_flash: counter += 1 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) for flash in flashed: x, y = flash numbers[x, y] = 0 if len(flashed) == 100: print(counter) synchron_flash = True