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