Skip to content

Commit 792e700

Browse files
committed
feat: Day 10: Pipe Maze
1 parent ed549b4 commit 792e700

File tree

6 files changed

+465
-2
lines changed

6 files changed

+465
-2
lines changed

.github/badges/completion.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"schemaVersion": 1,
33
"label": "completion",
4-
"message": "18/50",
4+
"message": "20/50",
55
"color": "yellow"
66
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
2121
- [Day 7: Camel Cards](day-07-camel-cards/)
2222
- [Day 8: Haunted Wasteland](day-08-haunted-wasteland/)
2323
- [Day 9: Mirage Maintenance](day-09-mirage-maintenance/)
24-
- [Day 10: TBD](day-10/)
24+
- [Day 10: Pipe Maze](day-10-pipe-maze/)
2525
- [Day 11: TBD](day-11/)
2626
- [Day 12: TBD](day-12/)
2727
- [Day 13: TBD](day-13/)

day-10-pipe-maze/README.md

+223
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
# Day 10: Pipe Maze
2+
3+
You use the hang glider to ride the hot air from Desert Island all the way up to the floating metal island. This island is surprisingly cold and there definitely aren't any thermals to glide on, so you leave your hang glider behind.
4+
5+
You wander around for a while, but you don't find any people or animals. However, you do occasionally find signposts labeled "[Hot Springs](https://en.wikipedia.org/wiki/Hot_spring)" pointing in a seemingly consistent direction; maybe you can find someone at the hot springs and ask them where the desert-machine parts are made.
6+
7+
The landscape here is alien; even the flowers and trees are made of metal. As you stop to admire some metal grass, you notice something metallic scurry away in your peripheral vision and jump into a big pipe! It didn't look like any animal you've ever seen; if you want a better look, you'll need to get ahead of it.
8+
9+
Scanning the area, you discover that the entire field you're standing on is densely packed with pipes; it was hard to tell at first because they're the same metallic silver color as the "ground". You make a quick sketch of all of the surface pipes you can see (your puzzle input).
10+
11+
The pipes are arranged in a two-dimensional grid of **tiles**:
12+
13+
* `|` is a **vertical pipe** connecting north and south.
14+
* `-` is a **horizontal pipe** connecting east and west.
15+
* `L` is a **90-degree bend** connecting north and east.
16+
* `J` is a **90-degree bend** connecting north and west.
17+
* `7` is a **90-degree bend** connecting south and west.
18+
* `F` is a **90-degree bend** connecting south and east.
19+
* `.` is **ground**; there is no pipe in this tile.
20+
* `S` is the **starting position** of the animal; there is a pipe on this tile, but your sketch doesn't show what shape the pipe has.
21+
22+
Based on the acoustics of the animal's scurrying, you're confident the pipe that contains the animal is **one large, continuous loop**.
23+
24+
For example, here is a square loop of pipe:
25+
26+
.....
27+
.F-7.
28+
.|.|.
29+
.L-J.
30+
.....
31+
32+
33+
If the animal had entered this loop in the northwest corner, the sketch would instead look like this:
34+
35+
.....
36+
.S-7.
37+
.|.|.
38+
.L-J.
39+
.....
40+
41+
42+
In the above diagram, the `S` tile is still a 90-degree `F` bend: you can tell because of how the adjacent pipes connect to it.
43+
44+
Unfortunately, there are also many pipes that **aren't connected to the loop**! This sketch shows the same loop as above:
45+
46+
-L|F7
47+
7S-7|
48+
L|7||
49+
-L-J|
50+
L|-JF
51+
52+
53+
In the above diagram, you can still figure out which pipes form the main loop: they're the ones connected to `S`, pipes those pipes connect to, pipes **those** pipes connect to, and so on. Every pipe in the main loop connects to its two neighbors (including `S`, which will have exactly two pipes connecting to it, and which is assumed to connect back to those two pipes).
54+
55+
Here is a sketch that contains a slightly more complex main loop:
56+
57+
..F7.
58+
.FJ|.
59+
SJ.L7
60+
|F--J
61+
LJ...
62+
63+
64+
Here's the same example sketch with the extra, non-main-loop pipe tiles also shown:
65+
66+
7-F7-
67+
.FJ|7
68+
SJLL7
69+
|F--J
70+
LJ.LJ
71+
72+
73+
If you want to **get out ahead of the animal**, you should find the tile in the loop that is **farthest** from the starting position. Because the animal is in the pipe, it doesn't make sense to measure this by direct distance. Instead, you need to find the tile that would take the longest number of steps **along the loop** to reach from the starting point - regardless of which way around the loop the animal went.
74+
75+
In the first example with the square loop:
76+
77+
.....
78+
.S-7.
79+
.|.|.
80+
.L-J.
81+
.....
82+
83+
84+
You can count the distance each tile in the loop is from the starting point like this:
85+
86+
.....
87+
.012.
88+
.1.3.
89+
.234.
90+
.....
91+
92+
93+
In this example, the farthest point from the start is **`4`** steps away.
94+
95+
Here's the more complex loop again:
96+
97+
..F7.
98+
.FJ|.
99+
SJ.L7
100+
|F--J
101+
LJ...
102+
103+
104+
Here are the distances for each tile on that loop:
105+
106+
..45.
107+
.236.
108+
01.78
109+
14567
110+
23...
111+
112+
113+
Find the single giant loop starting at `S`. **How many steps along the loop does it take to get from the starting position to the point farthest from the starting position?**
114+
115+
## Part Two
116+
117+
You quickly reach the farthest point of the loop, but the animal never emerges. Maybe its nest is **within the area enclosed by the loop**?
118+
119+
To determine whether it's even worth taking the time to search for such a nest, you should calculate how many tiles are contained within the loop. For example:
120+
121+
...........
122+
.S-------7.
123+
.|F-----7|.
124+
.||.....||.
125+
.||.....||.
126+
.|L-7.F-J|.
127+
.|..|.|..|.
128+
.L--J.L--J.
129+
...........
130+
131+
132+
The above loop encloses merely **four tiles** - the two pairs of `.` in the southwest and southeast (marked `I` below). The middle `.` tiles (marked `O` below) are **not** in the loop. Here is the same loop again with those regions marked:
133+
134+
...........
135+
.S-------7.
136+
.|F-----7|.
137+
.||OOOOO||.
138+
.||OOOOO||.
139+
.|L-7OF-J|.
140+
.|II|O|II|.
141+
.L--JOL--J.
142+
.....O.....
143+
144+
145+
In fact, there doesn't even need to be a full tile path to the outside for tiles to count as outside the loop - squeezing between pipes is also allowed! Here, `I` is still within the loop and `O` is still outside the loop:
146+
147+
..........
148+
.S------7.
149+
.|F----7|.
150+
.||OOOO||.
151+
.||OOOO||.
152+
.|L-7F-J|.
153+
.|II||II|.
154+
.L--JL--J.
155+
..........
156+
157+
158+
In both of the above examples, **`4`** tiles are enclosed by the loop.
159+
160+
Here's a larger example:
161+
162+
.F----7F7F7F7F-7....
163+
.|F--7||||||||FJ....
164+
.||.FJ||||||||L7....
165+
FJL7L7LJLJ||LJ.L-7..
166+
L--J.L7...LJS7F-7L7.
167+
....F-J..F7FJ|L7L7L7
168+
....L7.F7||L7|.L7L7|
169+
.....|FJLJ|FJ|F7|.LJ
170+
....FJL-7.||.||||...
171+
....L---J.LJ.LJLJ...
172+
173+
174+
The above sketch has many random bits of ground, some of which are in the loop (`I`) and some of which are outside it (`O`):
175+
176+
OF----7F7F7F7F-7OOOO
177+
O|F--7||||||||FJOOOO
178+
O||OFJ||||||||L7OOOO
179+
FJL7L7LJLJ||LJIL-7OO
180+
L--JOL7IIILJS7F-7L7O
181+
OOOOF-JIIF7FJ|L7L7L7
182+
OOOOL7IF7||L7|IL7L7|
183+
OOOOO|FJLJ|FJ|F7|OLJ
184+
OOOOFJL-7O||O||||OOO
185+
OOOOL---JOLJOLJLJOOO
186+
187+
188+
In this larger example, **`8`** tiles are enclosed by the loop.
189+
190+
Any tile that isn't part of the main loop can count as being enclosed by the loop. Here's another example with many bits of junk pipe lying around that aren't connected to the main loop at all:
191+
192+
FF7FSF7F7F7F7F7F---7
193+
L|LJ||||||||||||F--J
194+
FL-7LJLJ||||||LJL-77
195+
F--JF--7||LJLJ7F7FJ-
196+
L---JF-JLJ.||-FJLJJ7
197+
|F|F-JF---7F7-L7L|7|
198+
|FFJF7L7F-JF7|JL---7
199+
7-L-JL7||F7|L7F-7F7|
200+
L.L7LFJ|||||FJL7||LJ
201+
L7JLJL-JLJLJL--JLJ.L
202+
203+
204+
Here are just the tiles that are **enclosed by the loop** marked with `I`:
205+
206+
FF7FSF7F7F7F7F7F---7
207+
L|LJ||||||||||||F--J
208+
FL-7LJLJ||||||LJL-77
209+
F--JF--7||LJLJIF7FJ-
210+
L---JF-JLJIIIIFJLJJ7
211+
|F|F-JF---7IIIL7L|7|
212+
|FFJF7L7F-JF7IIL---7
213+
7-L-JL7||F7|L7F-7F7|
214+
L.L7LFJ|||||FJL7||LJ
215+
L7JLJL-JLJLJL--JLJ.L
216+
217+
218+
In this last example, **`10`** tiles are enclosed by the loop.
219+
220+
Figure out whether you have time to search for the nest by calculating the area within the loop. **How many tiles are enclosed by the loop?**
221+
222+
## References
223+
- https://adventofcode.com/2023/day/10

