aboutsummaryrefslogtreecommitdiff
path: root/2016/src/day05.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2016/src/day05.cpp')
-rw-r--r--2016/src/day05.cpp74
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
13using namespace std;
14
15void
16part1(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
38void
39part2(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
68int
69main()
70{
71 auto puzzle = "reyedfim"s;
72 part1(puzzle);
73 part2(puzzle);
74}