Skip to content

Commit e32b010

Browse files
committed
Added aoc 2017 solns - 4 days
1 parent dbdd57e commit e32b010

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+4018
-2
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "run",
6+
"type": "shell",
7+
"command": "node --use_strict '${file}'",
8+
"group": {
9+
"kind": "build",
10+
"isDefault": true
11+
}
12+
}
13+
]
14+
}

advent-of-code/2016/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Advent of Code 2016: Solutions in JS
2+
3+
## Story
4+
5+
Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you to retrieve all *fifty stars* by December 25th.
6+
7+
Collect stars by solving puzzles. Two puzzles will be made available on each day in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants *one star*. Good luck!
8+
9+
## Days
10+
11+
- [Day 1: No Time for a Taxicab](day-01/)
12+
- [Day 2: Bathroom Security](day-02/)
13+
- [Day 3: Squares With Three Sides](day-03/)
14+
- [Day 4: Security Through Obscurity](day-04/)

advent-of-code/2016/day-01/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Day 1: No Time for a Taxicab
2+
3+
You're airdropped near **Easter Bunny Headquarters** in a city somewhere. "Near", unfortunately, is as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody had time to work them out further.
4+
5+
The Document indicates that you should start at the given coordinates (where you just landed) and face North. Then, follow the provided sequence: either turn left (`L`) or right (`R`) 90 degrees, then walk forward the given number of blocks, ending at a new intersection.
6+
7+
There's no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the destination. Given that you can only walk on the [street grid of the city](https://en.wikipedia.org/wiki/Taxicab_geometry), how far is the shortest path to the destination?
8+
9+
For example:
10+
11+
- Following `R2, L3` leaves you `2` blocks East and `3` blocks North, or `5` blocks away.
12+
- `R2, R2, R2` leaves you `2` blocks due South of your starting position, which is `2` blocks away.
13+
- `R5, L5, R5, R3` leaves you `12` blocks away.
14+
15+
**How many blocks away** is Easter Bunny HQ?
16+
17+
## Part Two
18+
19+
Then, you notice the instructions continue on the back of the Recruiting Document. Easter Bunny HQ is actually at the first location you visit twice.
20+
21+
For example, if your instructions are `R8, R4, R4, R8`, the first location you visit twice is `4` blocks away, due East.
22+
23+
How many blocks away is the **first location you visit twice**?

advent-of-code/2016/day-01/input.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
L2, L5, L5, R5, L2, L4, R1, R1, L4, R2, R1, L1, L4, R1, L4, L4, R5, R3, R1, L1, R1, L5, L1, R5, L4, R2, L5, L3, L3, R3, L3, R4, R4, L2, L5, R1, R2, L2, L1, R3, R4, L193, R3, L5, R45, L1, R4, R79, L5, L5, R5, R1, L4, R3, R3, L4, R185, L5, L3, L1, R5, L2, R1, R3, R2, L3, L4, L2, R2, L3, L2, L2, L3, L5, R3, R4, L5, R1, R2, L2, R4, R3, L4, L3, L1, R3, R2, R1, R1, L3, R4, L5, R2, R1, R3, L3, L2, L2, R2, R1, R2, R3, L3, L3, R4, L4, R4, R4, R4, L3, L1, L2, R5, R2, R2, R2, L4, L3, L4, R4, L5, L4, R2, L4, L4, R4, R1, R5, L2, L4, L5, L3, L2, L4, L4, R3, L3, L4, R1, L2, R3, L2, R1, R2, R5, L4, L2, L1, L3, R2, R3, L2, L1, L5, L2, L1, R4

advent-of-code/2016/day-01/partOne.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const fs = require('fs');
2+
const instructions = fs
3+
.readFileSync(`${__dirname}/input.txt`)
4+
.toString()
5+
.split(', ')
6+
.map(str => {
7+
return { direction: str[0], blocks: parseInt(str.substring(1)) };
8+
});
9+
10+
const DIRECTIONS = {
11+
N: {
12+
L: 'W',
13+
R: 'E'
14+
},
15+
E: {
16+
L: 'N',
17+
R: 'S'
18+
},
19+
S: {
20+
L: 'E',
21+
R: 'W'
22+
},
23+
W: {
24+
L: 'S',
25+
R: 'N'
26+
}
27+
};
28+
29+
const result = instructions.reduce(
30+
(acc, instruction) => {
31+
acc.pointing = DIRECTIONS[acc.pointing][instruction.direction];
32+
33+
switch (acc.pointing) {
34+
case 'N':
35+
acc.v += instruction.blocks;
36+
break;
37+
case 'S':
38+
acc.v -= instruction.blocks;
39+
break;
40+
case 'E':
41+
acc.h += instruction.blocks;
42+
break;
43+
case 'W':
44+
acc.h -= instruction.blocks;
45+
break;
46+
}
47+
48+
return acc;
49+
},
50+
{ pointing: 'N', h: 0, v: 0 }
51+
);
52+
53+
console.log(Math.abs(result.v) + Math.abs(result.h));

