Skip to content

Commit 39e78a0

Browse files
author
Stefan Koch
committed
Add puzzle solution for day 04 and md5 library
1 parent 192e613 commit 39e78a0

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

04.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function calc(input) {
2+
input = input.trim();
3+
var i = -1;
4+
var hash, string, first_five, first_six;
5+
var i_five = -1;
6+
var i_six = -1;
7+
do {
8+
i++;
9+
string = input + i;
10+
hash = md5(string);
11+
first_five = hash.slice(0, 5);
12+
first_six = hash.slice(0, 6);
13+
if (i_five < 0 && first_five === "00000") {
14+
i_five = i;
15+
}
16+
if (i_six < 0 && first_six === "000000") {
17+
i_six = i;
18+
}
19+
} while (i_five < 0 || i_six < 0);
20+
21+
setResult(1, i_five);
22+
setResult(2, i_six);
23+
}

04.log

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--- Day 4: The Ideal Stocking Stuffer ---
2+
3+
Santa needs help mining some AdventCoins (very similar to bitcoins) to use as gifts for all the economically forward-thinking little girls and boys.
4+
5+
To do this, he needs to find MD5 hashes which, in hexadecimal, start with at least five zeroes. The input to the MD5 hash is some secret key (your puzzle input, given below) followed by a number in decimal. To mine AdventCoins, you must find Santa the lowest positive number (no leading zeroes: 1, 2, 3, ...) that produces such a hash.
6+
7+
For example:
8+
9+
If your secret key is abcdef, the answer is 609043, because the MD5 hash of abcdef609043 starts with five zeroes (000001dbbfa...), and it is the lowest such number to do so.
10+
If your secret key is pqrstuv, the lowest number it combines with to make an MD5 hash starting with five zeroes is 1048970; that is, the MD5 hash of pqrstuv1048970 looks like 000006136ef....
11+
Your puzzle answer was 254575.
12+
13+
--- Part Two ---
14+
15+
Now find one that starts with six zeroes.
16+
17+
Your puzzle answer was 1038736.
18+
19+
Both parts of this puzzle are complete! They provide two gold stars: **

04.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bgvyzdsv

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<meta charset="utf-8">
55
<title>Advent of code</title>
6+
<script type="text/javascript" src="lib/md5.min.js"></script>
67
<script type="text/javascript" src="input.js"></script>
78
</head>
89
<body>

lib/md5.min.js

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

0 commit comments

Comments
 (0)