-
Notifications
You must be signed in to change notification settings - Fork 2
/
05.cpp
70 lines (53 loc) · 1.52 KB
/
05.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <chrono>
#include <iostream>
#include <openssl/md5.h>
using std::string;
string md5(const string& str) {
static unsigned char hash[MD5_DIGEST_LENGTH];
static const char characters[] = "0123456789abcdef";
MD5(reinterpret_cast<const unsigned char*>(str.c_str()), str.size(), hash);
// faster method of converting to hexadecimal
string ret;
ret.reserve(4 * 2);
auto buf = std::back_inserter(ret);
for (int i = 0; i < 4; i++) {
auto byte = hash[i];
*buf++ = characters[byte >> 4];
*buf++ = characters[byte & 0x0F];
}
return ret;
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
string input;
std::getline(std::cin, input);
char pt1[9] = {0};
char pt2[9] = {0};
int pi = 0;
int pl = 0;
for (int i = 0;; i++) {
string r = md5(input + std::to_string(i));
if (r.compare(0, 5, "00000") == 0) {
if (pi < 8) {
pt1[pi++] = r[5];
}
int pi2 = r[5] - '0';
if (pl < 8 && pi2 >= 0 && pi2 < 8 && pt2[pi2] == 0) {
pt2[r[5] - '0'] = r[6];
pl += 1;
}
}
if (pi >= 8 && pl >= 8) {
break;
}
}
std::cout << "--- Day 5: How About a Nice Game of Chess? ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2: " << pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << duration.count() << " μs"
<< "\n";
return EXIT_SUCCESS;
}