advent-of-code/2016/day-01/partTwo.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const fs = require('fs');
2+
const instructions = fs
3+
.readFileSync(`${__dirname}/input.txt`)
4+
.toString()
5+
.split(', ')
6+
.map(str => {
7+
return { direction: str[0], blocks: parseInt(str.substring(1)) };
8+
});
9+
10+
const DIRECTIONS = {
11+
N: {
12+
L: 'W',
13+
R: 'E'
14+
},
15+
E: {
16+
L: 'N',
17+
R: 'S'
18+
},
19+
S: {
20+
L: 'E',
21+
R: 'W'
22+
},
23+
W: {
24+
L: 'S',
25+
R: 'N'
26+
}
27+
};
28+
29+
// Using destructuring assigment:
30+
const { visits } = instructions.reduce(
31+
(state, instruction) => {
32+
state.pointing = DIRECTIONS[state.pointing][instruction.direction];
33+
34+
switch (state.pointing) {
35+
case 'N':
36+
const newV_N = state.v + instruction.blocks;
37+
for (let i = state.v + 1; i <= newV_N; i++) {
38+
state.visits.push(`${state.h},${i}`);
39+
}
40+
state.v = newV_N;
41+
break;
42+
case 'S':
43+
const newV_S = state.v - instruction.blocks;
44+
for (let i = state.v - 1; i >= newV_S; i--) {
45+
state.visits.push(`${state.h},${i}`);
46+
}
47+
state.v = newV_S;
48+
break;
49+
case 'E':
50+
const newH_E = state.h + instruction.blocks;
51+
for (let i = state.h + 1; i <= newH_E; i++) {
52+
state.visits.push(`${i},${state.v}`);
53+
}
54+
state.h = newH_E;
55+
break;
56+
case 'W':
57+
const newH_W = state.h - instruction.blocks;
58+
for (let i = state.h - 1; i >= newH_W; i--) {
59+
state.visits.push(`${i},${state.v}`);
60+
}
61+
state.h = newH_W;
62+
break;
63+
}
64+
65+
return state;
66+
},
67+
{ pointing: 'N', h: 0, v: 0, visits: ['0,0'] }
68+
);
69+
70+
const firstDup = visits
71+
.find((e, i) => visits.lastIndexOf(e) !== i)
72+
.split(',')
73+
.map(Number);
74+
75+
console.log(Math.abs(firstDup[0]) + Math.abs(firstDup[1]));

advent-of-code/2016/day-02/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Day 2: Bathroom Security
2+
3+
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.
4+
5+
"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."
6+
7+
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.
8+
9+
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:
10+
11+
```
12+
1 2 3
13+
4 5 6
14+
7 8 9
15+
```
16+
17+
Suppose your instructions are:
18+
19+
```
20+
ULL
21+
RRDDD
22+
LURDL
23+
UUUUD
24+
```
25+
26+
- 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`.
27+
- 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`.
28+
- Continuing from "9", you move left, up, right, down, and left, ending with `8`.
29+
- Finally, you move up four times (stopping at "2"), then down once, ending with `5`.
30+
31+
So, in this example, the bathroom code is `1985`.
32+
33+
Your puzzle input is the instructions from the document you found at the front desk. What is the **bathroom code**?
34+
35+
## Part Two
36+
37+
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:
38+
39+
```
40+
1
41+
2 3 4
42+
5 6 7 8 9
43+
A B C
44+
D
45+
```
46+
47+
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:
48+
49+
- You start at "5" and don't move at all (up and left are both edges), ending at `5`.
50+
- Continuing from "5", you move right twice and down three times (through "6", "7", "B", "D", "D"), ending at `D`.
51+
- Then, from "D", you move five more times (through "D", "B", "C", "C", "B"), ending at `B`.
52+
- Finally, after five more moves, you end at `3`.
53+
54+
So, given the actual keypad layout, the code would be `5DB3`.
55+
56+
Using the same instructions in your puzzle input, what is the correct **bathroom code**?

