Day 17 (dimension independent)
This commit is contained in:
@@ -3,11 +3,12 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy import signal
|
from scipy import signal
|
||||||
|
|
||||||
|
DIMS = 3
|
||||||
lines = [[(1 if x == '#' else 0) for x in x.strip()] for x in open("input.txt")]
|
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.array(lines, dtype=np.byte)
|
||||||
grid = np.expand_dims(grid, axis=0)
|
grid = np.expand_dims(grid, axis=tuple(range(DIMS-2)))
|
||||||
kernel = np.ones((3, 3, 3), dtype=np.byte)
|
kernel = np.ones((3, )*DIMS, dtype=np.byte)
|
||||||
kernel[1, 1, 1] = 0
|
kernel[(1, )*DIMS] = 0
|
||||||
|
|
||||||
|
|
||||||
for iter in range(6):
|
for iter in range(6):
|
||||||
@@ -17,9 +18,8 @@ for iter in range(6):
|
|||||||
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
|
||||||
x = np.flatnonzero(grid.sum(axis=(1, 2)))
|
for dim in range(DIMS):
|
||||||
y = np.flatnonzero(grid.sum(axis=(0, 2)))
|
a = np.flatnonzero(grid.sum(axis=tuple(x for x in range(DIMS) if x!=dim)))
|
||||||
z = np.flatnonzero(grid.sum(axis=(0, 1)))
|
grid = grid[(slice(None), )*dim + (slice(min(a), max(a)+1), )]
|
||||||
grid = grid[min(x):max(x)+1, min(y):max(y)+1, min(z):max(z)+1]
|
|
||||||
|
|
||||||
print(np.sum(grid))
|
print(np.sum(grid))
|
||||||
|
|||||||
@@ -3,12 +3,12 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy import signal
|
from scipy import signal
|
||||||
|
|
||||||
|
DIMS = 4
|
||||||
lines = [[(1 if x == '#' else 0) for x in x.strip()] for x in open("input.txt")]
|
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.array(lines, dtype=np.byte)
|
||||||
grid = np.expand_dims(grid, axis=0)
|
grid = np.expand_dims(grid, axis=tuple(range(DIMS-2)))
|
||||||
grid = np.expand_dims(grid, axis=0)
|
kernel = np.ones((3, )*DIMS, dtype=np.byte)
|
||||||
kernel = np.ones((3, 3, 3, 3), dtype=np.byte)
|
kernel[(1, )*DIMS] = 0
|
||||||
kernel[1, 1, 1, 1] = 0
|
|
||||||
|
|
||||||
|
|
||||||
for iter in range(6):
|
for iter in range(6):
|
||||||
@@ -18,10 +18,8 @@ for iter in range(6):
|
|||||||
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
|
||||||
x = np.flatnonzero(grid.sum(axis=(1, 2, 3)))
|
for dim in range(DIMS):
|
||||||
y = np.flatnonzero(grid.sum(axis=(0, 2, 3)))
|
a = np.flatnonzero(grid.sum(axis=tuple(x for x in range(DIMS) if x!=dim)))
|
||||||
z = np.flatnonzero(grid.sum(axis=(0, 1, 3)))
|
grid = grid[(slice(None), )*dim + (slice(min(a), max(a)+1), )]
|
||||||
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]
|
|
||||||
|
|
||||||
print(np.sum(grid))
|
print(np.sum(grid))
|
||||||
|
|||||||
Reference in New Issue
Block a user