Day 17 (glamorize)
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy import signal
|
from scipy.signal import convolve
|
||||||
|
|
||||||
DIMS = 3
|
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")]
|
||||||
@@ -11,15 +11,15 @@ kernel = np.ones((3, )*DIMS, dtype=np.byte)
|
|||||||
kernel[(1, )*DIMS] = 0
|
kernel[(1, )*DIMS] = 0
|
||||||
|
|
||||||
|
|
||||||
for iter 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, mode='constant', constant_values=0)
|
||||||
neighbors = signal.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):
|
for dim in range(DIMS):
|
||||||
a = np.flatnonzero(grid.sum(axis=tuple(x for x in range(DIMS) if x!=dim)))
|
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), )]
|
grid = grid[(slice(None),)*dim + (slice(min(a), max(a)+1),)]
|
||||||
|
|
||||||
print(np.sum(grid))
|
print(np.sum(grid))
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from scipy import signal
|
from scipy.signal import convolve
|
||||||
|
|
||||||
DIMS = 4
|
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")]
|
||||||
@@ -11,15 +11,15 @@ kernel = np.ones((3, )*DIMS, dtype=np.byte)
|
|||||||
kernel[(1, )*DIMS] = 0
|
kernel[(1, )*DIMS] = 0
|
||||||
|
|
||||||
|
|
||||||
for iter 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, mode='constant', constant_values=0)
|
||||||
neighbors = signal.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):
|
for dim in range(DIMS):
|
||||||
a = np.flatnonzero(grid.sum(axis=tuple(x for x in range(DIMS) if x!=dim)))
|
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), )]
|
grid = grid[(slice(None),)*dim + (slice(min(a), max(a)+1),)]
|
||||||
|
|
||||||
print(np.sum(grid))
|
print(np.sum(grid))
|
||||||
|
|||||||
Reference in New Issue
Block a user