advent-of-code/2016/day-02/input.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DLUUULUDLRDDLLLUDULLULLRUURURLUULDUUUDLDDRUDLUULLRLDDURURDDRDRDLDURRURDLDUURULDDULDRDDLDLDLRDRUURLDLUDDDURULRLLLLRLULLUDRDLDUURDURULULULRLULLLULURLRDRDDDDDDDLRLULUULLULURLLDLRLUDULLDLLURUDDLDULDLULDDRLRLRDDLRURLLLURRLDURRDLLUUUUDRURUULRLDRRULLRUDLDRLUDRDRDRRDDURURRDRDRUDURDLUDRUDLRRULDLRDDRURDDUUDLDRDULDDRRURLLULRDRURLRLDLLLUULUUDLUDLDRRRRDUURULDUDUDRLDLLULLLRDDDDDLRDDLLUULLRRRDURLRURDURURLUDRRLRURDRDRRRRULUDLDRDULULRUDULLLUDRRLRLURDDURULDUUDULLURUULRDRDULRUUUDURURDDRRUDURRLRDRULRUUU
2+
LDRURRUUUULDRDDDLLULDRUDDRLLDLDRDLRUDDDLDDULULULLRULDUDRRDLRUURURDRURURDLLRUURDUUDRLDURDRDLRRURURDUUUURUURRLLLDRDUURRRRURULUUUDLUDDRUURRLDULRDULRRRRUDURRLURULRURRDRDLLDRRDUDRDURLDDRURULDRURUDDURDLLLUURRLDRULLURDRDRLDRRURRLRRRDDDDLUDLUDLLDURDURRDUDDLUDLRULRRRDRDDLUDRDURDRDDUURDULRRULDLDLLUDRDDUDUULUDURDRLDURLRRDLDDLURUDRLDUURLLRLUDLLRLDDUDLLLRRRLDLUULLUDRUUDRLDUUUDUURLRDDDDRRDRLDDRDLUDRULDDDRDUULLUUUUULDULRLLLRLLDULRDUDDRDDLRRLRDDULLDURRRURDDUDUDDRLURRLUUUULLDRDULUUDRDULDLLUDLURDLLURRDLUULURRULRLURRRRRUURDDURLRLLDDLRRDUUURDRDUDRDDDLLDDRDRRRLURRDUULULULULRRURDDLDDLLLRUDDDDDDLLLRDULURULLRLRDRR
3+
DDRLLLDLRRURRDLDDRUURRURRLRRRRUURUURDLURRRDDLRUDRURLUURLLRRLRLURLURURDULLLLDLRURULUUDURRLULRDRDRRDDLLULRLUDLUUUDRLLRRURRLDULDDLRRLUUUUDDLRLDRLRRDRDLDDURDDRDDLDLURLRRRDDUDLLRLRLURRRRULLULLLLDRLDULDLLDULRLDRDLDDRRDDDDRUDRLLURULRLDDLLRRURURDDRLLLULLULDDRDLDDDLRLLDRLDRUURRULURDDRLULLDUURRULURUUDULLRUDDRRLLDLLRDRUDDDDLLLDDDLLUUUULLDUUURULRUUDUUUDDLDURLDRDRRLLUDULDLUDRLLLDRRRULUUDDURUDRLUDDRRLLDUDUURDDRURLUURDURURURRUUDUDDLLLDRRRURURRURDLRULLDUDRLRLLRUDRUDLR
4+
RRRDRLRURLRRLUURDRLDUURURLRDRRUDLLUUDURULLUURDLLDRRLURRUDUUDRRURLRRDULLDDLRRRUDUUDUUDLDDDLUUDLDULDDULLDUUUUDDUUDUDULLDDURRDLRRUDUDLRDUULDULRURRRLDLLURUDLDDDRRLRDURDLRRLLLRUDLUDRLLLRLLRRURUDLUDURLDRLRUDLRUULDRULLRLDRDRRLDDDURRRUDDDUDRRDRLDDRDRLLRLLRDLRDUDURURRLLULRDRLRDDRUULRDDRLULDLULURDLRUDRRDDDLDULULRDDRUDRLRDDRLDRDDRRRDUURDRLLDDUULRLLLULLDRDUDRRLUUURLDULUUURULLRLUDLDDLRRDLLRDDLRDRUUDURDDLLLDUUULUUDLULDUDULDRLRUDDURLDDRRRDLURRLLRRRUDDLDDRURDUULRUURDRRURURRRUUDUDULUDLUDLLLUUUULRLLRRRRDUDRRDRUDURLUDDLDRDLDDRULLRRULDURUL
5+
DLLLRDDURDULRRLULURRDULDLUDLURDDURRLLRRLLULRDLDRDULRLLRDRUUULURRRLLRLDDDRDRRULDRRLLLLDLUULRRRURDDRULLULDDDLULRLRRRUDRURULUDDRULDUDRLDRRLURULRUULLLRUURDURLLULUURUULUUDLUDLRRULLLRRLRURDRRURDRULRURRUDUDDDRDDULDLURUDRDURLDLDLUDURLLRUULLURLDDDURDULRLUUUDLLRRLLUURRDUUDUUDUURURDRRRRRRRRRUDULDLULURUDUURDDULDUDDRDDRDRLRUUUUDLDLRDUURRLRUUDDDDURLRRULURDUUDLUUDUUURUUDRURDRDDDDULRLLRURLRLRDDLRUULLULULRRURURDDUULRDRRDRDLRDRRLDUDDULLDRUDDRRRD

