aboutsummaryrefslogtreecommitdiff
path: root/day4
diff options
context:
space:
mode:
authorNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-12-07 15:16:10 +0000
committerNathan Lasseter <Nathan Lasseter nathan@bytemark.co.uk>2015-12-07 15:16:10 +0000
commit09cbfeeca74e6b7ca9a9b5d49646c538b8a3f4ba (patch)
treed91f437cc4b676155b7c110b8da8c90995523771 /day4
parent02d64d66b1e3c904b54c19ac2b5e9756d49959fb (diff)
Rearranged into folders, added makefile for convenience.
Diffstat (limited to 'day4')
l---------day4/Makefile1
-rw-r--r--day4/day4.c56
-rw-r--r--day4/day4.input2
-rw-r--r--day4/day4.readme14
-rw-r--r--day4/override.mk1
5 files changed, 74 insertions, 0 deletions
diff --git a/day4/Makefile b/day4/Makefile
new file mode 120000
index 0000000..d0b0e8e
--- /dev/null
+++ b/day4/Makefile
@@ -0,0 +1 @@
+../Makefile \ No newline at end of file
diff --git a/day4/day4.c b/day4/day4.c
new file mode 100644
index 0000000..339ea1a
--- /dev/null
+++ b/day4/day4.c
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <openssl/md5.h>
+
+unsigned char secret[10];
+unsigned char target[10];
+unsigned char result[MD5_DIGEST_LENGTH];
+
+void hash(int i) {
+ char num[12];
+ sprintf(num, "%d", i);
+ char str[22] = "";
+ strcat(str, secret);
+ strcat(str, num);
+
+ MD5(str, strlen(str), result);
+}
+
+int tohex() {
+ int i, dg = 0;
+ for(i = 0; i < 4; i++) {
+ dg <<= 8;
+ dg += result[i];
+ }
+ return dg;
+}
+
+int check(int len) {
+ unsigned char str[8];
+ int dg = tohex();
+ sprintf(str, "%08x", dg);
+ return strncmp(str, target, len) == 0;
+}
+
+int main() {
+ int i = 1, got5 = 0, got6 = 0;
+
+ scanf("%s", secret);
+ scanf("%s", target);
+
+ while(!(got5 && got6)) {
+ hash(i);
+ if(!got5 && check(5)) {
+ got5 = 1;
+ printf("Lowest number for 5-zeroes hash: %d\n", i);
+ }
+ if(!got6 && check(6)) {
+ got6 = 1;
+ printf("Lowest number for 6-zeroes hash: %d\n", i);
+ }
+ i++;
+ }
+
+ return 0;
+}
diff --git a/day4/day4.input b/day4/day4.input
new file mode 100644
index 0000000..5d8d296
--- /dev/null
+++ b/day4/day4.input
@@ -0,0 +1,2 @@
+iwrupvqb
+000000
diff --git a/day4/day4.readme b/day4/day4.readme
new file mode 100644
index 0000000..b629160
--- /dev/null
+++ b/day4/day4.readme
@@ -0,0 +1,14 @@
+--- Day 4: The Ideal Stocking Stuffer ---
+
+Santa needs help mining some AdventCoins (very similar to bitcoins) to use as gifts for all the economically forward-thinking little girls and boys.
+
+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.
+
+For example:
+
+ - 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.
+ - 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....
+
+--- Part Two ---
+
+Now find one that starts with six zeroes.
diff --git a/day4/override.mk b/day4/override.mk
new file mode 100644
index 0000000..b031763
--- /dev/null
+++ b/day4/override.mk
@@ -0,0 +1 @@
+DAYFLAGS=-lcrypto