day-10-pipe-maze/part1.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module.exports = (input) => {
2+
let start = [0, 0];
3+
4+
const map = input.split('\n').map((line, y) => {
5+
return line.trim().split('').map((pipe, x) => {
6+
if (pipe === 'S') {
7+
start = [x, y];
8+
}
9+
10+
return {
11+
n: ['|', 'L', 'J'].includes(pipe),
12+
e: ['-', 'L', 'F'].includes(pipe),
13+
s: ['|', '7', 'F'].includes(pipe),
14+
w: ['-', 'J', '7'].includes(pipe),
15+
};
16+
});
17+
});
18+
19+
// patch map
20+
const sn = ((map[start[1] - 1] || [])[start[0]] || { s: false }).s;
21+
const se = (map[start[1]][start[0] + 1] || { w: false }).w;
22+
const ss = ((map[start[1] + 1] || [])[start[0]] || { n: false }).n;
23+
const sw = (map[start[1]][start[0] - 1] || { e: false }).e;
24+
25+
if (sn && ss) {
26+
map[start[1]][start[0]] = { n: true, e: false, s: true, w: false };
27+
} else if (sw && se) {
28+
map[start[1]][start[0]] = { n: false, e: true, s: false, w: true };
29+
} else if (sn && se) {
30+
map[start[1]][start[0]] = { n: true, e: true, s: false, w: false };
31+
} else if (sn && sw) {
32+
map[start[1]][start[0]] = { n: true, e: false, s: false, w: true };
33+
} else if (ss && sw) {
34+
map[start[1]][start[0]] = { n: false, e: false, s: true, w: true };
35+
} else if (ss && se) {
36+
map[start[1]][start[0]] = { n: false, e: true, s: true, w: false };
37+
}
38+
39+
let steps = 0;
40+
let vector = sn ? [0, -1] : se ? [1, 0] : ss ? [0, 1] : sw ? [-1, 0] : undefined;
41+
let position = [start[0], start[1]];
42+
43+
do {
44+
const [vx, vy] = vector;
45+
const [x2, y2] = [position[0] + vector[0], position[1] + vector[1]];
46+
const { n, e, s, w } = map[y2][x2];
47+
48+
vector = vx === -1 ? (n ? [0, -1] : w ? [-1, 0] : [0, 1]) :
49+
vx === 1 ? (n ? [0, -1] : e ? [1, 0] : [0, 1]) :
50+
vy === -1 ? (n ? [0, -1] : e ? [1, 0] : [-1, 0]) :
51+
vy === 1 ? (s ? [0, 1] : e ? [1, 0] : [-1, 0]) : undefined;
52+
53+
position = [x2, y2];
54+
steps++;
55+
} while (position[0] !== start[0] || position[1] !== start[1]);
56+
57+
return Math.ceil(steps / 2);
58+
};