advent-of-code/2016/day-02/partOne.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const fs = require('fs');
2+
const instructions = fs
3+
.readFileSync(`${__dirname}/input.txt`)
4+
.toString()
5+
.split(`\n`)
6+
.map(line => line.split(''));
7+
8+
const DIALS = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
9+
const MAX_INDEX = DIALS.length - 1;
10+
11+
const pos = { x: 1, y: 1 }; // We start at dial 5.
12+
let code = '';
13+
14+
instructions.forEach(line => {
15+
line.forEach(instruction => {
16+
switch (instruction) {
17+
case 'U':
18+
pos.y = pos.y === 0 ? 0 : pos.y - 1;
19+
break;
20+
case 'D':
21+
pos.y = pos.y === MAX_INDEX ? MAX_INDEX : pos.y + 1;
22+
break;
23+
case 'L':
24+
pos.x = pos.x === 0 ? 0 : pos.x - 1;
25+
break;
26+
case 'R':
27+
pos.x = pos.x === MAX_INDEX ? MAX_INDEX : pos.x + 1;
28+
break;
29+
}
30+
});
31+
32+
code += DIALS[pos.y][pos.x];
33+
});
34+
35+
console.log(code);

advent-of-code/2016/day-02/partTwo.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const fs = require('fs');
2+
const instructions = fs
3+
.readFileSync(`${__dirname}/input.txt`)
4+
.toString()
5+
.split(`\n`)
6+
.map(line => line.split(''));
7+
8+
const DIALS = [
9+
[null, null, 1, null, null],
10+
[null, 2, 3, 4, null],
11+
[5, 6, 7, 8, 9],
12+
[null, 'A', 'B', 'C', null],
13+
[null, null, 'D', null, null]
14+
];
15+
const MAX_INDEX = DIALS.length - 1;
16+
17+
const pos = { x: 0, y: 2 };
18+
let code = '';
19+
20+
instructions.forEach(line => {
21+
line.forEach(instruction => {
22+
switch (instruction) {
23+
case 'U':
24+
if (pos.y > 0 && DIALS[pos.y - 1][pos.x]) {
25+
pos.y--;
26+
}
27+
break;
28+
case 'D':
29+
if (pos.y < MAX_INDEX && DIALS[pos.y + 1][pos.x]) {
30+
pos.y++;
31+
}
32+
break;
33+
case 'L':
34+
if (pos.x > 0 && DIALS[pos.y][pos.x - 1]) {
35+
pos.x--;
36+
}
37+
break;
38+
case 'R':
39+
if (pos.x < MAX_INDEX && DIALS[pos.y][pos.x + 1]) {
40+
pos.x++;
41+
}
42+
break;
43+
}
44+
});
45+
46+
code += DIALS[pos.y][pos.x];
47+
});
48+
49+
console.log(code);

advent-of-code/2016/day-03/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Day 3: Squares With Three Sides
2+
3+
Now that you can think clearly, you move deeper into the labyrinth of hallways and office furniture that makes up this part of Easter Bunny HQ. This must be a graphic design department; the walls are covered in specifications for triangles.
4+
5+
Or are they?
6+
7+
The design document gives the side lengths of each triangle it describes, but... `5 10 25`? Some of these aren't triangles. You can't help but mark the impossible ones.
8+
9+
In a valid triangle, the sum of any two sides must be larger than the remaining side. For example, the "triangle" given above is impossible, because `5 + 10` is not larger than `25`.
10+
11+
In your puzzle input, **how many** of the listed triangles are **possible**?
12+
13+
## Part Two
14+
15+
Now that you've helpfully marked up their design documents, it occurs to you that triangles are specified in groups of three **vertically**. Each set of three numbers in a column specifies a triangle. Rows are unrelated.
16+
17+
For example, given the following specification, numbers with the same hundreds digit would be part of the same triangle:
18+
19+
```
20+
101 301 501
21+
102 302 502
22+
103 303 503
23+
201 401 601
24+
202 402 602
25+
203 403 603
26+
```
27+
28+
In your puzzle input, and instead reading by columns, **how many** of the listed triangles are **possible**?

0 commit comments

Comments
 (0)