diff options
author | Brian Chu <brianmchu42@gmail.com> | 2021-12-30 15:11:21 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2021-12-30 15:11:21 -0800 |
commit | e7085453864431ace3ad8f3123b259ed0829ae74 (patch) | |
tree | 2ef1fbb0e9d02fc934b5e09d96dd187f3e371ea6 /day18.py | |
download | AdventOfCode2015-e7085453864431ace3ad8f3123b259ed0829ae74.tar.gz |
all solutions for 2015 main
Diffstat (limited to 'day18.py')
-rw-r--r-- | day18.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/day18.py b/day18.py new file mode 100644 index 0000000..033cc76 --- /dev/null +++ b/day18.py @@ -0,0 +1,33 @@ +#!/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)) |