diff --git a/day17/input.txt b/day17/input.txt new file mode 100644 index 0000000..eedd3d2 --- /dev/null +++ b/day17/input.txt @@ -0,0 +1,3 @@ +.#. +..# +### diff --git a/day17/part1.py b/day17/part1.py new file mode 100644 index 0000000..ddf6272 --- /dev/null +++ b/day17/part1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import numpy as np +from scipy import signal + +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 + + +for iter in range(6): + grid = np.pad(grid, pad_width=1, mode='constant', constant_values=0) + neighbors = signal.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 + 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[x, :, :][:, y, :][:, :, z] + +print(np.sum(grid)) diff --git a/day17/part2.py b/day17/part2.py new file mode 100644 index 0000000..146517b --- /dev/null +++ b/day17/part2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +import numpy as np +from scipy import signal + +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) +print(grid.shape) +kernel = np.ones((3, 3, 3, 3), dtype=np.byte) +kernel[1, 1, 1, 1] = 0 + + +for iter in range(2): + grid = np.pad(grid, pad_width=1, mode='constant', constant_values=0) + neighbors = signal.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 + 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[x, :, :, :][:, y, :, :][:, :, z, :][:, :, :, w] + + print(iter+1, grid.shape) + +print(np.sum(grid))