#!/usr/bin/env python import numpy as np from scipy.ndimage import convolve with open('day18.txt') as data: grid = np.stack([list(map(lambda x: x == '#', line.strip())) for line in data]).astype(int) neighbors = [[1, 1, 1], [1, 0, 1], [1, 1, 1]] grid1 = np.copy(grid) for i in range(100): neighbor_counts = convolve(grid1.astype(int), neighbors, mode='constant', cval=0) survive = grid1 * (neighbor_counts >= 2) * (neighbor_counts <= 3) grid1 = np.where(neighbor_counts == 3, True, survive) print(np.count_nonzero(grid1)) grid2 = np.copy(grid) for coord in {(0,0), (0, 99), (99, 0), (99, 99)}: grid2[coord] = True for i in range(100): neighbor_counts = convolve(grid2.astype(int), neighbors, mode='constant', cval=0) survive = grid2 * (neighbor_counts >= 2) * (neighbor_counts <= 3) grid2 = np.where(neighbor_counts == 3, True, survive) for coord in {(0,0), (0, 99), (99, 0), (99, 99)}: grid2[coord] = True print(np.count_nonzero(grid2))