diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-10-17 16:37:41 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-10-17 16:37:41 +0200 |
| commit | 1d47028997c893535a2fd9064812a4a7cddaf5f6 (patch) | |
| tree | 4943e349d710f15c7bd4b5f64baa60cb9e545574 /2016/src/day14.cpp | |
| parent | ff958d47b0746d58ccdc08b90be94e43581a5813 (diff) | |
| download | advent-of-code-1d47028997c893535a2fd9064812a4a7cddaf5f6.tar.gz advent-of-code-1d47028997c893535a2fd9064812a4a7cddaf5f6.tar.bz2 advent-of-code-1d47028997c893535a2fd9064812a4a7cddaf5f6.zip | |
day 14, aoc 2016
Diffstat (limited to '2016/src/day14.cpp')
| -rw-r--r-- | 2016/src/day14.cpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/2016/src/day14.cpp b/2016/src/day14.cpp new file mode 100644 index 0000000..a9be56c --- /dev/null +++ b/2016/src/day14.cpp | |||
| @@ -0,0 +1,121 @@ | |||
| 1 | // Standard C++ | ||
| 2 | #include <array> | ||
| 3 | #include <iostream> | ||
| 4 | #include <map> | ||
| 5 | #include <optional> | ||
| 6 | #include <string> | ||
| 7 | |||
| 8 | // Standard C | ||
| 9 | #include <cstring> | ||
| 10 | |||
| 11 | // System | ||
| 12 | #include <md5.h> | ||
| 13 | |||
| 14 | using namespace std; | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | optional<char> | ||
| 19 | contains_repeating_chars(string_view str) | ||
| 20 | { | ||
| 21 | for ( size_t i = 2; i < str.size(); ++i ) { | ||
| 22 | if ( str[i] == str[i - 1] && str[i] == str[i - 2] ) { | ||
| 23 | return str[i]; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | return {}; | ||
| 28 | } | ||
| 29 | |||
| 30 | string | ||
| 31 | md5(const string& input) | ||
| 32 | { | ||
| 33 | array<char, MD5_DIGEST_STRING_LENGTH> digest{}; | ||
| 34 | MD5Data(input.data(), input.size(), digest.data()); | ||
| 35 | return digest.data(); | ||
| 36 | } | ||
| 37 | |||
| 38 | int | ||
| 39 | solve(const function<string(int)>& get_hash) | ||
| 40 | { | ||
| 41 | int keys = 0; | ||
| 42 | |||
| 43 | for ( int index = 0;; ++index ) { | ||
| 44 | auto hash = get_hash(index); | ||
| 45 | |||
| 46 | auto repeat = contains_repeating_chars(hash); | ||
| 47 | |||
| 48 | if ( !repeat.has_value() ) { | ||
| 49 | continue; | ||
| 50 | } | ||
| 51 | |||
| 52 | const string pattern(5, repeat.value()); | ||
| 53 | |||
| 54 | for ( auto index2 = index + 1; index2 != index + 1000; ++index2 ) { | ||
| 55 | hash = get_hash(index2); | ||
| 56 | if ( hash.find(pattern) != string::npos ) { | ||
| 57 | ++keys; | ||
| 58 | break; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | if ( keys == 64 ) { | ||
| 63 | return index; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | void | ||
| 69 | part1(const string& input) | ||
| 70 | { | ||
| 71 | map<int, string> cache; | ||
| 72 | |||
| 73 | auto get_hash = [&](int index) { | ||
| 74 | if ( cache.contains(index) ) { | ||
| 75 | return cache.at(index); | ||
| 76 | } | ||
| 77 | |||
| 78 | auto hash = md5(input + to_string(index)); | ||
| 79 | |||
| 80 | cache.emplace(index, hash); | ||
| 81 | |||
| 82 | return hash; | ||
| 83 | }; | ||
| 84 | |||
| 85 | cout << solve(get_hash) << '\n'; | ||
| 86 | } | ||
| 87 | |||
| 88 | void | ||
| 89 | part2(const string& input) | ||
| 90 | { | ||
| 91 | map<int, string> cache; | ||
| 92 | |||
| 93 | auto get_hash = [&](int index) { | ||
| 94 | if ( cache.contains(index) ) { | ||
| 95 | return cache.at(index); | ||
| 96 | } | ||
| 97 | |||
| 98 | auto hash = input + to_string(index); | ||
| 99 | |||
| 100 | for ( int i = 0; i != 2017; ++i ) { | ||
| 101 | hash = md5(hash); | ||
| 102 | } | ||
| 103 | |||
| 104 | cache.emplace(index, hash); | ||
| 105 | |||
| 106 | return hash; | ||
| 107 | }; | ||
| 108 | |||
| 109 | cout << solve(get_hash) << '\n'; | ||
| 110 | } | ||
| 111 | |||
| 112 | } // namespace | ||
| 113 | |||
| 114 | int | ||
| 115 | main() | ||
| 116 | { | ||
| 117 | // part1("abc"); | ||
| 118 | // part2("abc"); | ||
| 119 | part1("qzyelonm"); | ||
| 120 | part2("qzyelonm"); | ||
| 121 | } | ||
