summary refs log tree commit diff stats
path: root/day18.py
blob: 033cc760f6ce4807559a4ab0187595fd8e1c085a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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))