diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-01-22 14:16:19 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-01-22 14:16:19 +0800 |
commit | c0de24475bcb8231d951ca894c3717ec4aa3080a (patch) | |
tree | 368388a5bcea54c3f202e7eee23b91425d3ab41f /src | |
parent | 6486ade92a73f9a248af5669d263286d301bfed1 (diff) | |
download | advent-of-code-c0de24475bcb8231d951ca894c3717ec4aa3080a.tar.gz advent-of-code-c0de24475bcb8231d951ca894c3717ec4aa3080a.zip |
2016 day14
Diffstat (limited to 'src')
-rw-r--r-- | src/2016/day14/aoc.cpp | 65 |
1 files changed, 42 insertions, 23 deletions
diff --git a/src/2016/day14/aoc.cpp b/src/2016/day14/aoc.cpp index dc49f69..364bb5e 100644 --- a/src/2016/day14/aoc.cpp +++ b/src/2016/day14/aoc.cpp @@ -5,8 +5,13 @@ namespace aoc2016 { -static std::map<char, std::set<size_t>> has3s; -static std::map<char, std::set<size_t>> has5s; +struct md5_property { + std::string md5; + char c = 0; +}; + +static std::vector<md5_property> md5s; +static std::map<char, std::vector<size_t>> has5s; static bool is_equal(const char* p0, const char* p1) { while (p0 < p1) { @@ -29,21 +34,21 @@ const char* has(const char* md5, int n) { 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); +static bool in_range(char c, size_t l, size_t h) { + auto it = has5s.find(c); + if (it != has5s.end()) { + auto& v = it->second; + size_t ll = v[0]; + size_t hh = v[v.size() - 1]; + auto is_in = [l, h](size_t x) { return x >= l && x <= h; }; + if (is_in(ll) || is_in(hh)) { + return true; + } } + return false; } 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); @@ -55,24 +60,38 @@ md5_property md5(const char* secret, size_t 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); + md5s[i].c = *pc3; + auto pc5 = has(md5s[i].md5.c_str(), 5); + if (pc5 != nullptr) { + auto p = has5s.insert({*pc3, {index}}); + if (!p.second) { + p.first->second.push_back(index); + } + } } } } return md5s[index]; } -void find_key(const char* secret, size_t index, int* count) {} +size_t find_key(int* count, int max) { + size_t i{0}; + while (*count < max && i < md5s.size()) { + auto& p = md5s[i]; + if (p.c > 0 && in_range(p.c, i + 1, i + 1000)) { + *count += 1; + } + i++; + } + return i; +} std::pair<int64_t, int64_t> day14(line_view) { - // md5("abc", 30000); - md5("jlmsuwbz", 30000); + md5("abc", 30000); + // md5("jlmsuwbz", 30000); + int count{0}; + auto i = find_key(&count, 64); + printf("%zu\n", i); return {0, 0}; } } // namespace aoc2016 |