diff options
Diffstat (limited to '2015/src')
| -rw-r--r-- | 2015/src/day04.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/2015/src/day04.cpp b/2015/src/day04.cpp new file mode 100644 index 0000000..bc24177 --- /dev/null +++ b/2015/src/day04.cpp | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | // Standard C++ | ||
| 2 | #include <array> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <string> | ||
| 6 | |||
| 7 | // Standard C | ||
| 8 | #include <cstring> | ||
| 9 | |||
| 10 | // System | ||
| 11 | #include <md5.h> | ||
| 12 | |||
| 13 | using namespace std; | ||
| 14 | |||
| 15 | void | ||
| 16 | brute_force(string_view puzzle, size_t length) | ||
| 17 | { | ||
| 18 | array<char, MD5_DIGEST_STRING_LENGTH> digest{}; | ||
| 19 | |||
| 20 | string input; | ||
| 21 | for ( unsigned long counter = 0;; ++counter ) { | ||
| 22 | input = puzzle; | ||
| 23 | input += to_string(counter); | ||
| 24 | MD5Data(input.data(), input.size(), digest.data()); | ||
| 25 | |||
| 26 | if ( memcmp(digest.data(), "00000000000", length) == 0 ) { | ||
| 27 | cout << digest.data() << ": " << counter << endl; | ||
| 28 | return; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | void | ||
| 34 | part1(string_view puzzle) | ||
| 35 | { | ||
| 36 | brute_force(puzzle, 5); | ||
| 37 | } | ||
| 38 | |||
| 39 | void | ||
| 40 | part2(string_view puzzle) | ||
| 41 | { | ||
| 42 | brute_force(puzzle, 6); | ||
| 43 | } | ||
| 44 | |||
| 45 | int | ||
| 46 | main() | ||
| 47 | { | ||
| 48 | string puzzle = "yzbqklnj"; | ||
| 49 | |||
| 50 | part1(puzzle); | ||
| 51 | part2(puzzle); | ||
| 52 | } | ||
