aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/2015/day4/aoc.cpp29
-rw-r--r--src/2015/day4/aoc.h3
-rw-r--r--test/test_2015.cpp10
3 files changed, 36 insertions, 6 deletions
diff --git a/src/2015/day4/aoc.cpp b/src/2015/day4/aoc.cpp
index 6e47928..1bb0d4e 100644
--- a/src/2015/day4/aoc.cpp
+++ b/src/2015/day4/aoc.cpp
@@ -1,3 +1,30 @@
#include "aoc.h"
-namespace aoc2015 {}
+namespace aoc2015 {
+
+char* md5sum(char* s) {
+ static char md5[64] = {0};
+ uint8_t* x = md5String(s);
+ memset(md5, 0x0, 64);
+ for (auto i = 0; i < 16; i++) {
+ sprintf(md5 + i * 2, "%02x", x[i]);
+ }
+ return md5;
+}
+
+int lead_zeros(char* s) {
+ char* p = s;
+ int total = 0;
+ while (*p != '\0') {
+ if (*p == '0') {
+ total += 1;
+ p++;
+ }
+ else {
+ break;
+ }
+ }
+ return total;
+}
+
+} // namespace aoc2015
diff --git a/src/2015/day4/aoc.h b/src/2015/day4/aoc.h
index c8a6225..56d8ba3 100644
--- a/src/2015/day4/aoc.h
+++ b/src/2015/day4/aoc.h
@@ -4,4 +4,7 @@
namespace aoc2015 {
+char* md5sum(char *);
+int lead_zeros(char*);
+
}
diff --git a/test/test_2015.cpp b/test/test_2015.cpp
index 2aef880..a7c68a6 100644
--- a/test/test_2015.cpp
+++ b/test/test_2015.cpp
@@ -37,9 +37,9 @@ TEST_CASE("Perfectly Spherical Houses in a Vacuum", "[day3]") {
}
TEST_CASE("The Ideal Stocking Stuffer", "[day4]") {
- char s[] = "abcdef609043";
- uint8_t* md5 = md5String(s);
- for (auto i = 0; i < 16; i++) {
- printf("%x", md5[i]);
- }
+ char s1[] = "abcdef609043";
+ char s2[] = "pqrstuv1048970";
+
+ REQUIRE(aoc2015::lead_zeros(aoc2015::md5sum(s1)) >= 5);
+ REQUIRE(aoc2015::lead_zeros(aoc2015::md5sum(s2)) >= 5);
}