From fbb1bdf8a1be7949391a68265b202682da774773 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Thu, 17 Dec 2020 16:00:18 +1030 Subject: [PATCH] 2020 day 17 --- 2020/day17.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2020/input17 | 8 ++++++++ 2 files changed, 56 insertions(+) create mode 100644 2020/day17.py create mode 100644 2020/input17 diff --git a/2020/day17.py b/2020/day17.py new file mode 100644 index 0000000..9ed3bb6 --- /dev/null +++ b/2020/day17.py @@ -0,0 +1,48 @@ +import numpy as np +with open('input17', 'r') as file: + init_state = np.array([[c == '#' for c in l.strip()] for l in file.readlines()]) + +def sim_step3d(state): + new_state = set() + neighbours = {} + for (x,y,z) in state: + for i in (-1,0,1): + for j in (-1,0,1): + for k in (-1,0,1): + neighbours[(x+i,y+j,z+k)] = neighbours.get((x+i,y+j,z+k), 0) + 1 + for loc, n in neighbours.items(): + if loc in state: + if n in (3,4): # We added an off-by-one by counting active cubes as their own neighbour for simplicity + new_state.add(loc) + elif n == 3: + new_state.add(loc) + return new_state + +state = {(x,y,0) for (x, y), v in np.ndenumerate(init_state) if v} +for i in range(6): + state = sim_step3d(state) +print(f'Part 1: {len(state)}') + + +def sim_step4d(state): + new_state = set() + neighbours = {} + for (x,y,z,u) in state: + for i in (-1,0,1): + for j in (-1,0,1): + for k in (-1,0,1): + for l in (-1,0,1): + neighbours[(x+i,y+j,z+k,u+l)] = neighbours.get((x+i,y+j,z+k,u+l), 0) + 1 + for loc, n in neighbours.items(): + if loc in state: + if n in (3,4): # We added an off-by-one by counting active cubes as their own neighbour for simplicity + new_state.add(loc) + elif n == 3: + new_state.add(loc) + return new_state + + +state = {(x,y,0,0) for (x, y), v in np.ndenumerate(init_state) if v} +for i in range(6): + state = sim_step4d(state) +print(f'Part 2: {len(state)}') diff --git a/2020/input17 b/2020/input17 new file mode 100644 index 0000000..2869685 --- /dev/null +++ b/2020/input17 @@ -0,0 +1,8 @@ +##..#### +.###.... +#.###.## +#....#.. +...#..#. +#.#...## +..#.#.#. +.##...#.