Skip to content

Commit d736653

Browse files
committed
Add Day 6
1 parent 84083fe commit d736653

File tree

5 files changed

+656
-1
lines changed

5 files changed

+656
-1
lines changed

day-6/README.md

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
# Day 6: Guard Gallivant
2+
3+
<br>
4+
5+
## Part 1
6+
7+
The Historians use their fancy [device](https://adventofcode.com/2024/day/4) again, this time to whisk you all away to the North Pole
8+
prototype suit manufacturing lab... in the year [1518](https://adventofcode.com/2018/day/5)! It turns out that having direct access to
9+
history is very convenient for a group of historians.
10+
11+
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
12+
Chief. Unfortunately, a single **guard** is patrolling this part of the lab.
13+
14+
Maybe you can work out where the guard will go ahead of time so that The Historians can search safely?
15+
16+
You start by making a map (your puzzle input) of the situation. For example:
17+
18+
```txt
19+
....#.....
20+
.........#
21+
..........
22+
..#.......
23+
.......#..
24+
..........
25+
.#..^.....
26+
........#.
27+
#.........
28+
......#...
29+
```
30+
31+
The map shows the current position of the guard with `^` (to indicate the guard is currently facing **up** from the perspective of the map).
32+
Any **obstructions** - crates, desks, alchemical reactors, etc. - are shown as `#`.
33+
34+
Lab guards in 1518 follow a very strict patrol protocol which involves repeatedly following these steps:
35+
36+
- If there is something directly in front of you, turn right 90 degrees.
37+
- Otherwise, take a step forward.
38+
39+
Following the above protocol, the guard moves up several times until she reaches an obstacle (in this case, a pile of failed suit
40+
prototypes):
41+
42+
```txt
43+
....#.....
44+
....^....#
45+
..........
46+
..#.......
47+
.......#..
48+
..........
49+
.#........
50+
........#.
51+
#.........
52+
......#...
53+
```
54+
55+
Because there is now an obstacle in front of the guard, she turns right before continuing straight in her new facing direction:
56+
57+
```txt
58+
....#.....
59+
........>#
60+
..........
61+
..#.......
62+
.......#..
63+
..........
64+
.#........
65+
........#.
66+
#.........
67+
......#...
68+
```
69+
70+
Reaching another obstacle (a spool of several **very** long polymers), she turns right again and continues downward:
71+
72+
```txt
73+
....#.....
74+
.........#
75+
..........
76+
..#.......
77+
.......#..
78+
..........
79+
.#......v.
80+
........#.
81+
#.........
82+
......#...
83+
```
84+
85+
This process continues for a while, but the guard eventually leaves the mapped area (after walking past a tank of universal solvent):
86+
87+
```txt
88+
....#.....
89+
.........#
90+
..........
91+
..#.......
92+
.......#..
93+
..........
94+
.#........
95+
........#.
96+
#.........
97+
......#v..
98+
```
99+
100+
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
101+
starting position**, the positions visited by the guard before leaving the area are marked with an `X`:
102+
103+
```txt
104+
....#.....
105+
....XXXXX#
106+
....X...X.
107+
..#.X...X.
108+
..XXXXX#X.
109+
..X.X.X.X.
110+
.#XXXXXXX.
111+
.XXXXXXX#.
112+
#XXXXXXX..
113+
......#X..
114+
```
115+
116+
In this example, the guard will visit `41` distinct positions on your map.
117+
118+
Predict the path of the guard. **How many distinct positions will the guard visit before leaving the mapped area?**
119+
120+
<br>
121+
122+
## Part 2
123+
124+
While The Historians begin working around the guard's patrol route, you borrow their fancy device and step outside the lab. From the safety
125+
of a supply closet, you time travel through the last few months and [record](https://adventofcode.com/2018/day/4) the nightly status of the
126+
lab's guard post on the walls of the closet.
127+
128+
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
129+
to safely search the lab without getting caught.
130+
131+
Fortunately, they are **pretty sure** that adding a single new obstruction **won't** cause a time paradox. They'd like to place the new
132+
obstruction in such a way that the guard will get **stuck in a loop**, making the rest of the lab safe to search.
133+
134+
To have the lowest chance of creating a time paradox, The Historians would like to know **all** of the possible positions for such an
135+
obstruction. The new obstruction can't be placed at the guard's starting position - the guard is there right now and would notice.
136+
137+
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
138+
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
139+
position where the guard moves left/right, and `+` to show a position where the guard moves both up/down and left/right.
140+
141+
Option one, put a printing press next to the guard's starting position:
142+
143+
```txt
144+
....#.....
145+
....+---+#
146+
....|...|.
147+
..#.|...|.
148+
....|..#|.
149+
....|...|.
150+
.#.O^---+.
151+
........#.
152+
#.........
153+
......#...
154+
```
155+
156+
Option two, put a stack of failed suit prototypes in the bottom right quadrant of the mapped area:
157+
158+
```txt
159+
....#.....
160+
....+---+#
161+
....|...|.
162+
..#.|...|.
163+
..+-+-+#|.
164+
..|.|.|.|.
165+
.#+-^-+-+.
166+
......O.#.
167+
#.........
168+
......#...
169+
```
170+
171+
Option three, put a crate of chimney-squeeze prototype fabric next to the standing desk in the bottom right quadrant:
172+
173+
```txt
174+
....#.....
175+
....+---+#
176+
....|...|.
177+
..#.|...|.
178+
..+-+-+#|.
179+
..|.|.|.|.
180+
.#+-^-+-+.
181+
.+----+O#.
182+
#+----+...
183+
......#...
184+
```
185+
186+
Option four, put an alchemical retroencabulator near the bottom left corner:
187+
188+
```txt
189+
....#.....
190+
....+---+#
191+
....|...|.
192+
..#.|...|.
193+
..+-+-+#|.
194+
..|.|.|.|.
195+
.#+-^-+-+.
196+
..|...|.#.
197+
#O+---+...
198+
......#...
199+
```
200+
201+
Option five, put the alchemical retroencabulator a bit to the right instead:
202+
203+
```txt
204+
....#.....
205+
....+---+#
206+
....|...|.
207+
..#.|...|.
208+
..+-+-+#|.
209+
..|.|.|.|.
210+
.#+-^-+-+.
211+
....|.|.#.
212+
#..O+-+...
213+
......#...
214+
```
215+
216+
Option six, put a tank of sovereign glue right next to the tank of universal solvent:
217+
218+
```txt
219+
....#.....
220+
....+---+#
221+
....|...|.
222+
..#.|...|.
223+
..+-+-+#|.
224+
..|.|.|.|.
225+
.#+-^-+-+.
226+
.+----++#.
227+
#+----++..
228+
......#O..
229+
```
230+
231+
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
232+
noticing. The important thing is having enough options that you can find one that minimizes time paradoxes, and in this example, there are
233+
`6` different positions you could choose.
234+
235+
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
236+
obstruction?**

day-6/day-6.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert';
3+
import path from 'node:path';
4+
5+
import {
6+
determineNumberOfDistinctPositionsVisitedByGuard,
7+
determineNumberOfObstaclePositionsCausingALoop,
8+
determineNumberOfObstaclePositionsCausingALoopBruteforce,
9+
} from './day-6';
10+
11+
describe('Day 6: Guard Gallivant', () => {
12+
const mapFilePath = path.join(__dirname, 'map.txt');
13+
14+
it('Part 1: should determine number of distinct positions visited the guard', async () => {
15+
const expectedNumberOfDistinctPositionsVisitedByGuard = 5153; // Verified for this dataset
16+
17+
const numberOfDistinctPositionsVisitedByGuard = await determineNumberOfDistinctPositionsVisitedByGuard(mapFilePath);
18+
19+
assert.strictEqual(numberOfDistinctPositionsVisitedByGuard, expectedNumberOfDistinctPositionsVisitedByGuard);
20+
});
21+
22+
it('Part 2: should determine number of obstacle positions causing a loop', async () => {
23+
const expectedNumberOfObstaclePositionsCausingALoop = 1711; // Verified for this dataset
24+
25+
const numberOfObstaclePositionsCausingALoop = await determineNumberOfObstaclePositionsCausingALoop(mapFilePath);
26+
27+
assert.strictEqual(numberOfObstaclePositionsCausingALoop, expectedNumberOfObstaclePositionsCausingALoop);
28+
});
29+
30+
it('Part 2 (bruteforce): should determine number of obstacle positions causing a loop', async () => {
31+
const expectedNumberOfObstaclePositionsCausingALoop = 1711; // Verified for this dataset
32+
33+
const numberOfObstaclePositionsCausingALoop = await determineNumberOfObstaclePositionsCausingALoopBruteforce(mapFilePath);
34+
35+
assert.strictEqual(numberOfObstaclePositionsCausingALoop, expectedNumberOfObstaclePositionsCausingALoop);
36+
});
37+
});

0 commit comments

Comments
 (0)