Skip to content

Commit

Permalink
feat(2023): day 10 - pipe maze
Browse files Browse the repository at this point in the history
  • Loading branch information
rpidanny committed Dec 10, 2023
1 parent 9b72e05 commit bb0a974
Show file tree
Hide file tree
Showing 14 changed files with 710 additions and 17 deletions.
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@
}
},
"isort.args": ["--profile", "black"],
"cSpell.words": ["nums", "intcode"]
"cSpell.words": [
"downleft",
"downup",
"intcode",
"leftright",
"leftup",
"nums",
"rightup"
]
}
4 changes: 2 additions & 2 deletions 2023/Day03/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { directions } from "../utils/directions";
import { allDirections } from "../utils/directions";

export function isIndexValid(inputs: string[], row: number, col: number) {
return (
Expand Down Expand Up @@ -44,7 +44,7 @@ export function findAdjacentNumbers(
const adjacentNumbers: number[] = [];
const visited = new Set<string>();

for (const [dr, dc] of directions) {
for (const [dr, dc] of Object.values(allDirections)) {
const r = row + dr;
const c = col + dc;

Expand Down
241 changes: 241 additions & 0 deletions 2023/Day10/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
# [--- Day 10: Pipe Maze ---](https://adventofcode.com/2023/day/10)

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.

You wander around for a while, but you don't find any people or animals. However, you do occasionally find signposts labeled "Hot Springs" 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.

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.

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).

The pipes are arranged in a two-dimensional grid of tiles:

- `|` is a vertical pipe connecting north and south.
- `-` is a horizontal pipe connecting east and west.
- `L` is a 90-degree bend connecting north and east.
- `J` is a 90-degree bend connecting north and west.
- `7` is a 90-degree bend connecting south and west.
- `F` is a 90-degree bend connecting south and east.
- `.` is ground; there is no pipe in this tile.
- `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.

Based on the acoustics of the animal's scurrying, you're confident the pipe that contains the animal is one large, continuous loop.

For example, here is a square loop of pipe:

```txt
.....
.F-7.
.|.|.
.L-J.
.....
```

If the animal had entered this loop in the northwest corner, the sketch would instead look like this:

```txt
.....
.S-7.
.|.|.
.L-J.
.....
```

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.

Unfortunately, there are also many pipes that aren't connected to the loop! This sketch shows the same loop as above:

```txt
-L|F7
7S-7|
L|7||
-L-J|
L|-JF
```

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).

Here is a sketch that contains a slightly more complex main loop:

```txt
..F7.
.FJ|.
SJ.L7
|F--J
LJ...
```

Here's the same example sketch with the extra, non-main-loop pipe tiles also shown:

```txt
7-F7-
.FJ|7
SJLL7
|F--J
LJ.LJ
```

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.

In the first example with the square loop:

```txt
.....
.S-7.
.|.|.
.L-J.
.....
```

You can count the distance each tile in the loop is from the starting point like this:

```txt
.....
.012.
.1.3.
.234.
.....
```

In this example, the farthest point from the start is `4` steps away.

Here's the more complex loop again:

```txt
..F7.
.FJ|.
SJ.L7
|F--J
LJ...
```

Here are the distances for each tile on that loop:

```txt
..45.
.236.
01.78
14567
23...
```

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?**

> Your puzzle answer was `6886`.
## --- Part Two ---

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?

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:

```txt
...........
.S-------7.
.|F-----7|.
.||.....||.
.||.....||.
.|L-7.F-J|.
.|..|.|..|.
.L--J.L--J.
...........
```

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:

```txt
...........
.S-------7.
.|F-----7|.
.||OOOOO||.
.||OOOOO||.
.|L-7OF-J|.
.|II|O|II|.
.L--JOL--J.
.....O.....
```

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:

```txt
..........
.S------7.
.|F----7|.
.||OOOO||.
.||OOOO||.
.|L-7F-J|.
.|II||II|.
.L--JL--J.
..........
```

In both of the above examples, `4` tiles are enclosed by the loop.

Here's a larger example:

```txt
.F----7F7F7F7F-7....
.|F--7||||||||FJ....
.||.FJ||||||||L7....
FJL7L7LJLJ||LJ.L-7..
L--J.L7...LJS7F-7L7.
....F-J..F7FJ|L7L7L7
....L7.F7||L7|.L7L7|
.....|FJLJ|FJ|F7|.LJ
....FJL-7.||.||||...
....L---J.LJ.LJLJ...
```

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):

```txt
OF----7F7F7F7F-7OOOO
O|F--7||||||||FJOOOO
O||OFJ||||||||L7OOOO
FJL7L7LJLJ||LJIL-7OO
L--JOL7IIILJS7F-7L7O
OOOOF-JIIF7FJ|L7L7L7
OOOOL7IF7||L7|IL7L7|
OOOOO|FJLJ|FJ|F7|OLJ
OOOOFJL-7O||O||||OOO
OOOOL---JOLJOLJLJOOO
```

In this larger example, `8` tiles are enclosed by the loop.

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:

```txt
FF7FSF7F7F7F7F7F---7
L|LJ||||||||||||F--J
FL-7LJLJ||||||LJL-77
F--JF--7||LJLJ7F7FJ-
L---JF-JLJ.||-FJLJJ7
|F|F-JF---7F7-L7L|7|
|FFJF7L7F-JF7|JL---7
7-L-JL7||F7|L7F-7F7|
L.L7LFJ|||||FJL7||LJ
L7JLJL-JLJLJL--JLJ.L
```

Here are just the tiles that are enclosed by the loop marked with I:

```txt
FF7FSF7F7F7F7F7F---7
L|LJ||||||||||||F--J
FL-7LJLJ||||||LJL-77
F--JF--7||LJLJIF7FJ-
L---JF-JLJIIIIFJLJJ7
|F|F-JF---7IIIL7L|7|
|FFJF7L7F-JF7IIL---7
7-L-JL7||F7|L7F-7F7|
L.L7LFJ|||||FJL7||LJ
L7JLJL-JLJLJL--JLJ.L
```

In this last example, `10` tiles are enclosed by the loop.

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?**

> Your puzzle answer was `371`.
48 changes: 48 additions & 0 deletions 2023/Day10/helpers.back-track.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { DOWN, LEFT, RIGHT, UP } from "../utils/directions";

export const pipeDirections: Record<string, number[][]> = {
F: [DOWN, RIGHT],
J: [LEFT, UP],
L: [RIGHT, UP],
"7": [LEFT, DOWN],
"|": [DOWN, UP],
"-": [RIGHT, LEFT],
S: [DOWN, RIGHT, UP, LEFT],
};

export function isValid(grid: string[], x: number, y: number): boolean {
return (
y >= 0 &&
y < grid.length &&
x >= 0 &&
x < grid[y].length &&
grid[y][x] !== "."
);
}

export function dfs(
grid: string[],
x: number,
y: number,
visited = new Set<string>(),
distance = 0,
): number {
const pipe = grid[y][x];
let maxDistance = distance;

for (const [dx, dy] of pipeDirections[pipe]) {
const newX = x + dx;
const newY = y + dy;

if (isValid(grid, newX, newY) && !visited.has(`${newX},${newY}`)) {
visited.add(`${newX},${newY}`);
maxDistance = Math.max(
maxDistance,
dfs(grid, newX, newY, visited, distance + 1),
);
visited.delete(`${newX},${newY}`);
}
}

return maxDistance;
}
Loading

0 comments on commit bb0a974

Please sign in to comment.