aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day19/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2022/day19/aoc.cpp')
-rw-r--r--src/2022/day19/aoc.cpp56
1 files changed, 37 insertions, 19 deletions
diff --git a/src/2022/day19/aoc.cpp b/src/2022/day19/aoc.cpp
index 85e0767..f5ea6a1 100644
--- a/src/2022/day19/aoc.cpp
+++ b/src/2022/day19/aoc.cpp
@@ -5,10 +5,12 @@ namespace aoc2022 {
struct build_result {
int products[4] = {0, 0, 0, 0}; // ore,clay,obsi,geod
int robots[4] = {1, 0, 0, 0}; // ore,clay,obsi,geod
+ friend bool operator<(build_result r1, build_result r2) { return r1.products[3] < r2.products[3]; }
};
void print_result(int i, int m, const build_result& r) {
- printf("%d %02d| ore[%d] clay[%d] obsi[%d] geod[%d]\n", i, m, r.products[0], r.products[1], r.products[2], r.products[3]);
+ printf("%d %02d| ore[%d] clay[%d] obsi[%d] geod[%d]\n", i, m, r.products[0], r.products[1], r.products[2],
+ r.products[3]);
printf("%d %02d| robots: %d %d %d %d\n", i, m, r.robots[0], r.robots[1], r.robots[2], r.robots[3]);
}
@@ -28,7 +30,7 @@ bool can_build(int i, const build_result& r, const blueprint& b) {
return false;
}
-void build_robot(int i, build_result& r, const blueprint& b) {
+build_result build_robot(int i, build_result r, const blueprint& b) {
switch (i) {
case 0:
r.products[0] -= b.c_ore_r;
@@ -51,11 +53,36 @@ void build_robot(int i, build_result& r, const blueprint& b) {
default:
break;
}
+ return r;
}
-void build_product(build_result& r, bool bs[4]) {
+void build_product(build_result& r, int x) {
for (int i = 0; i < 4; i++) {
- r.products[i] += r.robots[i] - (int) bs[i];
+ r.products[i] += r.robots[i] - ((int)i == x);
+ }
+}
+
+int to_build(const blueprint& b, const build_result& r) {
+ int i = 3;
+ return i;
+}
+
+void build(int m, const blueprint& b, build_result r, build_result& max) {
+ if (m > 0) {
+ // print_result(b.idx, 24 - m, r);
+ int i = to_build(b, r);
+ if (i != INT32_MAX) {
+ auto r0 = build_robot(i, r, b);
+ build_product(r0, i);
+ build(m - 1, b, r0, max);
+ } else {
+ build_product(r, 5);
+ build(m - 1, b, r, max);
+ }
+ } else {
+ if (max < r) {
+ max = r;
+ }
}
}
@@ -67,22 +94,13 @@ std::pair<int, int> day19(line_view file) {
});
std::vector<build_result> rs;
- for (size_t i = 0; i < bs.size(); i++) {
+ for (auto& b : bs) {
build_result r;
- int n = 24;
- bs[i].print();
- while (n-- > 0) {
- bool bss[4] = {false, false, false, false};
- for (int j = 3; j >= 0; j--) {
- if (can_build(j, r, bs[i])) {
- bss[j] = true;
- build_robot(j, r, bs[i]);
- }
- }
- build_product(r, bss);
- print_result(bs[i].idx, 24 - n, r);
- }
- rs.push_back(r);
+ build_result m;
+ // b.print();
+ build(24, b, r, m);
+ print_result(b.idx, 24, m);
+ rs.push_back(m);
}
return {0, 0};