Skip to content

Commit 971d09f

Browse files
committed
Use uint64_t for C++ implementation
1 parent 5d7ca7b commit 971d09f

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

day22/experiments/day22.cpp

+27-26
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include <cstdlib>
2+
#include <cstdint>
23
#include <iostream>
34
#include <fstream>
45
#include <vector>
56

6-
const int LIMIT = 2000;
7+
const uint64_t LIMIT = 2000;
78

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
1011
secret ^= secret << 6;
1112
secret &= mask;
1213
secret ^= secret >> 5;
@@ -16,19 +17,19 @@ int next(int secret) {
1617
return secret;
1718
}
1819

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++) {
2122
secret = next(secret);
2223
}
2324
return secret;
2425
}
2526

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;
3031
secret = next(secret);
31-
int price = secret % 10;
32+
uint64_t price = secret % 10;
3233
d1 = d2;
3334
d2 = d3;
3435
d3 = d4;
@@ -40,27 +41,27 @@ int monkey(int secret, int x1, int x2, int x3, int x4) {
4041
return -1;
4142
}
4243

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);
4748
if (price > 0) {
4849
sum += price;
4950
}
5051
}
5152
return sum;
5253
}
5354

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;
5758
#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++) {
6061
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);
6465
if (n > bestScore) {
6566
bestScore = n;
6667
}
@@ -77,7 +78,7 @@ int main(int argc, char *argv[]) {
7778
return 1;
7879
}
7980

80-
std::vector<int> input;
81+
std::vector<uint64_t> input;
8182

8283
{
8384
std::ifstream file;
@@ -87,13 +88,13 @@ int main(int argc, char *argv[]) {
8788
}
8889
}
8990

90-
int part1 = 0;
91-
for (int n : input) {
91+
uint64_t part1 = 0;
92+
for (uint64_t n : input) {
9293
part1 += prng(n, LIMIT);
9394
}
9495
std::cout << "Part 1: " << part1 << std::endl;
9596

96-
int part2 = findBestScore(input);
97+
uint64_t part2 = findBestScore(input);
9798
std::cout << "Part 2: " << part2 << std::endl;
9899

99100
return 0;

0 commit comments

Comments
 (0)