aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/2021/day4/README.md7
-rw-r--r--src/2021/day4/aoc.cpp30
2 files changed, 27 insertions, 10 deletions
diff --git a/src/2021/day4/README.md b/src/2021/day4/README.md
index 87a7c1e..cb957b6 100644
--- a/src/2021/day4/README.md
+++ b/src/2021/day4/README.md
@@ -53,4 +53,11 @@ The score of the winning board can now be calculated. Start by finding the sum o
To guarantee victory against the giant squid, figure out which board will win first. What will your final score be if you choose that board?
+--- Part Two ---
+On the other hand, it might be wise to try a different strategy: let the giant squid win.
+You aren't sure how many bingo boards a giant squid could play at once, so rather than waste time counting its arms, the safe thing to do is to figure out which board will win last and choose that one. That way, no matter which boards it picks, it will win for sure.
+
+In the above example, the second board is the last to win, which happens after 13 is eventually called and its middle column is completely marked. If you were to keep playing until this point, the second board would have a sum of unmarked numbers equal to 148 for a final score of 148 * 13 = 1924.
+
+Figure out which board will win last. Once it wins, what would its final score be?
diff --git a/src/2021/day4/aoc.cpp b/src/2021/day4/aoc.cpp
index c38a019..fc18c1e 100644
--- a/src/2021/day4/aoc.cpp
+++ b/src/2021/day4/aoc.cpp
@@ -78,37 +78,35 @@ struct board {
p++;
}
at = bingo_at();
- print();
+ //print();
}
void print() const noexcept {
int i{0};
for (auto& b : is) {
- // printf("{%02d,%02d}", b.v, b.i);
- printf("{%02d}", b.v);
+ printf("{%02d,%02d}", b.v, b.i);
if (++i % 5 == 0) {
printf("\n");
} else {
printf(" ");
}
}
- printf("-> %d %d\n", at.i, at.v);
+ printf("-> %d %d\n", at.v, at.i);
}
int sum_unmarked(int x) {
int s{0};
for (auto& n : is) {
if (n.i > x) {
+ // printf("%d ", n.v);
s += n.v;
}
}
return s;
}
- bool operator<(const board& b) const noexcept { return at < b.at; }
+ bool operator<(const board& b) const noexcept { return at.i < b.at.i; }
bnum bingo_at() const noexcept {
- bnum b{INT32_MAX, 0};
-
auto is_bingo_r = [this](int y) -> bnum {
int m{INT32_MIN};
int v{0};
@@ -123,6 +121,7 @@ struct board {
}
}
}
+ // printf("row %d is {%d, %d}\n", y, v, m);
return {m, v};
};
@@ -140,13 +139,22 @@ struct board {
}
}
}
+ // printf("col %d is {%d, %d}\n", x, v, m);
return {m, v};
};
+ bnum b{INT32_MAX, 0};
for (int i : {0, 1, 2, 3, 4}) {
bnum b1 = is_bingo_r(i);
bnum b2 = is_bingo_c(i);
- b = std::min(b, std::min(b1, b2));
+
+ bnum bx{0, 0};
+ bx.i = b1.i < b2.i ? b1.i : b2.i;
+ bx.v = b1.i < b2.i ? b1.v : b2.v;
+
+ if (bx.i < b.i) {
+ b = bx;
+ }
}
return b;
@@ -173,8 +181,10 @@ int day4(line_view file) {
bs.emplace_back(line_view{p1, p}, &b);
std::sort(bs.begin(), bs.end());
- printf("%d\n", bs[0].sum_unmarked(bs[0].at.i));
- return 0;
+ // for (auto& b : bs) {
+ // printf("%d, %d\n", b.at.i, b.sum_unmarked(b.at.i) * b.at.v);
+ // }
+ return bs[0].sum_unmarked(bs[0].at.i) * bs[0].at.v;
}
} // namespace aoc2021