From a82057d89c33400cedb91bb9e4dfd92afde5a059 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Fri, 23 Dec 2022 06:49:29 +0100 Subject: [PATCH] day23 --- day23.py | 85 +++++++++++++++++++++++++++++++++++++++ inputs/input23 | 70 ++++++++++++++++++++++++++++++++ inputs/input23_test | 7 ++++ inputs/input23_test_small | 6 +++ 4 files changed, 168 insertions(+) create mode 100644 day23.py create mode 100644 inputs/input23 create mode 100644 inputs/input23_test create mode 100644 inputs/input23_test_small diff --git a/day23.py b/day23.py new file mode 100644 index 0000000..0887f43 --- /dev/null +++ b/day23.py @@ -0,0 +1,85 @@ +from collections import defaultdict + +from tools.aoc import AOCDay +from tools.coordinate import Coordinate +from tools.grid import Grid +from typing import Any + + +CHECK_ORDER = [ + (Coordinate(0, -1), Coordinate(-1, -1), Coordinate(1, -1)), + (Coordinate(0, 1), Coordinate(1, 1), Coordinate(-1, 1)), + (Coordinate(-1, 0), Coordinate(-1, 1), Coordinate(-1, -1)), + (Coordinate(1, 0), Coordinate(1, 1), Coordinate(1, -1)) +] + + +def move_elfs(map: Grid, round: int) -> bool: + order_index = round % 4 + + move_to = {} + proposed_positions = defaultdict(int) + for elf in map.getActiveCells(): + if map.getNeighbourSum(elf, includeDiagonal=True) == 0: + continue + + for i in range(4): + check_dir = CHECK_ORDER[(order_index + i) % 4] + free = True + for check_pos in check_dir: + if map.get(elf + check_pos): + free = False + break + + if free: + move_to[elf] = elf + check_dir[0] + proposed_positions[elf + check_dir[0]] += 1 + break + + if not move_to: + return False + + for elf, target in move_to.items(): + if proposed_positions[target] == 1: + map.toggle(elf) + map.toggle(target) + + return True + + +class Day(AOCDay): + inputs = [ + [ + (25, "input23_test_small"), + (110, "input23_test"), + (3766, "input23"), + ], + [ + (20, "input23_test"), + (954, "input23"), + ] + ] + + def part1(self) -> Any: + map = Grid.from_str("/".join(self.getInput()), true_char='#') + + for i in range(10): + if not move_elfs(map, i): + break + + map.recalcBoundaries() + return (map.maxX - map.minX + 1) * (map.maxY - map.minY + 1) - map.getOnCount() + + def part2(self) -> Any: + map = Grid.from_str("/".join(self.getInput()), true_char='#') + round = 0 + + while move_elfs(map, round): + round += 1 + + return round + 1 + + +if __name__ == '__main__': + day = Day(2022, 23) + day.run(verbose=True) diff --git a/inputs/input23 b/inputs/input23 new file mode 100644 index 0000000..8a1bcee --- /dev/null +++ b/inputs/input23 @@ -0,0 +1,70 @@ +.#...#..#..#.##...#.##..#.#####.#........##.##....#..#.#.#.#....#..### +#..#.#...#..#.#.#.###...#...#.########..#..####.....#.#..###..###.##.. +###....#....#.##.#....###..#...###..###...#####.#..##.#.##.#.#.....#.# +...#.###..#####.##..#......#......##..#..#..###.##.#...#.##..#...##... +.......#.####.#.##...#..##..###.....##.###.#...###.###...##.#..#.#...# +..#.##.#.##.#...##.#.###..#.##..#......#...#####.#.#.#.####.#..#..##.# +.#..######...#.##.##..#.########....#.....#.##..#....###.#.##..######. +.#..##.#....#.#.##..##..#...###.#.##.###..#.#.#.##.#..########..#..#.# +.###.###.###.##..#.#..#...##..####..###..###.##..##.#..#...###.#.##### +.#####...#####.#.###...###...##.###.#.#.#..#...###.#.#.#.....#.##..#.# +#.#....#######.#...###.###...##.##....###..###..#..###.#...#.#..##.##. +...###.####....#.#....#.###....####...#...#..##......##.##.#..#.#.###. +.#...#...##..####....##..##.#..###.#.##..####.#...........####....###. +...###...#.#.#.###.########..###..##..#.#.#.####..#..#.##..###.#####.. +#..##...###.####..#....#..##.###.##.#...###.##..#..#.##..#....#######. +##.##.....##..##..###.#..#.#.#..#..#....##.#........#.#.#####......#.. +...##........#.###..##.#.##..####..#####...##....####...#.###...#..... +..#####.###.#.#..#.#.#..#...#.##..#..#.#...#..#.#..###..#....#.##..... +....##.###..#..#..######.###.##.#.###.#.###..###.#..#..##.#.......##.. +##..#..#......###.#..##.##.#...##...##...#..#.......#....#.#.#.###...# +###....#..#..#..#.#..#...##.##...#.#.#.#.#..#..###.####..##...#.##..## +#.......####.####.#.#.###.......#.......#.###..#.#.###.######..#...... +.#.#.#.##.#.##.#####..##.#.####.#......##.###.#..######..#...#..##...# +##.....###..###..#.####.#########.####..###.#...#..###.#..###.#..#.##. +###.#.#.###..##....#.###.#.##..#....#..####.#..###..##.....#....#...#. +....#..##..###.##.###.##.##.##..#..#.#.#.##.###..########.#.##.#..#.#. +##....##..#....##..###...#####.##.#...###.##...#####..#..###.#.#..#.## +.#.##.#.##...###.....#......#.###.#.###...##...#.##.##..#.####...##.## +..###.#..###...#.###.##..#....#.#.#...#.#..#...##....##...#....#..##.# +####.#.#..#...###..###...#.###...#...###..#####.#.#..##...##.......### +..#.##.##..#.#.#.#.##....##..#....##..##.##..#....###....#..##..#..### +..###.#...###.#.#..#.####..#.#.##...##..#######..##.#.##.##.#.#.#.##.. +..#..##.#..######...##.##...#.#.####.##.#...#.......##.#..##.#.#.##.## +.....##....#####...#....#.#....#####...#..##.......########..####.##.# +#.#.#..#...####.####.##....#..#.###.##....#..#...#..#..#.#.###..##.### +.#..#...#..#....#.###.##..#..##.####.####..##...##..###.#####..###.#.# +..#.##.######..#.#..#.#..##.#.#......#.#.#..#.#.#..#.##..#....#...#... +...##...##.##.#.##....#.####...#..#.#.....#.#.###...#.....#.###..#...# +###.##.#.#.........#####..#.#...#.##..##.#.######.#.....##..##.#.##.#. +##.#.#.##..#...#.....#####....#...#...#..#...#.##...###.####....#..### +#.#....#.###....#..#....###.####...###.##..#..##...############.####.. +.#..#...##.##...#.##.##.#.#...#.##..#....#...##....##.#.#.....#...#... +##.###.#.#####.#.#.#####.#.###.....##.#.#.##.#..#.###..#.....#.....#.. +#.#...#.##..#.##..###.....#.###......#.#.####...#..#..##....##.#.#..## +.#.##.#.##..###.##.#.##.##.#.##.#.##..###...##....#..#....###..####..# +...#......#.#.###.....####.##.####.####.#..#######....##....#.###..##. +##.##..#..#.#.#.####.#.#...####.#..#.#..####.##.##.#...#..#...##.#.... +#..#####..#.....###.#.....#.#.####..##....##..###.##.##..#..##....#### +..##..###..#..###.#..#..#.######..#...#...#..###..#..#.#.########.#.#. +.#####..##.#...##..####..#..####...###.###.#.###......##.#..##.#...#.# +####..#...#.#..#...###..##.#.#.##.....#.#.##...##..####.####.....###.. +...#.....##.##...#..##..#.#..##.##.#.#..##...#.#....#..##....######... +....#..####.#.#...####.....#.#.###.....##...##..###..#...###........#. +..##.......#.####...#.##..##.##.#.#....#...#.##..#...#...########.###. +....###..#.####.##.###........###.##..####.#.##.#.##...###..#.#..##.## +.#####....#.#.###.#.##...#..#.#.##...#.#...##..##........#####..####.. +#.#.#.#.##..###.###..#..#..####.........##.##...#....##.#####.#####.#. +..#.###..#..##..#.......#.#...#..##.#.#.......##.#####.####.#.##.#..## +#.......####....#.##.#.#..#.....#.#####..#....###.#....##...##.#.#...# +#...####.##.#####..#####..#.#.#..###...####..##..#.##.#####.#.####.##. +....#.#..#.#...###....###.######..#.####.....#....###..##..#.####.##.. +.#..##...##..##.###.##.###....#..#....#..###.##....#......####...#.### +##.##.......##.#..##.#....#.##.#.###.....##.#.#....##.....###.#..#.#.. +#.#.#..#..#...###.......#.#..#.###.#..#...###.#####.#.#..####...##.##. +..#.####.####..##....####.###...###.###..#..#..###.####.##...#####.... +##.#.#.....#.#.##...##...##..#..#...###....#.#.#..##....#####...#####. +##.#....#..##.#.######.#.#....##.#.##.####.###.#.......##...##.#..##.# +####.###..#.#.#...##.#...##...#...#.#.##..#..#.##.##.###..#...####.... +##.##.....#.#..#.....###.###.############.###.#..####..#.#.#.#...###.# +...#.#.#.###..####..#......##.##.###..##..#####..#.##.#.#.##.#######.. diff --git a/inputs/input23_test b/inputs/input23_test new file mode 100644 index 0000000..7ac3ba9 --- /dev/null +++ b/inputs/input23_test @@ -0,0 +1,7 @@ +....#.. +..###.# +#...#.# +.#...## +#.###.. +##.#.## +.#..#.. \ No newline at end of file diff --git a/inputs/input23_test_small b/inputs/input23_test_small new file mode 100644 index 0000000..e2a080c --- /dev/null +++ b/inputs/input23_test_small @@ -0,0 +1,6 @@ +..... +..##. +..#.. +..... +..##. +..... \ No newline at end of file