From c87b1b75e8dd0fe2f06886d23e0f74cf300512be Mon Sep 17 00:00:00 2001 From: Abhishek Maharjan Date: Fri, 6 Dec 2024 16:41:50 +0100 Subject: [PATCH] feat(2024): day 6 - Guard Gallivant (#14) --- 2024/Day06/README.md | 220 +++++++++++++++++++++++++++++++++- 2024/Day06/guard_gallivant.py | 90 ++++++++++++++ 2024/Day06/input.txt | 130 ++++++++++++++++++++ 2024/Day06/input_test.txt | 10 ++ 2024/Day06/solutions.py | 9 +- 2024/Day06/test_solutions.py | 11 +- 2024/utils/output.py | 6 + 7 files changed, 466 insertions(+), 10 deletions(-) create mode 100644 2024/Day06/guard_gallivant.py diff --git a/2024/Day06/README.md b/2024/Day06/README.md index 98e93b9..b053f8d 100644 --- a/2024/Day06/README.md +++ b/2024/Day06/README.md @@ -1,3 +1,219 @@ -# --- Day 0: Template --- +# --- Day 6: Guard Gallivant --- -To be used as template for the other days. +The Historians use their fancy device again, this time to whisk you all away to the North Pole prototype suit manufacturing lab... in the year 1518! It turns out that having direct access to history is very convenient for a group of historians. + +You still have to be careful of time paradoxes, and so it will be important to avoid anyone from `1518` while The Historians search for the Chief. Unfortunately, a single guard is patrolling this part of the lab. + +Maybe you can work out where the guard will go ahead of time so that The Historians can search safely? + +You start by making a map (your puzzle input) of the situation. For example: + +```txt +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... +``` + +The map shows the current position of the guard with ^ (to indicate the guard is currently facing up from the perspective of the map). Any obstructions - crates, desks, alchemical reactors, etc. - are shown as `#`. + +Lab guards in `1518` follow a very strict patrol protocol which involves repeatedly following these steps: + +If there is something directly in front of you, turn right `90` degrees. +Otherwise, take a step forward. +Following the above protocol, the guard moves up several times until she reaches an obstacle (in this case, a pile of failed suit prototypes): + +```txt +....#..... +....^....# +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#... +``` + +Because there is now an obstacle in front of the guard, she turns right before continuing straight in her new facing direction: + +```txt +....#..... +........># +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#... +``` + +Reaching another obstacle (a spool of several very long polymers), she turns right again and continues downward: + +```txt +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#......v. +........#. +#......... +......#... +``` + +This process continues for a while, but the guard eventually leaves the mapped area (after walking past a tank of universal solvent): + +```txt +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#v.. +``` + +By predicting the guard's route, you can determine which specific positions in the lab will be in the patrol path. Including the guard's starting position, the positions visited by the guard before leaving the area are marked with an `X`: + +```txt +....#..... +....XXXXX# +....X...X. +..#.X...X. +..XXXXX#X. +..X.X.X.X. +.#XXXXXXX. +.XXXXXXX#. +#XXXXXXX.. +......#X.. +``` + +In this example, the guard will visit `41` distinct positions on your map. + +Predict the path of the guard. How many distinct positions will the guard visit before leaving the mapped area? + +> Your puzzle answer was `5461`. + +## --- Part Two --- + +While The Historians begin working around the guard's patrol route, you borrow their fancy device and step outside the lab. From the safety of a supply closet, you time travel through the last few months and record the nightly status of the lab's guard post on the walls of the closet. + +Returning after what seems like only a few seconds to The Historians, they explain that the guard's patrol area is simply too large for them to safely search the lab without getting caught. + +Fortunately, they are pretty sure that adding a single new obstruction won't cause a time paradox. They'd like to place the new obstruction in such a way that the guard will get stuck in a loop, making the rest of the lab safe to search. + +To have the lowest chance of creating a time paradox, The Historians would like to know all of the possible positions for such an obstruction. The new obstruction can't be placed at the guard's starting position - the guard is there right now and would notice. + +In the above example, there are only 6 different positions where a new obstruction would cause the guard to get stuck in a loop. The diagrams of these six situations use O to mark the new obstruction, | to show a position where the guard moves up/down, - to show a position where the guard moves left/right, and + to show a position where the guard moves both up/down and left/right. + +Option one, put a printing press next to the guard's starting position: + +```txt +....#..... +....+---+# +....|...|. +..#.|...|. +....|..#|. +....|...|. +.#.O^---+. +........#. +#......... +......#... +``` + +Option two, put a stack of failed suit prototypes in the bottom right quadrant of the mapped area: + +```txt +....#..... +....+---+# +....|...|. +..#.|...|. +..+-+-+#|. +..|.|.|.|. +.#+-^-+-+. +......O.#. +#......... +......#... +``` + +Option three, put a crate of chimney-squeeze prototype fabric next to the standing desk in the bottom right quadrant: + +```txt +....#..... +....+---+# +....|...|. +..#.|...|. +..+-+-+#|. +..|.|.|.|. +.#+-^-+-+. +.+----+O#. +#+----+... +......#... +``` + +Option four, put an alchemical retroencabulator near the bottom left corner: + +```txt +....#..... +....+---+# +....|...|. +..#.|...|. +..+-+-+#|. +..|.|.|.|. +.#+-^-+-+. +..|...|.#. +#O+---+... +......#... +``` + +Option five, put the alchemical retroencabulator a bit to the right instead: + +```txt +....#..... +....+---+# +....|...|. +..#.|...|. +..+-+-+#|. +..|.|.|.|. +.#+-^-+-+. +....|.|.#. +#..O+-+... +......#... +``` + +Option six, put a tank of sovereign glue right next to the tank of universal solvent: + +```txt +....#..... +....+---+# +....|...|. +..#.|...|. +..+-+-+#|. +..|.|.|.|. +.#+-^-+-+. +.+----++#. +#+----++.. +......#O.. +``` + +It doesn't really matter what you choose to use as an obstacle so long as you and The Historians can put it into position without the guard noticing. The important thing is having enough options that you can find one that minimizes time paradoxes, and in this example, there are `6` different positions you could choose. + +You need to get the guard stuck in a loop by adding a single new obstruction. How many different positions could you choose for this obstruction? + +> Your puzzle answer was `1836`. + +Both parts of this puzzle are complete! They provide two gold stars: `**` diff --git a/2024/Day06/guard_gallivant.py b/2024/Day06/guard_gallivant.py new file mode 100644 index 0000000..31c219c --- /dev/null +++ b/2024/Day06/guard_gallivant.py @@ -0,0 +1,90 @@ +import os +from copy import deepcopy + +from termcolor import colored + +from utils.output import print_grid + + +class GuardGallivant: + def __init__(self, grid: list[str]): + self.grid = [list(row) for row in grid] + self.initial_direction = (0, -1) + self.rows, self.cols = len(self.grid), len(self.grid[0]) + self.visualize = os.environ.get("VISUALIZE", "False") == "True" + self.dir_map = { + (0, -1): "^", + (0, 1): "v", + (-1, 0): "<", + (1, 0): ">", + } + + def simulate_move(self, start: tuple[int, int]) -> set[tuple[int, int]]: + visited = set() + x, y = start + dx, dy = self.initial_direction + + while True: + visited.add((x, y)) + nx, ny = x + dx, y + dy + + if not (0 <= nx < self.cols and 0 <= ny < self.rows): + break + + if self.grid[ny][nx] == "#": + dx, dy = -dy, dx + else: + x, y = nx, ny + + return visited + + def _get_start_pos(self) -> tuple[int, int]: + for y, row in enumerate(self.grid): + for x, cell in enumerate(row): + if cell == "^": + return x, y + return -1, -1 + + def _creates_loop(self, start: tuple[int, int]) -> bool: + visited_states = set() + x, y = start + dx, dy = self.initial_direction + + if self.visualize: + grid = deepcopy(self.grid) + + while True: + state = (x, y, dx, dy) + if state in visited_states: + return True + visited_states.add(state) + + if self.visualize: + grid[y][x] = colored(self.dir_map[(dx, dy)], "red") + print_grid(grid, delay=0.01, padding=True) + grid[y][x] = colored("*", "green") + + nx, ny = x + dx, y + dy + if not (0 <= nx < self.cols and 0 <= ny < self.rows): + return False + + if self.grid[ny][nx] == "#" or self.grid[ny][nx] == "O": + dx, dy = -dy, dx + else: + x, y = nx, ny + + def get_distinct_visited_positions(self) -> set[tuple[int, int]]: + start = self._get_start_pos() + return self.simulate_move(start) + + def get_num_distinct_visited_positions_with_loop(self) -> int: + start = self._get_start_pos() + positions = self.simulate_move(start) - {start} + + count = 0 + for x, y in positions: + self.grid[y][x] = "O" + if self._creates_loop(start): + count += 1 + self.grid[y][x] = "." + return count diff --git a/2024/Day06/input.txt b/2024/Day06/input.txt index e69de29..80f45c4 100644 --- a/2024/Day06/input.txt +++ b/2024/Day06/input.txt @@ -0,0 +1,130 @@ +....#.................#......................#..........................#..................#....##..#...........#................. +...................................#...............................#......#..#...............................#.................... +..........................#................#......##.....#.....................................#...............#..#............... +.......................................................................................................#.......................... +...................#....#.........................#..............#.....#......................................................#... +.....#.........................................................................................................#.................. +.........................................................#....................#..#............#................#.................. +..............#...............#..................................................................#......#......................... +.........#.....#.......#......#.......................#.............#..#........#.......#............#.......#....#..#.......#.... +........................#....#...............................................#...#.#.........................................#.... +.........................#........................................................................#.....................#......... +.#..........#...#...............#..................#...#......#.................................#....................#.......#.... +............................................................#......#.........................................................#.... +.......................#...#..................#.#.............#......................#.............#.............................. +............................................................................................................#.........#........... +....#..................###........#.............................#.#....................#..........#.....................#......... +................................................................................................#.......................#.#....... +..........................................................................#........#...#.................#.......#................ +..........................#.#..#.#...............................................#..........#..............................#...... +..............#........#.#...............#............................#..........#....................................#........#.# +#..................................................................................................#...........#................#. +...............................................#........................................#.................................#....... +.....#..#......#.......#..............#........#........#...........................................................#............. +.......#..........#.......#.#...........#................#.................................................#...................... +....#.....#...............#....#.......#...#.............................................................#...................#.... +............................................#....#.......#................................#......#......................#.....#... +.....................#...................................#....#...........................................................#....... +..#..............................#...........#...........................#.................................#...................... +........#.............................##...................#.....................#................................................ +................#.#.#......#...............................................#...###.........#....#................................. +.......#.............#.............#...........#..#...............................#..................................#.....#.....# +.....#...#.......#.........................................#..............................#........................#.............# +...............................................#.....#..............................................................#............. +.........#.......................................#.......................#...........#............................................ +.......................#.....#.....................................................#....................................#......... +.............................#......................#.....................#..............#..#.......................#............. +......#......................................#.......#.....................#.....#...........#..........#.....................#... +..................................................#....................#.......#....#.......#...#................................. +......................................#......#........#.....#........................................#.............#.............. +#......................#.#..............#.....#.........#...............................#...............................#......... +#......#...............#.........................................................................................#..#............. +.#..............................................#.................#.......#....................................................... +...#.......#.....#.............#...................................................................................#.............. +..........................#........#........#.....................................#........................#..............#..#.... +.......#..........#.......#...................................................#.##.....#.#.........#....#.................#....... +..##...................#.......................................#..............#.#........#........................................ +#.......#..............#...........#.....#....#....#............#........#..................................#.#.........#......... +..................#.......................................................................................................#....... +............................#.........#....................#...............................#..........#........................... +.............#.....................................................................#.....#.................#...................... +...#...#............#..........................#..........................................#....................................... +..#.............#.#........................#...#.......................#.........#....................##....#..................... +...........................................................................................................#.........#...#........ +.............................#.#..........................................................................................#....... +....................#........................#.#......#...............#......#.............................#...................... +....#...#.......#..................#..........................................................#........................#.......... +.#...........#..............................................#...........................................#......................... +..............................#...........#...........................................#.........#...........................#....# +............#.......#......#....#.......................................................................#......................... +.........................................#...#..................#..................#.....................................#........ +................#..................#........#.........................#...................#......................................# +............................................................#.....#........#...................................................... +..............................#............#.........................................#.............#.......#..#................... +...#............#........................................................................#..................................#....# +..............................#.......#.............#.................................................#........................... +.....#.................................................#..........#...............................#..##..........................# +.............#.........................................................#.............................#..#..............#........#. +....................#...............#................................................................................#....#......# +........#....#..#...........................................................................#................#..............#..... +....#......................#...............#..........#......#.........#..^....#..........#..................#.......#............ +...........................#.#...................................................#...............................#................ +#..#.........................................................#.....#........................#..................................... +.#.#....#.......................................#......#..........#................#..#........................................... +...#.....#..........#...................#.......#............................#.................................................... +.............#......................................................#............#.....................#.....................#.... +...................#.............................................................................................#................ +...................#.....#...................................................................................................#.... +................#............#......................#.......#..................#..#.....................#..............#........#. +#....#...................................................................#.......##....#..........#............................... +.........................................#............#......#.......................#........#..............#.#.................. +.##........#.....#...........................................................................................#..............#..... +#...........................................................#.......#....#......#..#...................................#..#.#..... +.............#........................................#...............................#........................................... +......................#..................................................................#..................................#..... +...#.#........................................#.................................................................................#. +.............#.......................#............................#.#................................#.......#..#...........#..... +...#..#...............................................................................#................#.......................... +.......#.............................#............#..#........#.....#.........................#..................#................ +......#...............................................................................##.................................#....#... +........#..................#.......................................................................#......#..........#............ +...........#.......#........#..........................................#........#..............#.................#.........#...... +...................#..............................#...............................#.....................#.....#................... +.............#..#..................#.#..#.....#..#..........................................................................#..... +..............................#...................................................#.......................#..............#........ +.......#......................#.............................#...........................#.....#...#.................#........#.... +.........#.......#....##.......................................................................................................... +........................................................................#......................#.................................. +.......................#..........##.#................................................#..#.....#......#......#.................#.. +......................................#......................................................................#.................... +.....................................#....#...............................................#...#.....#.......#...#...........#..... +...................#..#................................................................................#.......................... +..............#.................#..............................#..............#...........#.#..................#...#.............# +................#..#......#.........................#.....#.....#..............#..#.........................................#..... +.....#.........................#........................................#.....#.......#...........#...................#........... +......#....................##......................................................................................#.............. +............................................................................###...............................#................... +#..................................#......#..............................................................................#.......# +.....#...................#.....#............................................................#.....................#...#........... +.......#......#........................#.....................#.#..##............#......#.................#.....................#.. +.....#..............#.................#........#..................#..#..........................##.....#..#...................#... +........#................#................#...........#.........................................#....................#............ +.........#.........#....#................#........#........#...............#....#................................................. +...........#...#............#..................#.............................#.#..#....................#.#........................ +.#............#..#..#................#.#................................................................##.............#.......... +...........#.......................#.........................................................#.................................... +..#..........................#...........#......#................................#.#.............................................. +............................................................#..................#...........#.....##.................#............. +....#.........#............................##..........................................#.....#....#..........#.................... +..................#..............#........#........#....................#..............#....................#..................... +....#...................................#.....................................................#.............#...................#. +.........#...........#...#.............................#.............##..#................................................#.#..... +............................##..............#................#..#........................................#....................#... +.............................#.#......................................................##.....#..................................#. +.............................................................................#..................#...#.................#........... +........................#...........#..................#......#................................................................... +.....#.......................#......#................#............#..................#.................................#.......... +#..........#...................................................................#...................#...#...............#.......... +....................#............................................#...............#........................##....#..#.............. +.#.....#............................#...............#.#.#..........................#.........#..............#..................... +.............................#....................................#..#......#........................#............................ \ No newline at end of file diff --git a/2024/Day06/input_test.txt b/2024/Day06/input_test.txt index e69de29..b60e466 100644 --- a/2024/Day06/input_test.txt +++ b/2024/Day06/input_test.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... \ No newline at end of file diff --git a/2024/Day06/solutions.py b/2024/Day06/solutions.py index fda2aae..2992068 100644 --- a/2024/Day06/solutions.py +++ b/2024/Day06/solutions.py @@ -1,6 +1,11 @@ +from guard_gallivant import GuardGallivant + + def part1(inputs: list[str]) -> int: - return 1 + gg = GuardGallivant(inputs) + return len(gg.get_distinct_visited_positions()) def part2(inputs: list[str]) -> int: - return 2 + gg = GuardGallivant(inputs) + return gg.get_num_distinct_visited_positions_with_loop() diff --git a/2024/Day06/test_solutions.py b/2024/Day06/test_solutions.py index ed7fdb1..16c4e12 100644 --- a/2024/Day06/test_solutions.py +++ b/2024/Day06/test_solutions.py @@ -12,19 +12,18 @@ class TestPart1: + # @pytest.mark.skip(reason="not implemented") def test_with_test_data(self): - assert part1(input_test) == 1 + assert part1(input_test) == 41 - @pytest.mark.skip(reason="not implemented") def test_with_real_data(self): - assert part1(input) == 1 + assert part1(input) == 5461 class TestPart2: - @pytest.mark.skip(reason="not implemented") def test_with_test_data(self): - assert part2(input_test) == 2 + assert part2(input_test) == 6 @pytest.mark.skip(reason="not implemented") def test_with_real_data(self): - assert part2(input) == 2 + assert part2(input) == 1836 diff --git a/2024/utils/output.py b/2024/utils/output.py index 9e1aa07..c3f7b9f 100644 --- a/2024/utils/output.py +++ b/2024/utils/output.py @@ -29,3 +29,9 @@ def print_grid_dict(grid, mapper=lambda v: str(v), delay=0): if delay: time.sleep(delay) + + +def rotate_90_clockwise(matrix): + transposed = list(zip(*matrix)) + rotated = [list(row)[::-1] for row in transposed] + return rotated