Skip to content

Commit b2fbcf5

Browse files
authored
Merge pull request #58 from romellem/2016/day-2
2016 - Day 2
2 parents ff2293f + df77491 commit b2fbcf5

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

2016/2/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Answers
2+
3+
| Part 1 | Part 2 |
4+
|---------|---------|
5+
| `12578` | `516DD` |
6+
7+
## --- Day 2: Bathroom Security ---
8+
9+
You arrive at _Easter Bunny Headquarters_ under cover of darkness. However, you left in such a rush that you forgot to use the bathroom! Fancy office buildings like this one usually have keypad locks on their bathrooms, so you search the front desk for the code.
10+
11+
"In order to improve security," the document you find says, "bathroom codes will no longer be written down. Instead, please memorize and follow the procedure below to access the bathrooms."
12+
13+
The document goes on to explain that each button to be pressed can be found by starting on the previous button and moving to adjacent buttons on the keypad: `U` moves up, `D` moves down, `L` moves left, and `R` moves right. Each line of instructions corresponds to one button, starting at the previous button (or, for the first line, _the "5" button_); press whatever button you're on at the end of each line. If a move doesn't lead to a button, ignore it.
14+
15+
You can't hold it much longer, so you decide to figure out the code as you walk to the bathroom. You picture a keypad like this:
16+
17+
1 2 3
18+
4 5 6
19+
7 8 9
20+
21+
22+
Suppose your instructions are:
23+
24+
ULL
25+
RRDDD
26+
LURDL
27+
UUUUD
28+
29+
30+
* You start at "5" and move up (to "2"), left (to "1"), and left (you can't, and stay on "1"), so the first button is `1`.
31+
* Starting from the previous button ("1"), you move right twice (to "3") and then down three times (stopping at "9" after two moves and ignoring the third), ending up with `9`.
32+
* Continuing from "9", you move left, up, right, down, and left, ending with `8`.
33+
* Finally, you move up four times (stopping at "2"), then down once, ending with `5`.
34+
35+
So, in this example, the bathroom code is `1985`.
36+
37+
Your puzzle input is the instructions from the document you found at the front desk. What is the _bathroom code_?
38+
39+
-----------------
40+
41+
## --- Part Two ---
42+
43+
You finally arrive at the bathroom (it's a several minute walk from the lobby so visitors can behold the many fancy conference rooms and water coolers on this floor) and go to punch in the code. Much to your bladder's dismay, the keypad is not at all like you imagined it. Instead, you are confronted with the result of hundreds of man-hours of bathroom-keypad-design meetings:
44+
45+
1
46+
2 3 4
47+
5 6 7 8 9
48+
A B C
49+
D
50+
51+
52+
You still start at "5" and stop when you're at an edge, but given the same instructions as above, the outcome is very different:
53+
54+
* You start at "5" and don't move at all (up and left are both edges), ending at `5`.
55+
* Continuing from "5", you move right twice and down three times (through "6", "7", "B", "D", "D"), ending at `D`.
56+
* Then, from "D", you move five more times (through "D", "B", "C", "C", "B"), ending at `B`.
57+
* Finally, after five more moves, you end at `3`.
58+
59+
So, given the actual keypad layout, the code would be `5DB3`.
60+
61+
Using the same instructions in your puzzle input, what is the correct _bathroom code_?

2016/2/input.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2016/2/input.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RDRRDLRRUDRUUUULDDRDUULLDUULDURDDUDRULDLUDDRLRDUDDURRRRURDURLLRDRUUULDLLLURDRLLULLUULULLLDLLLRRURRLRDUULRURRUDRRDRLURLRURLLULRUURRUURDDLDRDLDLLUDUULLLUUUUDULLDRRUURLDURDDDDDRLLRRURDLUDRRUUDLRRLLRDURDUDDDLRDDRDLRULLUULRULRLLULDDRURUUDLDDULDRLLURDDUDDUDRDUDLDRRRDURRLDRDRLDLLDUDDDULULRRULRLLURDRRDDUUUUUULRUDLRRDURDLRDLUDLDURUDDUUURUDLUUULDLRDURDLDUUDLDDDURUUDUUDRLRDULLDUULUDRUDRLRRRDLLDRUDULRRUDDURLDRURRLLRRRDRLLDLULULRRUURRURLLUDRRLRULURLDDDDDURUDRRRRULLUUDLDDLUUL
2+
ULURUDLULDULDLLDDLLLDRRLLUDRRDRDUDURUDLRRRRUDRDDURLRRULLDLURLDULLUDDLUDURDUURRRRLDLRLDDULLRURLULLDDRUDLRRRLDRRRDLDRLLDDRRDDLUUDRUDDLULRURLDURRLLDLRUDLLRRUULUDRLLLRLDULURRRRRDDUURDRRUUDULRUULDDULRLUDLUDDULLRLRDDLRLLURRRULDLRRRUURRLDDRDLRDLRRDRDLDRDUDRDURUUDRLRRULRDLLDLLRRRDRLDRLRLRLLLURURDULUUDDRLLDDDRURRURLRDDULLRURUDRRDRLRRRLDLRLRULDRLUURRUUULRLDLLURLLLDLLLDRRDULRURRRRLUDLLRRUDLRUDRURDRRDLUUURRDLRLRUUUDURDLUDURRUUDURLUDDDULLDRDLLDDDRLDDDRLDLDDDDUDUUDURUUDULRDDLULDRDRLURLUDRDLUULLULRLULRDDRULDUDDURUURULUDLUURLURU
3+
URLURDDRLLURRRLDLDLUDUURDRUDLLLRRDLUUULRRLURRRLUDUDLRLDDRUDLRRRULUDUDLLLULULLLRUDULDDDLLLRRRLRURDULUDDDULDLULURRRDLURDLRLLDUDRUDURDRRURULDRDUDLLRDDDUDDURLUULLULRDRRLDDLDURLRRRLDRDLDULRULRRRLRLLDULRDLURLRUUDURRUUDLLUDRUDLRLDUUDLURRRDDUUDUDRLDLDDRURDDLLDDRDLRLRDLLLUDLUUDRLRLRDDRDLRDLLUULLLLUULLDLLDLRDLRLRRLUUDLLRLRUDRURULRLRLULUDRLLUDDUDDULRLDDRUUUURULDRRULLLDUURULUDRLLURLRRLDLRRLDDRRRDUDDUDLDDLULUDDUURDLLLRLDLRDRUUUUUDDDLDRDDRRRLRURRRRDURDRURUDLURRURDRLRUUDDLDRRULLDURDRLRRDURURUULRDUDLDRDDLULULRDRRUDDRLLRLULRLLUUDRDUUDDUDLRUUDLLUULLRUULUDDLURRRLLDRLRRLLRULLDUULURLLLLUUULDR
4+
LDUURULLRLDRRUUDUUUURUUUDDDURRDDLRDLLRDDRULDDUURUDDURLLUDRDUDRDULDRRRLULUDRULLLLDRLLDRDLDLLRURULUDDDDURDDDRLLUDLDRULRDLDUDDDUUDLLRLLLDLRLRLRRUDDULDDDUDLDDLUDDULDLLLLULLLLDDURDDURRRDDRLRLLUDLLUDDDUDURUDRLRDRULULDDRULDLULULLRUULRLDULUURRDRDRRDLDDDRRLUULDLUDRDDUDLRURULLDDURLDDRULUDLDDDRDRLLRDLLUURRRURDRLRURLDDLURDRURDDURLLRLRUDUUDLDUDURRDDURDRDDUDDDUDUURURDDLLRUUDDRRDULDDLLDLRULUULRUUDLLDRLULDULDDUDLULRULDRLLDUULDDDLRLLRLULDDULDDRRRLDRRLURULRDDRDLRRDUDDRDRRRRUDUDLLRRDRRURRUURDRULDDUDURLUDDURDUDRDUULLDRURUURURDRRLDDLDLUURLULRUDURDRUUURRURRDRUDRUURDURLRULULLLULDLLDLRRLDRDLUULUDDDLRDRLRLDRUDUDRLLRL
5+
LURLUURLURDUUDRUDLDLLURRRDLDRRRULDDRRDRDUUDRUDURDDDURLUDDLULUULRRLLRULUDRDDRRRLDURDUDDURDDDLRLDDLULLDRDDLUUDUURRRLULRUURRRRLLULDUDRDUURRRURRDRDUDUDLUDULLDLDDRLUDRURDULURLURRLLURLLLRLUURLRUDLUDDRLURRUULULRLURRURUDURDLDLDDUDDRDLLRLLRRULDDRUDURUDDDUDLLRDLRUDULLLRRRUURUDUUULLRDUDRURUDULLDLLUUUDRULRLLRRDDDDUDULDRDRLLDDLLDDDURRUDURLDLRDRUURDDURLRDRURLRRLLRLULDRRLRUDURDUURRLUUULUDDDLRLULRDRLLURRRDLURDUUDRRRLUURRLLUDLUDLUULLRRDLLRDDRURRUURLDDLRLRLRUDLDLRLRDRRDLLLRDLRDUDLLDDDRD

2016/2/keypad.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// prettier-ignore
2+
const SQUARE = [
3+
[1, 2, 3],
4+
[4, 5, 6],
5+
[7, 8, 9],
6+
];
7+
8+
const A = 'A';
9+
const B = 'B';
10+
const C = 'C';
11+
const D = 'D';
12+
13+
// prettier-ignore
14+
const DIAMOND = [
15+
[ , , 1, , ],
16+
[ , 2, 3, 4, ],
17+
[5, 6, 7, 8, 9],
18+
[ , A, B, C, ],
19+
[ , , D, , ],
20+
];
21+
22+
class Keypad {
23+
constructor({ type } = {}) {
24+
this.grid =
25+
type === 'diamond'
26+
? JSON.parse(JSON.stringify(DIAMOND))
27+
: JSON.parse(JSON.stringify(SQUARE));
28+
}
29+
30+
press(instructions, starting_point = [1, 1]) {
31+
let [x, y] = starting_point;
32+
let steps = instructions.split('');
33+
34+
steps.forEach(step => {
35+
switch (step) {
36+
case 'U':
37+
if (this.grid[y - 1] && this.grid[y - 1][x]) y--;
38+
break;
39+
40+
case 'D':
41+
if (this.grid[y + 1] && this.grid[y + 1][x]) y++;
42+
break;
43+
44+
case 'L':
45+
if (this.grid[y] && this.grid[y][x - 1]) x--;
46+
break;
47+
48+
case 'R':
49+
if (this.grid[y] && this.grid[y][x + 1]) x++;
50+
break;
51+
}
52+
});
53+
54+
return {
55+
x,
56+
y,
57+
num: this.grid[y][x],
58+
};
59+
}
60+
61+
pressAll(instructions, initial_starting_point = [1, 1]) {
62+
let code = [];
63+
let starting_point = initial_starting_point.slice(0);
64+
instructions.forEach(instruction => {
65+
let answer = this.press(instruction, starting_point);
66+
starting_point = [answer.x, answer.y];
67+
code.push(answer.num);
68+
});
69+
70+
return code.join('');
71+
}
72+
}
73+
74+
module.exports = Keypad;

2016/2/part-one.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { input, sampleInput } = require('./input');
2+
const Keypad = require('./keypad');
3+
4+
let keypad = new Keypad();
5+
console.log(keypad.pressAll(input));
6+

2016/2/part-two.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { input, sampleInput } = require('./input');
2+
const Keypad = require('./keypad');
3+
4+
let keypad = new Keypad({ type: 'diamond' });
5+
6+
// Still start on the 5, which is on the left corner
7+
const DIAMOND_STARTING_POINT = [0, 2];
8+
9+
console.log(keypad.pressAll(input, DIAMOND_STARTING_POINT));
10+

0 commit comments

Comments
 (0)