aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-13 12:48:47 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-13 12:48:47 +0800
commit375cac70cf7bd7e6018cdc7199f83865f9eec784 (patch)
tree8163f835480b5cad49e481390b3508cffeb10656 /src
parent7009f3ed3c6f9938f6a6b80785e4df9d38d4ec03 (diff)
downloadadvent-of-code-375cac70cf7bd7e6018cdc7199f83865f9eec784.tar.gz
advent-of-code-375cac70cf7bd7e6018cdc7199f83865f9eec784.zip
2022 day19 part1
Diffstat (limited to 'src')
-rw-r--r--src/2022/day19/aoc.cpp76
-rw-r--r--src/2022/day19/aoc.h2
2 files changed, 76 insertions, 2 deletions
diff --git a/src/2022/day19/aoc.cpp b/src/2022/day19/aoc.cpp
index 60acb2b..85e0767 100644
--- a/src/2022/day19/aoc.cpp
+++ b/src/2022/day19/aoc.cpp
@@ -2,6 +2,63 @@
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
+};
+
+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| robots: %d %d %d %d\n", i, m, r.robots[0], r.robots[1], r.robots[2], r.robots[3]);
+}
+
+bool can_build(int i, const build_result& r, const blueprint& b) {
+ switch (i) {
+ case 0:
+ return r.products[0] >= b.c_ore_r;
+ case 1:
+ return r.products[0] >= b.c_clay_r;
+ case 2:
+ return r.products[0] >= b.c_obsi_r[0] && r.products[1] >= b.c_obsi_r[1];
+ case 3:
+ return r.products[0] >= b.c_geod_r[0] && r.products[2] >= b.c_geod_r[1];
+ default:
+ break;
+ }
+ return false;
+}
+
+void build_robot(int i, build_result& r, const blueprint& b) {
+ switch (i) {
+ case 0:
+ r.products[0] -= b.c_ore_r;
+ r.robots[0] += 1;
+ break;
+ case 1:
+ r.products[0] -= b.c_clay_r;
+ r.robots[1] += 1;
+ break;
+ case 2:
+ r.products[0] -= b.c_obsi_r[0];
+ r.products[1] -= b.c_obsi_r[1];
+ r.robots[2] += 1;
+ break;
+ case 3:
+ r.products[0] -= b.c_geod_r[0];
+ r.products[2] -= b.c_geod_r[1];
+ r.robots[3] += 1;
+ break;
+ default:
+ break;
+ }
+}
+
+void build_product(build_result& r, bool bs[4]) {
+ for (int i = 0; i < 4; i++) {
+ r.products[i] += r.robots[i] - (int) bs[i];
+ }
+}
+
std::pair<int, int> day19(line_view file) {
std::vector<blueprint> bs;
per_line(file, [&bs](line_view lv) {
@@ -9,8 +66,23 @@ std::pair<int, int> day19(line_view file) {
return true;
});
- for (auto& b : bs) {
- b.print();
+ std::vector<build_result> rs;
+ for (size_t i = 0; i < bs.size(); i++) {
+ 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);
}
return {0, 0};
diff --git a/src/2022/day19/aoc.h b/src/2022/day19/aoc.h
index f2b2b8b..9b08565 100644
--- a/src/2022/day19/aoc.h
+++ b/src/2022/day19/aoc.h
@@ -5,6 +5,8 @@ namespace aoc2022 {
// which blueprint would maximize the number of opened geodes after 24 minutes
// by figuring out which robots to build and when to build them.
+// geod <- obsi <- clay <- ore
+// ore ore
struct blueprint {
int idx = 0;
int c_ore_r = 0; // ore