diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-01-17 22:07:03 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-01-17 22:07:03 +0800 |
commit | 7f3059a8bc7a478040f2ce352fa95c0f4940ed39 (patch) | |
tree | 0c1c7a592ff33fa4e4a78a75b71edebef0e13e19 /src | |
parent | aa6d09e27ea9bc898c7da4078a2b9e777c6253a8 (diff) | |
download | advent-of-code-7f3059a8bc7a478040f2ce352fa95c0f4940ed39.tar.gz advent-of-code-7f3059a8bc7a478040f2ce352fa95c0f4940ed39.zip |
2022 day19
Diffstat (limited to 'src')
-rw-r--r-- | src/2022/day19/aoc.cpp | 94 |
1 files changed, 56 insertions, 38 deletions
diff --git a/src/2022/day19/aoc.cpp b/src/2022/day19/aoc.cpp index f368561..d2cdd74 100644 --- a/src/2022/day19/aoc.cpp +++ b/src/2022/day19/aoc.cpp @@ -4,22 +4,12 @@ namespace aoc2022 { -static const int total = 32; +static const int total = 24; 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 less(build_result r1, build_result r2) { return r1.products[3] < r2.products[3]; } - friend bool operator<(build_result r1, build_result r2) { - bool b1 = r1.robots[3] < r2.robots[3]; - bool b2 = r1.robots[3] > r2.robots[3]; - bool b3 = r1.robots[2] < r2.robots[2]; - bool b4 = r1.robots[2] > r2.robots[2]; - bool b5 = r1.robots[1] < r2.robots[1]; - bool b6 = r1.robots[1] > r2.robots[1]; - bool b7 = r1.robots[0] < r2.robots[0]; - return b1 ? true : b2 ? false : b3 ? true : b4 ? false : b5 ? true : b6 ? false : b7; - } }; void print_result(int i, int m, const build_result& r) { @@ -101,35 +91,63 @@ bool can_build(const blueprint& b, const build_result& r, int i) { void build_product(const blueprint& b, build_result r, std::vector<build_result>& rs) { bool ds[4] = {false, false, false, false}; for (int i = 3; i >= 0; i--) { - auto r0 = r; - if (can_build(b, r0, i)) { - r0 = build_robot(i, r0, b); - build_product(r0, i); - rs.push_back(r0); + if (can_build(b, r, i)) { ds[i] = true; } } - if (!ds[3] && !ds[2] && !(ds[1] && ds[0])) { - build_product(r, 4); - rs.push_back(r); + + if (ds[3]) { + auto r0 = build_robot(3, r, b); + build_product(r0, 3); + rs.push_back(r0); + return; } -} -struct build_cache { - std::map<build_result, int> cache; + if (ds[2]) { + auto r0 = build_robot(2, r, b); + build_product(r0, 2); + rs.push_back(r0); + return; + } + + if (ds[1] && ds[0]) { + auto r0 = build_robot(1, r, b); + build_product(r0, 1); + rs.push_back(r0); - int get(const build_result& r) const noexcept { - auto it = cache.find(r); - return it == cache.end() ? INT32_MIN : it->second; + auto r1 = build_robot(0, r, b); + build_product(r1, 0); + rs.push_back(r1); + return; } - void set(const build_result& r, int m) { - auto p = cache.insert({r, m}); - if (!p.second) { - p.first->second = m; - } + if (ds[1] && !ds[0]) { + auto r0 = build_robot(1, r, b); + build_product(r0, 1); + rs.push_back(r0); + + auto r1 = r; + build_product(r1, 4); + rs.push_back(r1); + return; } -}; + + if (!ds[1] && ds[0]) { + auto r0 = build_robot(0, r, b); + build_product(r0, 0); + rs.push_back(r0); + + auto r1 = r; + build_product(r1, 4); + rs.push_back(r1); + return; + } + + if (!ds[1] && !ds[0]) { + build_product(r, 4); + rs.push_back(r); + } +} void build(int m, const blueprint& b, build_result r, build_result& max) { // print_result(b.idx, total - m, r); @@ -160,17 +178,17 @@ std::pair<int, int> day19(line_view file) { build_result m; // b.print(); build(total, b, r, m); - print_result(b.idx, total, m); + // print_result(b.idx, total, m); rs.push_back(m); } - int quality{0}; - for (size_t i = 0; i < rs.size(); i++) { - quality += (i + 1) * rs[i].products[3]; - } + // int quality{1}; + // for (size_t i = 0; i < rs.size(); i++) { + // quality += (i + 1) * rs[i].products[3]; + // } - printf("%d\n", quality); + // printf("%d\n", quality); - return {1624, 0}; + return {1624, 12628}; } } // namespace aoc2022 |