4
4
#include < fstream>
5
5
#include < vector>
6
6
7
- const uint64_t LIMIT = 2000 ;
7
+ const int64_t LIMIT = 2000 ;
8
8
9
- uint64_t next (uint64_t secret) {
10
- uint64_t mask = 16777215 ; // = 2^24 - 1
9
+ int64_t next (int64_t secret) {
10
+ int64_t mask = 16777215 ; // = 2^24 - 1
11
11
secret ^= secret << 6 ;
12
12
secret &= mask;
13
13
secret ^= secret >> 5 ;
@@ -17,19 +17,19 @@ uint64_t next(uint64_t secret) {
17
17
return secret;
18
18
}
19
19
20
- uint64_t prng (uint64_t secret, uint64_t n) {
21
- for (uint64_t i = 0 ; i < n; i++) {
20
+ int64_t prng (int64_t secret, int64_t n) {
21
+ for (int64_t i = 0 ; i < n; i++) {
22
22
secret = next (secret);
23
23
}
24
24
return secret;
25
25
}
26
26
27
- uint64_t monkey (uint64_t secret, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4) {
28
- uint64_t d1 = -1 , d2 = -1 , d3 = -1 , d4 = -1 ;
29
- for (uint64_t i = 0 ; i < LIMIT; i++) {
30
- uint64_t lastPrice = secret % 10 ;
27
+ int64_t monkey (int64_t secret, int64_t x1, int64_t x2, int64_t x3, int64_t x4) {
28
+ int64_t d1 = -1 , d2 = -1 , d3 = -1 , d4 = -1 ;
29
+ for (int64_t i = 0 ; i < LIMIT; i++) {
30
+ int64_t lastPrice = secret % 10 ;
31
31
secret = next (secret);
32
- uint64_t price = secret % 10 ;
32
+ int64_t price = secret % 10 ;
33
33
d1 = d2;
34
34
d2 = d3;
35
35
d3 = d4;
@@ -41,27 +41,27 @@ uint64_t monkey(uint64_t secret, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t
41
41
return -1 ;
42
42
}
43
43
44
- uint64_t score (std::vector<uint64_t > input, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t x4) {
45
- uint64_t sum = 0 ;
46
- for (uint64_t n : input) {
47
- uint64_t price = monkey (n, x1, x2, x3, x4);
44
+ int64_t score (std::vector<int64_t > input, int64_t x1, int64_t x2, int64_t x3, int64_t x4) {
45
+ int64_t sum = 0 ;
46
+ for (int64_t n : input) {
47
+ int64_t price = monkey (n, x1, x2, x3, x4);
48
48
if (price > 0 ) {
49
49
sum += price;
50
50
}
51
51
}
52
52
return sum;
53
53
}
54
54
55
- uint64_t findBestScore (std::vector<uint64_t > input) {
56
- uint64_t bestScore = 0 ;
57
- uint64_t bound = 9 ;
58
- #pragma omp parallel for
59
- for (uint64_t x1 = -bound; x1 <= bound; x1++) {
60
- for (uint64_t x2 = -bound; x2 <= bound; x2++) {
55
+ int64_t findBestScore (std::vector<int64_t > input) {
56
+ int64_t bestScore = 0 ;
57
+ int64_t bound = 9 ;
58
+ for (int64_t x1 = -bound; x1 <= bound; x1++) {
59
+ for (int64_t x2 = -bound; x2 <= bound; x2++) {
61
60
std::cout << " Searching (" << x1 << " , " << x2 << " )" << std::endl;
62
- for (uint64_t x3 = -bound; x3 <= bound; x3++) {
63
- for (uint64_t x4 = -bound; x4 <= bound; x4++) {
64
- uint64_t n = score (input, x1, x2, x3, x4);
61
+ #pragma omp parallel for
62
+ for (int64_t x3 = -bound; x3 <= bound; x3++) {
63
+ for (int64_t x4 = -bound; x4 <= bound; x4++) {
64
+ int64_t n = score (input, x1, x2, x3, x4);
65
65
if (n > bestScore) {
66
66
bestScore = n;
67
67
}
@@ -78,7 +78,7 @@ int main(int argc, char *argv[]) {
78
78
return 1 ;
79
79
}
80
80
81
- std::vector<uint64_t > input;
81
+ std::vector<int64_t > input;
82
82
83
83
{
84
84
std::ifstream file;
@@ -88,13 +88,13 @@ int main(int argc, char *argv[]) {
88
88
}
89
89
}
90
90
91
- uint64_t part1 = 0 ;
92
- for (uint64_t n : input) {
91
+ int64_t part1 = 0 ;
92
+ for (int64_t n : input) {
93
93
part1 += prng (n, LIMIT);
94
94
}
95
95
std::cout << " Part 1: " << part1 << std::endl;
96
96
97
- uint64_t part2 = findBestScore (input);
97
+ int64_t part2 = findBestScore (input);
98
98
std::cout << " Part 2: " << part2 << std::endl;
99
99
100
100
return 0 ;
0 commit comments