Day 17 (minimized part 2)

This commit is contained in:
Sebastian Seedorf
2020-12-17 20:20:55 +01:00
parent 4a2ade2def
commit bc46bbdc14
3 changed files with 9 additions and 8 deletions

View File

@@ -12,14 +12,13 @@ kernel[(1, )*DIMS] = 0
for _ in range(6): for _ in range(6):
grid = np.pad(grid, pad_width=1, mode='constant', constant_values=0) grid = np.pad(grid, pad_width=1)
neighbors = convolve(grid, kernel, mode='same') neighbors = convolve(grid, kernel, mode='same')
set_inactive = np.logical_and(grid == 1, np.floor_divide(neighbors, 2) != 1) set_inactive = np.logical_and(grid == 1, np.floor_divide(neighbors, 2) != 1)
set_active = np.logical_and(grid == 0, neighbors == 3) set_active = np.logical_and(grid == 0, neighbors == 3)
grid[set_inactive] = 0 grid[set_inactive] = 0
grid[set_active] = 1 grid[set_active] = 1
for dim in range(DIMS): a = tuple(slice(np.min(idxs), np.max(idxs) + 1) for idxs in np.where(grid == 0))
a = np.flatnonzero(grid.sum(axis=tuple(x for x in range(DIMS) if x != dim))) grid = grid[a]
grid = grid[(slice(None),)*dim + (slice(min(a), max(a)+1),)]
print(np.sum(grid)) print(np.sum(grid))

3
day17/part2-minified.py Normal file
View File

@@ -0,0 +1,3 @@
import numpy as m;from scipy.signal import convolve as c;g,k,a=m.expand_dims(m.array([[(1if x=='#'else 0)for x in x.strip()]for x in open("input.txt")]),axis=(0,1)),m.ones((3,)*4,dtype=m.byte),m.logical_and;k[(1,)*4]=0
for _ in range(6):g=m.pad(g,pad_width=1);n=c(g,k,mode='same');g[a(g==1,m.isin(n,(2,3))!=1)]=0;g[a(g==0,n==3)]=1
print(m.sum(g))

View File

@@ -12,14 +12,13 @@ kernel[(1, )*DIMS] = 0
for _ in range(6): for _ in range(6):
grid = np.pad(grid, pad_width=1, mode='constant', constant_values=0) grid = np.pad(grid, pad_width=1)
neighbors = convolve(grid, kernel, mode='same') neighbors = convolve(grid, kernel, mode='same')
set_inactive = np.logical_and(grid == 1, np.floor_divide(neighbors, 2) != 1) set_inactive = np.logical_and(grid == 1, np.floor_divide(neighbors, 2) != 1)
set_active = np.logical_and(grid == 0, neighbors == 3) set_active = np.logical_and(grid == 0, neighbors == 3)
grid[set_inactive] = 0 grid[set_inactive] = 0
grid[set_active] = 1 grid[set_active] = 1
for dim in range(DIMS): a = tuple(slice(np.min(idxs), np.max(idxs) + 1) for idxs in np.where(grid == 0))
a = np.flatnonzero(grid.sum(axis=tuple(x for x in range(DIMS) if x != dim))) grid = grid[a]
grid = grid[(slice(None),)*dim + (slice(min(a), max(a)+1),)]
print(np.sum(grid)) print(np.sum(grid))