diff options
Diffstat (limited to 'src/2016/day14/aoc.cpp')
-rw-r--r-- | src/2016/day14/aoc.cpp | 74 |
1 files changed, 73 insertions, 1 deletions
diff --git a/src/2016/day14/aoc.cpp b/src/2016/day14/aoc.cpp index c46c374..dc49f69 100644 --- a/src/2016/day14/aoc.cpp +++ b/src/2016/day14/aoc.cpp @@ -1,6 +1,78 @@ #include "aoc.h" +#include "md5.h" +#include <map> +#include <set> namespace aoc2016 { -std::pair<int64_t, int64_t> day14(line_view) { return {0, 0}; } +static std::map<char, std::set<size_t>> has3s; +static std::map<char, std::set<size_t>> has5s; + +static bool is_equal(const char* p0, const char* p1) { + while (p0 < p1) { + if (*p0 != *p1) { + return false; + } + p0++; + } + return true; +} + +const char* has(const char* md5, int n) { + const char* p = md5; + while (p < md5 + 32 - n + 1) { + if (is_equal(p, p + n - 1)) { + return p; + } + p++; + } + return nullptr; +} + +struct md5_property { + std::string md5; + char c3 = 0; + char c5 = 0; +}; + +static void insert(char c, size_t index, std::map<char, std::set<size_t>>& hs) { + auto p = hs.insert({c, {index}}); + if (!p.second) { + p.first->second.insert(index); + } +} + +md5_property md5(const char* secret, size_t index) { + static std::vector<md5_property> md5s; + if (index >= md5s.size()) { + char buf[128] = {0}; + int l = strlen(secret); + memcpy(buf, secret, l); + auto s = md5s.size(); + md5s.resize(index + 1); + for (size_t i = s; i < md5s.size(); i++) { + sprintf(buf + l, "%zu", index); + md5s[i].md5 = std::string(md5sum(buf), 32); + auto pc3 = has(md5s[i].md5.c_str(), 3); + if (pc3 != nullptr) { + md5s[i].c3 = *pc3; + insert(*pc3, index, has3s); + } + auto pc5 = has(md5s[i].md5.c_str(), 5); + if (pc5 != nullptr) { + md5s[i].c5 = *pc5; + insert(*pc5, index, has5s); + } + } + } + return md5s[index]; +} + +void find_key(const char* secret, size_t index, int* count) {} + +std::pair<int64_t, int64_t> day14(line_view) { + // md5("abc", 30000); + md5("jlmsuwbz", 30000); + return {0, 0}; +} } // namespace aoc2016 |