diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/2015/day4/README.md | 5 | ||||
-rw-r--r-- | src/2015/day4/aoc.cpp | 15 | ||||
-rw-r--r-- | src/2015/day4/aoc.h | 1 |
3 files changed, 21 insertions, 0 deletions
diff --git a/src/2015/day4/README.md b/src/2015/day4/README.md index b3f4140..99334b6 100644 --- a/src/2015/day4/README.md +++ b/src/2015/day4/README.md @@ -8,3 +8,8 @@ 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.... + +The first half of this puzzle is complete! It provides one gold star: * +--- Part Two --- + +Now find one that starts with six zeroes. diff --git a/src/2015/day4/aoc.cpp b/src/2015/day4/aoc.cpp index 1bb0d4e..ab5f301 100644 --- a/src/2015/day4/aoc.cpp +++ b/src/2015/day4/aoc.cpp @@ -27,4 +27,19 @@ int lead_zeros(char* s) { return total; } +int day4(const char* secret, int target) { + char buf[128] = {0}; + int len = strlen(secret); + memcpy(buf, secret, len); + int i = 1; + while (i < INT32_MAX) { + sprintf(buf+len, "%d", i); + if (lead_zeros(md5sum(buf)) >= target) { + break; + } + i++; + } + return i; +} + } // namespace aoc2015 diff --git a/src/2015/day4/aoc.h b/src/2015/day4/aoc.h index 56d8ba3..88a38fc 100644 --- a/src/2015/day4/aoc.h +++ b/src/2015/day4/aoc.h @@ -6,5 +6,6 @@ namespace aoc2015 { char* md5sum(char *); int lead_zeros(char*); +int day4(const char*, int); } |