import numpy as np from scipy.ndimage import convolve1d with open('day18.txt') as data: line = np.array([char == '^' for char in data.read().strip()], dtype=int) kernel = [1, 2, 4] total = 100 - np.count_nonzero(line) for i in range(400000 - 1): line = convolve1d(line, kernel, mode="constant", cval=0) line = (line==3) | (line==6) | (line==1) | (line==4) line = line.astype(int) total += 100 - np.count_nonzero(line) if i == 38: print(total) print(total)