-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path15.cpp
77 lines (65 loc) · 1.64 KB
/
15.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
71
72
73
74
75
76
77
#include <cctype>
#include <chrono>
#include <cstdint>
#include <iostream>
unsigned int solve_pt1(uint64_t a, uint64_t b) {
unsigned int count = 0;
for (int i = 0; i < 40e6; i++) {
a = (a * 16807) % 2147483647;
b = (b * 48271) % 2147483647;
count += !((a ^ b) & 0xFFFF);
}
return count;
}
unsigned int solve_pt2(uint64_t a, uint64_t b) {
unsigned int count = 0;
for (int i = 0; i < 5e6; i++) {
do {
a = (a * 16807) % 2147483647;
} while ((a & 3));
do {
b = (b * 48271) % 2147483647;
} while ((b & 7));
count += !((a ^ b) & 0xFFFF);
}
return count;
}
struct Input {
uint64_t a;
uint64_t b;
};
Input parse_input() {
std::string input;
uint64_t a = 0;
uint64_t b = 0;
while (std::getline(std::cin, input)) {
auto it = input.end() - 1;
while (std::isdigit(*it)) {
it--;
}
if (a == 0) {
a = static_cast<uint64_t>(std::stoi(&(*it)));
} else {
b = static_cast<uint64_t>(std::stoi(&(*it)));
}
}
return {a, b};
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
unsigned int pt1 = 0;
unsigned int pt2 = 0;
const auto [a, b] = parse_input();
pt1 = solve_pt1(a, b);
pt2 = solve_pt2(a, b);
std::cout << "--- Day 15: Dueling Generators ---\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: " << (static_cast<double>(duration.count()) / 1000.0)
<< " ms"
<< "\n";
return EXIT_SUCCESS;
}