Skip to content

Commit a1f3fcd

Browse files
committed
Use nullable types for monkey return
1 parent a79d56a commit a1f3fcd

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

day22/experiments/day22.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int64_t prng(int64_t secret, int64_t n) {
2525
return secret;
2626
}
2727

28-
int64_t monkey(int64_t secret, int64_t x1, int64_t x2, int64_t x3, int64_t x4) {
28+
std::optional<int64_t> monkey(int64_t secret, int64_t x1, int64_t x2, int64_t x3, int64_t x4) {
2929
std::optional<int64_t> d1, d2, d3, d4;
3030
for (int64_t i = 0; i < LIMIT; i++) {
3131
int64_t lastPrice = secret % 10;
@@ -39,15 +39,15 @@ int64_t monkey(int64_t secret, int64_t x1, int64_t x2, int64_t x3, int64_t x4) {
3939
return price;
4040
}
4141
}
42-
return -1;
42+
return std::nullopt;
4343
}
4444

4545
int64_t score(std::vector<int64_t> input, int64_t x1, int64_t x2, int64_t x3, int64_t x4) {
4646
int64_t sum = 0;
4747
for (int64_t n : input) {
48-
int64_t price = monkey(n, x1, x2, x3, x4);
49-
if (price > 0) {
50-
sum += price;
48+
std::optional<int64_t> price = monkey(n, x1, x2, x3, x4);
49+
if (price.has_value()) {
50+
sum += *price;
5151
}
5252
}
5353
return sum;

day22/src/day22.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ int prng(int secret, int n) {
2020
return secret;
2121
}
2222

23-
int monkey(int secret, int x1, int x2, int x3, int x4) {
23+
int? monkey(int secret, int x1, int x2, int x3, int x4) {
2424
int? d1 = null, d2 = null, d3 = null, d4 = null;
2525
for (var i = 0; i < LIMIT; i++) {
2626
int lastPrice = secret % 10;
@@ -31,14 +31,14 @@ int monkey(int secret, int x1, int x2, int x3, int x4) {
3131
return price;
3232
}
3333
}
34-
return -1;
34+
return null;
3535
}
3636

3737
int score(List<int> input, int x1, int x2, int x3, int x4) {
3838
int sum = 0;
3939
for (int n in input) {
40-
int price = monkey(n, x1, x2, x3, x4);
41-
if (price > 0) {
40+
int? price = monkey(n, x1, x2, x3, x4);
41+
if (price != null) {
4242
sum += price;
4343
}
4444
}

0 commit comments

Comments
 (0)