day-10-pipe-maze/part2.js

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// adapted from: https://www.algorithms-and-technologies.com/point_in_polygon/javascript
2+
const pointInPolygon = function (polygon, point) {
3+
let odd = false;
4+
5+
// for each edge (in this case for each point of the polygon and the previous one)
6+
for (let i = 0, j = polygon.length - 1; i < polygon.length; i++) {
7+
// if a line from the point into infinity crosses this edge
8+
if (((polygon[i][1] > point[1]) !== (polygon[j][1] > point[1])) // one point needs to be above, one below our y coordinate
9+
// ...and the edge doesn't cross our x corrdinate before our x coordinate (but between our x coordinate and infinity)
10+
&& (point[0] < ((polygon[j][0] - polygon[i][0]) * (point[1] - polygon[i][1]) / (polygon[j][1] - polygon[i][1]) + polygon[i][0]))) {
11+
odd = !odd;
12+
}
13+
14+
j = i;
15+
}
16+
17+
return odd;
18+
};
19+
20+
module.exports = (input) => {
21+
let start = [0, 0];
22+
23+
const map = input.split('\n').map((line, y) => {
24+
return line.trim().split('').map((pipe, x) => {
25+
if (pipe === 'S') {
26+
start = [x, y];
27+
}
28+
29+
return {
30+
n: ['|', 'L', 'J'].includes(pipe),
31+
e: ['-', 'L', 'F'].includes(pipe),
32+
s: ['|', '7', 'F'].includes(pipe),
33+
w: ['-', 'J', '7'].includes(pipe),
34+
};
35+
});
36+
});
37+
38+
// patch map
39+
const sn = ((map[start[1] - 1] || [])[start[0]] || { s: false }).s;
40+
const se = (map[start[1]][start[0] + 1] || { w: false }).w;
41+
const ss = ((map[start[1] + 1] || [])[start[0]] || { n: false }).n;
42+
const sw = (map[start[1]][start[0] - 1] || { e: false }).e;
43+
44+
if (sn && ss) {
45+
map[start[1]][start[0]] = { n: true, e: false, s: true, w: false };
46+
} else if (sw && se) {
47+
map[start[1]][start[0]] = { n: false, e: true, s: false, w: true };
48+
} else if (sn && se) {
49+
map[start[1]][start[0]] = { n: true, e: true, s: false, w: false };
50+
} else if (sn && sw) {
51+
map[start[1]][start[0]] = { n: true, e: false, s: false, w: true };
52+
} else if (ss && sw) {
53+
map[start[1]][start[0]] = { n: false, e: false, s: true, w: true };
54+
} else if (ss && se) {
55+
map[start[1]][start[0]] = { n: false, e: true, s: true, w: false };
56+
}
57+
58+
let vector = sn ? [0, -1] : se ? [1, 0] : ss ? [0, 1] : sw ? [-1, 0] : undefined;
59+
let position = [start[0], start[1]];
60+
let positions = new Set();
61+
let vertices = [];
62+
63+
do {
64+
const [vx, vy] = vector;
65+
const [x2, y2] = [position[0] + vector[0], position[1] + vector[1]];
66+
const { n, e, s, w } = map[y2][x2];
67+
68+
vector = vx === -1 ? (n ? [0, -1] : w ? [-1, 0] : [0, 1]) :
69+
vx === 1 ? (n ? [0, -1] : e ? [1, 0] : [0, 1]) :
70+
vy === -1 ? (n ? [0, -1] : e ? [1, 0] : [-1, 0]) :
71+
vy === 1 ? (s ? [0, 1] : e ? [1, 0] : [-1, 0]) : undefined;
72+
73+
if (vector[0] !== vx && vector[1] !== vy) {
74+
vertices.push([x2, y2]);
75+
}
76+
77+
position = [x2, y2];
78+
positions.add(`${position[0]},${position[1]}`);
79+
} while (position[0] !== start[0] || position[1] !== start[1]);
80+
81+
let inside = 0;
82+
83+
for (let y = 0; y < map.length; y++) {
84+
for (let x = 0; x < map[y].length; x++) {
85+
inside += !positions.has(`${x},${y}`) && pointInPolygon(vertices, [x, y]) ? 1 : 0;
86+
}
87+
}
88+
89+
return inside;
90+
};

0 commit comments

Comments
 (0)