|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) 2021 Andrew Krepps |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | + |
| 24 | +from . import util |
| 25 | + |
| 26 | + |
| 27 | +def parse_initial_state(lines): |
| 28 | + initial_state = set() |
| 29 | + max_dim = 0 |
| 30 | + for y, line in enumerate(lines): |
| 31 | + max_dim = max(max_dim, y + 1) |
| 32 | + for x, c in enumerate(line.strip()): |
| 33 | + max_dim = max(max_dim, x + 1) |
| 34 | + if c == '#': |
| 35 | + initial_state.add((x, y, 0, 0)) |
| 36 | + return initial_state, max_dim |
| 37 | + |
| 38 | + |
| 39 | +def count_active_neighbors(x, y, z, w, current_state): |
| 40 | + count = 0 |
| 41 | + for test_x in range(x - 1, x + 2): |
| 42 | + for test_y in range(y - 1, y + 2): |
| 43 | + for test_z in range(z - 1, z + 2): |
| 44 | + for test_w in range(w - 1, w + 2): |
| 45 | + if (test_x != x or test_y != y or test_z != z or test_w != w) \ |
| 46 | + and (test_x, test_y, test_z, test_w) in current_state: |
| 47 | + count += 1 |
| 48 | + # stop early if count is above rule thresholds |
| 49 | + if count > 3: |
| 50 | + return count |
| 51 | + return count |
| 52 | + |
| 53 | + |
| 54 | +def cube_will_be_active(x, y, z, w, current_state): |
| 55 | + active_neighbors = count_active_neighbors(x, y, z, w, current_state) |
| 56 | + return active_neighbors == 3 or (active_neighbors == 2 and (x, y, z, w) in current_state) |
| 57 | + |
| 58 | + |
| 59 | +def do_simulation_step(current_state, dim_size, w_size): |
| 60 | + next_state = set() |
| 61 | + xyz_range = range(-dim_size + 1, dim_size) |
| 62 | + for x in xyz_range: |
| 63 | + for y in xyz_range: |
| 64 | + for z in xyz_range: |
| 65 | + for w in range(-w_size + 1, w_size): |
| 66 | + if cube_will_be_active(x, y, z, w, current_state): |
| 67 | + next_state.add((x, y, z, w)) |
| 68 | + return next_state |
| 69 | + |
| 70 | + |
| 71 | +def run_simulation(initial_state, max_dim, num_steps, max_w=-1): |
| 72 | + state = initial_state |
| 73 | + for step in range(num_steps): |
| 74 | + dim_size = max_dim + step + 1 |
| 75 | + if max_w < 0: |
| 76 | + w_size = dim_size |
| 77 | + else: |
| 78 | + w_size = max_w |
| 79 | + state = do_simulation_step(state, max_dim + step + 1, w_size) |
| 80 | + return state |
| 81 | + |
| 82 | + |
| 83 | +def get_part1_answer(initial_state, max_dim): |
| 84 | + return len(run_simulation(initial_state, max_dim, 6, max_w=1)) |
| 85 | + |
| 86 | + |
| 87 | +def get_part2_answer(initial_state, max_dim): |
| 88 | + return len(run_simulation(initial_state, max_dim, 6)) |
| 89 | + |
| 90 | + |
| 91 | +def run(): |
| 92 | + lines = util.get_input_file_lines("day17.txt") |
| 93 | + initial_state, max_dim = parse_initial_state(lines) |
| 94 | + print(f"The answer to part 1 is {get_part1_answer(initial_state, max_dim)}") |
| 95 | + print(f"The answer to part 2 is {get_part2_answer(initial_state, max_dim)}") |
0 commit comments