Skip to content

Commit 007903b

Browse files
committed
Parellelize the inner loop and use signed ints
1 parent 971d09f commit 007903b

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

day22/experiments/day22.cpp

+27-27
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#include <fstream>
55
#include <vector>
66

7-
const uint64_t LIMIT = 2000;
7+
const int64_t LIMIT = 2000;
88

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
1111
secret ^= secret << 6;
1212
secret &= mask;
1313
secret ^= secret >> 5;
@@ -17,19 +17,19 @@ uint64_t next(uint64_t secret) {
1717
return secret;
1818
}
1919

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++) {
2222
secret = next(secret);
2323
}
2424
return secret;
2525
}
2626

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;
3131
secret = next(secret);
32-
uint64_t price = secret % 10;
32+
int64_t price = secret % 10;
3333
d1 = d2;
3434
d2 = d3;
3535
d3 = d4;
@@ -41,27 +41,27 @@ uint64_t monkey(uint64_t secret, uint64_t x1, uint64_t x2, uint64_t x3, uint64_t
4141
return -1;
4242
}
4343

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);
4848
if (price > 0) {
4949
sum += price;
5050
}
5151
}
5252
return sum;
5353
}
5454

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++) {
6160
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);
6565
if (n > bestScore) {
6666
bestScore = n;
6767
}
@@ -78,7 +78,7 @@ int main(int argc, char *argv[]) {
7878
return 1;
7979
}
8080

81-
std::vector<uint64_t> input;
81+
std::vector<int64_t> input;
8282

8383
{
8484
std::ifstream file;
@@ -88,13 +88,13 @@ int main(int argc, char *argv[]) {
8888
}
8989
}
9090

91-
uint64_t part1 = 0;
92-
for (uint64_t n : input) {
91+
int64_t part1 = 0;
92+
for (int64_t n : input) {
9393
part1 += prng(n, LIMIT);
9494
}
9595
std::cout << "Part 1: " << part1 << std::endl;
9696

97-
uint64_t part2 = findBestScore(input);
97+
int64_t part2 = findBestScore(input);
9898
std::cout << "Part 2: " << part2 << std::endl;
9999

100100
return 0;

0 commit comments

Comments
 (0)