diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-01-13 17:49:18 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-01-13 17:49:18 +0800 |
commit | 69f52cc26954094f3725ec63597c2a43ada51d69 (patch) | |
tree | 6e132eeeda05b18b427ff43181acf74f3c031ffe /src/2022/day19/aoc.cpp | |
parent | 5b4403c08297c5742a7cef0434902f390be505f1 (diff) | |
download | advent-of-code-69f52cc26954094f3725ec63597c2a43ada51d69.tar.gz advent-of-code-69f52cc26954094f3725ec63597c2a43ada51d69.zip |
2022 day19 part1 need
Diffstat (limited to 'src/2022/day19/aoc.cpp')
-rw-r--r-- | src/2022/day19/aoc.cpp | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/src/2022/day19/aoc.cpp b/src/2022/day19/aoc.cpp index 7124873..7ccac05 100644 --- a/src/2022/day19/aoc.cpp +++ b/src/2022/day19/aoc.cpp @@ -1,4 +1,5 @@ #include "aoc.h" +#include <set> namespace aoc2022 { @@ -17,9 +18,9 @@ void print_result(int i, int m, const build_result& r) { bool can_build(int i, const build_result& r, const blueprint& b) { switch (i) { case 0: - return r.products[0] >= b.c_ore_r; + return r.products[0] >= b.c_ore_r[0]; case 1: - return r.products[0] >= b.c_clay_r; + return r.products[0] >= b.c_clay_r[0]; case 2: return r.products[0] >= b.c_obsi_r[0] && r.products[1] >= b.c_obsi_r[1]; case 3: @@ -33,11 +34,11 @@ bool can_build(int i, const 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; + r.products[0] -= b.c_ore_r[0]; r.robots[0] += 1; break; case 1: - r.products[0] -= b.c_clay_r; + r.products[0] -= b.c_clay_r[0]; r.robots[1] += 1; break; case 2: @@ -62,22 +63,40 @@ void build_product(build_result& r, int x) { } } -std::vector<int> to_build(const blueprint& b, const build_result& r) { - std::vector<int> xs; - for (int i = 3; i >= 0; i--) { - if (can_build(i, r, b)) { - xs.push_back(i); +int required(const blueprint& b, int i) { + switch (i) { + case 0: + return 0; + case 1: + return 0; + case 2: + return b.c_obsi_r[1]; + case 3: + return b.c_geod_r[1]; + default: + break; + } + return 0; +} + +std::set<int> to_build(const blueprint& b, const build_result& r) { + int need{3}; // 0, 1, 2, 3 + while (need > 0) { + auto c = can_build(need, r, b); + if (c || (r.products[need - 1] >= required(b, need))) { + break; } + need--; } - xs.push_back(INT32_MAX); - return xs; + // printf("need %d\n", need); + return can_build(need, r, b) ? std::set<int>{need} : std::set<int>{INT32_MAX}; } void build(int m, const blueprint& b, build_result r, build_result& max) { if (m > 0) { // print_result(b.idx, 24 - m, r); auto is = to_build(b, r); - for (int i : is) { + for (auto i : is) { if (i != INT32_MAX) { auto r0 = build_robot(i, r, b); build_product(r0, i); |