diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-01-12 17:36:26 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-01-12 17:36:26 +0100 |
| commit | d396dc3221fcf893c885bd9a370b9a30f25b31dd (patch) | |
| tree | f003b49ea2601a54926a2ef3de3b06f0f897dc37 /2016/src/day05.cpp | |
| parent | a8f11c0a8470c4e6b67e70219986712cd70d0de2 (diff) | |
| download | advent-of-code-d396dc3221fcf893c885bd9a370b9a30f25b31dd.tar.gz advent-of-code-d396dc3221fcf893c885bd9a370b9a30f25b31dd.tar.bz2 advent-of-code-d396dc3221fcf893c885bd9a370b9a30f25b31dd.zip | |
aoc 2016, days 4, 5, 6
Diffstat (limited to '2016/src/day05.cpp')
| -rw-r--r-- | 2016/src/day05.cpp | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/2016/src/day05.cpp b/2016/src/day05.cpp new file mode 100644 index 0000000..dfce653 --- /dev/null +++ b/2016/src/day05.cpp | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | // Standard C++ | ||
| 2 | #include <array> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <string> | ||
| 6 | |||
| 7 | // Standard C | ||
| 8 | #include <cstdlib> // memcmp | ||
| 9 | |||
| 10 | // System | ||
| 11 | #include <md5.h> | ||
| 12 | |||
| 13 | using namespace std; | ||
| 14 | |||
| 15 | void | ||
| 16 | part1(string_view puzzle) | ||
| 17 | { | ||
| 18 | array<char, MD5_DIGEST_STRING_LENGTH> digest{}; | ||
| 19 | |||
| 20 | string result; | ||
| 21 | |||
| 22 | string input; | ||
| 23 | for ( unsigned long counter = 0;; ++counter ) { | ||
| 24 | input = puzzle; | ||
| 25 | input += to_string(counter); | ||
| 26 | MD5Data(input.data(), input.size(), digest.data()); | ||
| 27 | |||
| 28 | if ( memcmp(digest.data(), "00000", 5) == 0 ) { | ||
| 29 | result += digest.at(5); | ||
| 30 | if ( result.length() == 8 ) { | ||
| 31 | cout << result << endl; | ||
| 32 | return; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | void | ||
| 39 | part2(string_view puzzle) | ||
| 40 | { | ||
| 41 | array<char, MD5_DIGEST_STRING_LENGTH> digest{}; | ||
| 42 | |||
| 43 | string result = "________"; | ||
| 44 | |||
| 45 | string input; | ||
| 46 | for ( unsigned long counter = 0;; ++counter ) { | ||
| 47 | input = puzzle; | ||
| 48 | input += to_string(counter); | ||
| 49 | MD5Data(input.data(), input.size(), digest.data()); | ||
| 50 | |||
| 51 | if ( memcmp(digest.data(), "00000", 5) == 0 ) { | ||
| 52 | auto position = digest.at(5) - '0'; | ||
| 53 | if ( position < 0 || position > 7 ) { | ||
| 54 | continue; | ||
| 55 | } | ||
| 56 | if ( result.at(size_t(position)) != '_' ) { | ||
| 57 | continue; | ||
| 58 | } | ||
| 59 | result.at(size_t(position)) = digest.at(6); | ||
| 60 | if ( result.find('_') == string::npos ) { | ||
| 61 | cout << result << endl; | ||
| 62 | return; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | int | ||
| 69 | main() | ||
| 70 | { | ||
| 71 | auto puzzle = "reyedfim"s; | ||
| 72 | part1(puzzle); | ||
| 73 | part2(puzzle); | ||
| 74 | } | ||
