Skip to content

Commit b79e9d2

Browse files
committed
feat: Like a Rogue
1 parent 2e78e71 commit b79e9d2

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
2323
- [Day 12: Leonardo's Monorail](day-12-leonardos-monorail/)
2424
- [Day 15: Timing is Everything](day-15-timing-is-everything/)
2525
- [Day 16: Dragon Checksum](day-16-dragon-checksum/)
26+
- [Day 18: Like a Rogue](day-18-like-a-rogue/)
2627

2728
## Running Tests
2829

day-18-like-a-rogue/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Day 18: Like a Rogue
2+
3+
As you enter this room, you hear a loud click! Some of the tiles in the floor here seem to be pressure plates for [traps](https://nethackwiki.com/wiki/Trap), and the trap you just triggered has run out of... whatever it tried to do to you. You doubt you'll be so lucky next time.
4+
5+
Upon closer examination, the traps and safe tiles in this room seem to follow a pattern. The tiles are arranged into rows that are all the same width; you take note of the safe tiles (`.`) and traps (`^`) in the first row (your puzzle input).
6+
7+
The type of tile (trapped or safe) in each row is based on the types of the tiles in the same position, and to either side of that position, in the previous row. (If either side is off either end of the row, it counts as "safe" because there isn't a trap embedded in the wall.)
8+
9+
For example, suppose you know the first row (with tiles marked by letters) and want to determine the next row (with tiles marked by numbers):
10+
11+
```
12+
ABCDE
13+
12345
14+
```
15+
16+
The type of tile `2` is based on the types of tiles `A`, `B`, and `C`; the type of tile `5` is based on tiles `D`, `E`, and an imaginary "safe" tile. Let's call these three tiles from the previous row the **left**, **center**, and **right** tiles, respectively. Then, a new tile is a **trap** only in one of the following situations:
17+
18+
- Its **left** and **center** tiles are traps, but its **right** tile is not.
19+
- Its **center** and **right** tiles are traps, but its **left** tile is not.
20+
- Only its **left** tile is a trap.
21+
- Only its **right** tile is a trap.
22+
23+
In any other situation, the new tile is safe.
24+
25+
Then, starting with the row `..^^.`, you can determine the next row by applying those rules to each new tile:
26+
27+
- The leftmost character on the next row considers the left (nonexistent, so we assume "safe"), center (the first `.`, which means "safe"), and right (the second `.`, also "safe") tiles on the previous row. Because all of the trap rules require a trap in at least one of the previous three tiles, the first tile on this new row is also safe, `.`.
28+
- The second character on the next row considers its left (`.`), center (`.`), and right (`^`) tiles from the previous row. This matches the fourth rule: only the right tile is a trap. Therefore, the next tile in this new row is a trap, `^`.
29+
- The third character considers `.^^`, which matches the second trap rule: its center and right tiles are traps, but its left tile is not. Therefore, this tile is also a trap, `^`.
30+
- The last two characters in this new row match the first and third rules, respectively, and so they are both also traps, `^`.
31+
32+
After these steps, we now know the next row of tiles in the room: `.^^^^`. Then, we continue on to the next row, using the same rules, and get `^^..^`. After determining two new rows, our map looks like this:
33+
34+
```
35+
..^^.
36+
.^^^^
37+
^^..^
38+
```
39+
40+
Here's a larger example with ten tiles per row and ten rows:
41+
42+
```
43+
.^^.^.^^^^
44+
^^^...^..^
45+
^.^^.^.^^.
46+
..^^...^^^
47+
.^^^^.^^.^
48+
^^..^.^^..
49+
^^^^..^^^.
50+
^..^^^^.^^
51+
.^^^..^.^^
52+
^^.^^^..^^
53+
```
54+
55+
In ten rows, this larger example has `38` safe tiles.
56+
57+
Starting with the map in your puzzle input, in a total of `40` rows (including the starting row), **how many safe tiles** are there?
58+
59+
## References
60+
- https://adventofcode.com/2016/day/18

day-18-like-a-rogue/test.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const assert = require('assert');
2+
3+
const traps = require('./traps');
4+
5+
describe('Day 18: Like a Rogue', () => {
6+
it('should determine number of safe tiles', () => {
7+
const seed = '.^^.^.^^^^';
8+
9+
assert.strictEqual(traps(seed, 10), 38);
10+
});
11+
});

day-18-like-a-rogue/traps.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const getNextRow = (row) => {
2+
const currentRow = `.${row}.`;
3+
const nextRow = [];
4+
5+
for (let i = 1; i < currentRow.length - 1; i++) {
6+
const parts = currentRow.slice(i - 1, i + 2);
7+
const trapRules = ['^^.', '.^^', '^..', '..^'];
8+
9+
nextRow.push(trapRules.includes(parts) ? '^' : '.');
10+
}
11+
12+
return nextRow.join('');
13+
};
14+
15+
module.exports = (seed, iterations = 40) => {
16+
const history = [seed];
17+
18+
for (let i = 0; i < iterations - 1; i++) {
19+
history.push(getNextRow(history[i]));
20+
}
21+
22+
return history
23+
.reduce((a, b) => a + b.split('').filter((x) => x === '.').length, 0);
24+
};

0 commit comments

Comments
 (0)