Skip to content

Commit 4e7664f

Browse files
feat: 2016 Day 5
Add day 5 of 2016
1 parent f0791d3 commit 4e7664f

File tree

6 files changed

+128
-4
lines changed

6 files changed

+128
-4
lines changed

2016/day/3/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFileSync } from 'fs';
1+
import { readFileSync } from 'node:fs';
22
import { part1, part2 } from '.';
33

44
const input = readFileSync(`${__dirname}/input`, 'utf-8').split('\n');

2016/day/4/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { readFileSync } from 'fs';
1+
import { readFileSync } from 'node:fs';
22
import { part1, part2 } from '.';
33

44
const input = readFileSync(`${__dirname}/input`, 'utf-8').split('\n');
55

6-
describe('2016 Day 3', () => {
6+
describe('2016 Day 4', () => {
77
describe('Part 1', () => {
88
it('should sum the sector IDs of real rooms', () => {
99
expect.assertions(2);

2016/day/5/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## --- Day 5: How About a Nice Game of Chess? ---
2+
3+
You are faced with a security door designed by Easter Bunny engineers that seem to have acquired most of their security knowledge by watching [hacking](<https://en.wikipedia.org/wiki/Hackers_(film)>) [movies](https://en.wikipedia.org/wiki/WarGames).
4+
5+
The _eight-character password_ for the door is generated one character at a time by finding the [MD5](https://en.wikipedia.org/wiki/MD5) hash of some Door ID (your puzzle input) and an increasing integer index (starting with `0`).
6+
7+
A hash indicates the _next character_ in the password if its [hexadecimal](https://en.wikipedia.org/wiki/Hexadecimal) representation starts with _five zeroes_. If it does, the sixth character in the hash is the next character of the password.
8+
9+
For example, if the Door ID is `abc`:
10+
11+
- The first index which produces a hash that starts with five zeroes is `3231929`, which we find by hashing `abc3231929`; the sixth character of the hash, and thus the first character of the password, is `1`.
12+
- `5017308` produces the next interesting hash, which starts with `000008f82...`, so the second character of the password is `8`.
13+
- The third time a hash starts with five zeroes is for `abc5278568`, discovering the character `f`.
14+
15+
In this example, after continuing this search a total of eight times, the password is `18f47a30`.
16+
17+
Given the actual Door ID, _what is the password_?
18+
19+
Your puzzle answer was `1a3099aa`.
20+
21+
## --- Part Two ---
22+
23+
As the door slides open, you are presented with a second door that uses a slightly more <span title="This one says 'WOPR' in block letters.">inspired</span> security mechanism. Clearly unimpressed by the last version (in what movie is the password decrypted _in order_?!), the Easter Bunny engineers have worked out [a better solution](https://www.youtube.com/watch?v=NHWjlCaIrQo&t=25).
24+
25+
Instead of simply filling in the password from left to right, the hash now also indicates the _position_ within the password to fill. You still look for hashes that begin with five zeroes; however, now, the _sixth_ character represents the _position_ (`0`-`7`), and the _seventh_ character is the character to put in that position.
26+
27+
A hash result of `000001f` means that `f` is the _second_ character in the password. Use only the _first result_ for each position, and ignore invalid positions.
28+
29+
For example, if the Door ID is `abc`:
30+
31+
- The first interesting hash is from `abc3231929`, which produces `0000015...`; so, `5` goes in position `1`: `_5______`.
32+
- In the previous method, `5017308` produced an interesting hash; however, it is ignored, because it specifies an invalid position (`8`).
33+
- The second interesting hash is at index `5357525`, which produces `000004e...`; so, `e` goes in position `4`: `_5__e___`.
34+
35+
You almost choke on your popcorn as the final character falls into place, producing the password `05ace8e3`.
36+
37+
Given the actual Door ID and this new method, _what is the password_? Be extra proud of your solution if it uses a cinematic "decrypting" animation.
38+
39+
Your puzzle answer was `694190cd`.
40+
41+
Both parts of this puzzle are complete! They provide two gold stars: \*\*
42+
43+
At this point, you should [return to your Advent calendar](/2016) and try another puzzle.
44+
45+
Your puzzle input was `uqwqemis`.
46+
47+
You can also <span class="share">[Share<span class="share-content">on [Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+%22How+About+a+Nice+Game+of+Chess%3F%22+%2D+Day+5+%2D+Advent+of+Code+2016&url=https%3A%2F%2Fadventofcode%2Ecom%2F2016%2Fday%2F5&related=ericwastl&hashtags=AdventOfCode) [Mastodon](<javascript:void(0);>)</span>]</span> this puzzle.

2016/day/5/index.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { part1, part2 } from '.';
2+
3+
const ONE_MINUTE_IN_MS = 1000 * 60;
4+
5+
describe('2016 Day 5', () => {
6+
describe('Part 1', () => {
7+
it(
8+
'should sum the sector IDs of real rooms',
9+
() => {
10+
expect.assertions(2);
11+
12+
expect(part1('abc')).toStrictEqual('18f47a30');
13+
expect(part1('uqwqemis')).toStrictEqual('1a3099aa');
14+
},
15+
ONE_MINUTE_IN_MS,
16+
);
17+
});
18+
19+
describe('Part 2', () => {
20+
it(
21+
'should find the sector ID of the room where the northpole objects are being stored',
22+
() => {
23+
expect.assertions(2);
24+
25+
expect(part2('abc')).toStrictEqual('05ace8e3');
26+
expect(part2('uqwqemis')).toStrictEqual('694190cd');
27+
},
28+
ONE_MINUTE_IN_MS,
29+
);
30+
});
31+
});

2016/day/5/index.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { createHash } from 'node:crypto';
2+
3+
function part1(doorId: string): string {
4+
const password: string[] = [];
5+
let index = 0;
6+
7+
while (password.length < 8) {
8+
const hash = createHash('md5')
9+
.update(doorId + index)
10+
.digest('hex');
11+
12+
if (hash.startsWith('00000')) {
13+
password.push(hash[5]);
14+
}
15+
16+
index += 1;
17+
}
18+
19+
return password.join('');
20+
}
21+
22+
function part2(doorId: string): string {
23+
const password: string[] = [];
24+
let index = 0;
25+
26+
while (password.length < 8) {
27+
const hash = createHash('md5')
28+
.update(doorId + index)
29+
.digest('hex');
30+
31+
if (hash.startsWith('00000')) {
32+
const position = parseInt(hash[5], 10);
33+
34+
if (position < 8 && !password[position]) {
35+
// eslint-disable-next-line prefer-destructuring
36+
password[position] = hash[6];
37+
}
38+
}
39+
40+
index += 1;
41+
}
42+
43+
return password.join('');
44+
}
45+
46+
export { part1, part2 };

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ My attempt of solving [Advent of Code](https://adventofcode.com/) puzzles in Jav
176176
| [Day 8](2016/day/8) | |
177177
| [Day 7](2016/day/7) | |
178178
| [Day 6](2016/day/6) | |
179-
| [Day 5](2016/day/5) | |
179+
| [Day 5](2016/day/5) | ⭐⭐ |
180180
| [Day 4](2016/day/4) | ⭐⭐ |
181181
| [Day 3](2016/day/3) ||
182182
| [Day 2](2016/day/2) | ⭐⭐ |

0 commit comments

Comments
 (0)