Skip to content

Commit a29379e

Browse files
committed
feat: Day 22: Mode Maze (part 1)
1 parent 1dd3750 commit a29379e

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
3232
- [Day 16: Chronal Classification](day-16-chronal-classification/)
3333
- [Day 17: Reservoir Research](day-17-reservoir-research/)
3434
- [Day 18: Settlers of The North Pole](day-18-settlers-of-the-north-pole/)
35+
- [Day 22: Mode Maze](day-22-mode-maze/)
3536
- [Day 23: Experimental Emergency Teleportation](day-23-experimental-emergency-teleportation/)
3637
- [Day 25: Four-Dimensional Adventure](day-25-four-dimensional-adventure/)
3738

day-22-mode-maze/README.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
## Day 22: Mode Maze
2+
3+
This is it, your final stop: the year -483. It's snowing and dark outside; the only light you can see is coming from a small cottage in the distance. You make your way there and knock on the door.
4+
5+
A portly man with a large, white beard answers the door and invites you inside. For someone living near the North Pole in -483, he must not get many visitors, but he doesn't act surprised to see you. Instead, he offers you some milk and cookies.
6+
7+
After talking for a while, he asks a favor of you. His friend hasn't come back in a few hours, and he's not sure where he is. Scanning the region briefly, you discover one life signal in a cave system nearby; his friend must have taken shelter there. The man asks if you can go there to retrieve his friend.
8+
9+
The cave is divided into square **regions** which are either dominantly **rocky**, **narrow**, or **wet** (called its **type**). Each region occupies exactly one **coordinate** in `X,Y` format where `X` and `Y` are integers and zero or greater. (Adjacent regions can be the same type.)
10+
11+
The scan (your puzzle input) is not very detailed: it only reveals the **depth** of the cave system and the **coordinates of the target**. However, it does not reveal the type of each region. The mouth of the cave is at `0,0`.
12+
13+
The man explains that due to the unusual geology in the area, there is a method to determine any region's type based on its **erosion level**. The erosion level of a region can be determined from its **geologic index**. The geologic index can be determined using the first rule that applies from the list below:
14+
15+
- The region at `0,0` (the mouth of the cave) has a geologic index of `0`.
16+
- The region at the coordinates of the target has a geologic index of `0`.
17+
- If the region's `Y` coordinate is `0`, the geologic index is its `X` coordinate times `16807`.
18+
- If the region's `X` coordinate is `0`, the geologic index is its `Y` coordinate times `48271`.
19+
- Otherwise, the region's geologic index is the result of multiplying the erosion **levels** of the regions at `X-1,Y` and `X,Y-1`.
20+
21+
A region's **erosion level** is its **geologic index** plus the cave system's **depth**, all [modulo](https://en.wikipedia.org/wiki/Modulo_operation) `20183`. Then:
22+
23+
- If the **erosion level modulo `3`** is `0`, the region's type is **rocky**.
24+
- If the **erosion level modulo `3`** is `1`, the region's type is **wet**.
25+
- If the **erosion level modulo `3`** is `2`, the region's type is **narrow**.
26+
27+
For example, suppose the cave system's depth is `510` and the target's coordinates are `10,10`. Using `%` to represent the modulo operator, the cavern would look as follows:
28+
29+
- At `0,0`, the geologic index is `0`. The erosion level is `(0 + 510) % 20183 = 510`. The type is `510 % 3 = 0`, **rocky**.
30+
- At `1,0`, because the `Y` coordinate is `0`, the geologic index is `1 * 16807 = 16807`. The erosion level is `(16807 + 510) % 20183 = 17317`. The type is `17317 % 3 = 1`, **wet**.
31+
- At `0,1`, because the `X` coordinate is `0`, the geologic index is `1 * 48271 = 48271`. The erosion level is `(48271 + 510) % 20183 = 8415`. The type is `8415 % 3 = 0`, **rocky**.
32+
- At `1,1`, neither coordinate is `0` and it is not the coordinate of the target, so the geologic index is the erosion level of `0,1` (`8415`) times the erosion level of `1,0` (`17317`), `8415 * 17317 = 145722555`. The erosion level is `(145722555 + 510) % 20183 = 1805`. The type is `1805 % 3 = 2`, **narrow**.
33+
- At `10,10`, because they are the target's coordinates, the geologic index is `0`. The erosion level is `(0 + 510) % 20183 = 510`. The type is `510 % 3 = 0`, **rocky**.
34+
35+
Drawing this same cave system with rocky as `.`, wet as `=`, narrow as `|`, the mouth as `M`, the target as `T`, with `0,0` in the top-left corner, `X` increasing to the right, and `Y` increasing downward, the top-left corner of the map looks like this:
36+
37+
```
38+
M=.|=.|.|=.|=|=.
39+
.|=|=|||..|.=...
40+
.==|....||=..|==
41+
=.|....|.==.|==.
42+
=|..==...=.|==..
43+
=||.=.=||=|=..|=
44+
|.=.===|||..=..|
45+
|..==||=.|==|===
46+
.=..===..=|.|||.
47+
.======|||=|=.|=
48+
.===|=|===T===||
49+
=|||...|==..|=.|
50+
=.=|=.=..=.||==|
51+
||=|=...|==.=|==
52+
|=.=||===.|||===
53+
||.|==.|.|.||=||
54+
```
55+
56+
Before you go in, you should determine the **risk level** of the area. For the rectangle that has a top-left corner of region `0,0` and a bottom-right corner of the region containing the target, add up the risk level of each individual region: `0` for rocky regions, `1` for wet regions, and `2` for narrow regions.
57+
58+
In the cave system above, because the mouth is at `0,0` and the target is at `10,10`, adding up the risk level of all regions with an `X` coordinate from `0` to `10` and a `Y` coordinate from `0` to `10`, this total is **`114`**.
59+
60+
**What is the total risk level for the smallest rectangle that includes `0,0` and the target's coordinates?**
61+
62+
## References
63+
- https://adventofcode.com/2018/day/22

day-22-mode-maze/maze.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = (input) => {
2+
const depth = +/depth: (\d+)/.exec(input)[1];
3+
const target = /target: (\d+),(\d+)/.exec(input).slice(1).map((c) => +c);
4+
5+
const erosionLevelMap = Array
6+
.from({ length: target[1] + 1 })
7+
.map(() => Array.from({ length: target[0] + 1 }));
8+
9+
const erosionLevel = (geologicIndex) => (geologicIndex + depth) % 20183;
10+
11+
let riskLevel = 0;
12+
13+
for (let y = 0; y <= target[1]; y++) {
14+
for (let x = 0; x <= target[0]; x++) {
15+
if ((x === 0 && y === 0) || (x === target[0] && y === target[1])) {
16+
erosionLevelMap[y][x] = 0;
17+
} else if (y === 0) {
18+
erosionLevelMap[y][x] = erosionLevel(x * 16807);
19+
} else if (x === 0) {
20+
erosionLevelMap[y][x] = erosionLevel(y * 48271);
21+
} else {
22+
const geologicIndex = erosionLevelMap[y][x - 1] * erosionLevelMap[y - 1][x];
23+
erosionLevelMap[y][x] = erosionLevel(geologicIndex);
24+
}
25+
26+
riskLevel += erosionLevelMap[y][x] % 3;
27+
}
28+
}
29+
30+
return riskLevel;
31+
};

day-22-mode-maze/test.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const assert = require('assert');
2+
3+
const maze = require('./maze');
4+
5+
describe('Day 22: Mode Maze', () => {
6+
it('should calculate total risk level', () => {
7+
const scan =
8+
`depth: 510
9+
target: 10,10`;
10+
11+
assert.strictEqual(maze(scan), 114);
12+
});
13+
});

0 commit comments

Comments
 (0)