From bc46bbdc1498dc8ff1bd8cba7699d67194ac0020 Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Thu, 17 Dec 2020 20:20:55 +0100 Subject: [PATCH] Day 17 (minimized part 2) --- day17/part1.py | 7 +++---- day17/part2-minified.py | 3 +++ day17/part2.py | 7 +++---- 3 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 day17/part2-minified.py diff --git a/day17/part1.py b/day17/part1.py index 3bc0b31..631c9a2 100644 --- a/day17/part1.py +++ b/day17/part1.py @@ -12,14 +12,13 @@ kernel[(1, )*DIMS] = 0 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') set_inactive = np.logical_and(grid == 1, np.floor_divide(neighbors, 2) != 1) set_active = np.logical_and(grid == 0, neighbors == 3) grid[set_inactive] = 0 grid[set_active] = 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),)] + a = tuple(slice(np.min(idxs), np.max(idxs) + 1) for idxs in np.where(grid == 0)) + grid = grid[a] print(np.sum(grid)) diff --git a/day17/part2-minified.py b/day17/part2-minified.py new file mode 100644 index 0000000..6021c59 --- /dev/null +++ b/day17/part2-minified.py @@ -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)) diff --git a/day17/part2.py b/day17/part2.py index 1c8bbe5..44fb3bd 100644 --- a/day17/part2.py +++ b/day17/part2.py @@ -12,14 +12,13 @@ kernel[(1, )*DIMS] = 0 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') set_inactive = np.logical_and(grid == 1, np.floor_divide(neighbors, 2) != 1) set_active = np.logical_and(grid == 0, neighbors == 3) grid[set_inactive] = 0 grid[set_active] = 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),)] + a = tuple(slice(np.min(idxs), np.max(idxs) + 1) for idxs in np.where(grid == 0)) + grid = grid[a] print(np.sum(grid))