Day 17 (dimension independent)

This commit is contained in:
Sebastian Seedorf
2020-12-17 12:28:51 +01:00
parent f8fca68bd8
commit 42ae3c7352
2 changed files with 14 additions and 16 deletions

View File

@@ -3,11 +3,12 @@
import numpy as np
from scipy import signal
DIMS = 3
lines = [[(1 if x == '#' else 0) for x in x.strip()] for x in open("input.txt")]
grid = np.array(lines, dtype=np.byte)
grid = np.expand_dims(grid, axis=0)
kernel = np.ones((3, 3, 3), dtype=np.byte)
kernel[1, 1, 1] = 0
grid = np.expand_dims(grid, axis=tuple(range(DIMS-2)))
kernel = np.ones((3, )*DIMS, dtype=np.byte)
kernel[(1, )*DIMS] = 0
for iter in range(6):
@@ -17,9 +18,8 @@ for iter in range(6):
set_active = np.logical_and(grid == 0, neighbors == 3)
grid[set_inactive] = 0
grid[set_active] = 1
x = np.flatnonzero(grid.sum(axis=(1, 2)))
y = np.flatnonzero(grid.sum(axis=(0, 2)))
z = np.flatnonzero(grid.sum(axis=(0, 1)))
grid = grid[min(x):max(x)+1, min(y):max(y)+1, min(z):max(z)+1]
for dim in range(DIMS):
a = np.flatnonzero(grid.sum(axis=tuple(x for x in range(DIMS) if x!=dim)))
grid = grid[(slice(None), )*dim + (slice(min(a), max(a)+1), )]
print(np.sum(grid))

View File

@@ -3,12 +3,12 @@
import numpy as np
from scipy import signal
DIMS = 4
lines = [[(1 if x == '#' else 0) for x in x.strip()] for x in open("input.txt")]
grid = np.array(lines, dtype=np.byte)
grid = np.expand_dims(grid, axis=0)
grid = np.expand_dims(grid, axis=0)
kernel = np.ones((3, 3, 3, 3), dtype=np.byte)
kernel[1, 1, 1, 1] = 0
grid = np.expand_dims(grid, axis=tuple(range(DIMS-2)))
kernel = np.ones((3, )*DIMS, dtype=np.byte)
kernel[(1, )*DIMS] = 0
for iter in range(6):
@@ -18,10 +18,8 @@ for iter in range(6):
set_active = np.logical_and(grid == 0, neighbors == 3)
grid[set_inactive] = 0
grid[set_active] = 1
x = np.flatnonzero(grid.sum(axis=(1, 2, 3)))
y = np.flatnonzero(grid.sum(axis=(0, 2, 3)))
z = np.flatnonzero(grid.sum(axis=(0, 1, 3)))
w = np.flatnonzero(grid.sum(axis=(0, 1, 2)))
grid = grid[min(x):max(x)+1, min(y):max(y)+1, min(z):max(z)+1, min(w):max(w)+1]
for dim in range(DIMS):
a = np.flatnonzero(grid.sum(axis=tuple(x for x in range(DIMS) if x!=dim)))
grid = grid[(slice(None), )*dim + (slice(min(a), max(a)+1), )]
print(np.sum(grid))