aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-11 17:52:53 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-11 17:52:53 +0800
commit28bc467fb9b92866c84f1601f1c3e7f76e9040de (patch)
treef361188710ba9e3f1d73614c1d80ceec436ee896 /src
parent4dcceb6cb95970f90bb1a981fc47b02c6a35f580 (diff)
downloadadvent-of-code-28bc467fb9b92866c84f1601f1c3e7f76e9040de.tar.gz
advent-of-code-28bc467fb9b92866c84f1601f1c3e7f76e9040de.zip
2022 day16 part2
Diffstat (limited to 'src')
-rw-r--r--src/2022/day16/aoc.cpp47
1 files changed, 44 insertions, 3 deletions
diff --git a/src/2022/day16/aoc.cpp b/src/2022/day16/aoc.cpp
index d381b6e..704d6e2 100644
--- a/src/2022/day16/aoc.cpp
+++ b/src/2022/day16/aoc.cpp
@@ -63,7 +63,7 @@ void update_total(int* total, std::map<valve*, int>& opened, int m) {
*total = t;
}
-bool is_open(valve* v, std::map<valve*, int>& opened) { return opened.find(v) != opened.end(); }
+bool is_open(valve* v, const std::map<valve*, int>& opened) { return opened.find(v) != opened.end(); }
// m minutes left at v
void flow(int m, valve* v, std::map<valve*, int> opened, int os, int total, int* max) {
@@ -111,6 +111,16 @@ struct valve_mm {
}
};
+std::vector<valve_m> cango(valve* v, int m, const std::map<valve*, int>& opened) {
+ std::vector<valve_m> t;
+ for (auto& vm : diagram[v]) {
+ if (!is_open(vm.v, opened) && m > vm.m) {
+ t.emplace_back(vm);
+ }
+ }
+ return t;
+}
+
void flow(valve_m vs[2], std::map<valve_mm, int>& visited, std::map<valve*, int> opened, int os, int total, int* max) {
for (auto i = 0; i < 2; i++) {
update_total(&total, opened, vs[i].m);
@@ -151,8 +161,39 @@ void flow(valve_m vs[2], std::map<valve_mm, int>& visited, std::map<valve*, int>
vs[1].m -= vs[1].m > 0 ? 1 : 0;
flow(vs, visited, opened, os, total, max);
} else {
- // auto& ns0 = diagram[vs[0].v];
- // auto& ns1 = diagram[vs[1].v];
+ auto ns0 = cango(vs[0].v, vs[0].m, opened);
+ auto ns1 = cango(vs[1].v, vs[1].m, opened);
+ if (ns0.size() == 0 && ns1.size() > 0) {
+ for (auto& n : ns1) {
+ vs[1].m -= n.m;
+ vs[1].v = n.v;
+ flow(vs, visited, opened, os, total, max);
+ }
+ }
+ if (ns0.size() > 0 && ns1.size() == 0) {
+ for (auto& n : ns0) {
+ vs[0].m -= n.m;
+ vs[0].v = n.v;
+ flow(vs, visited, opened, os, total, max);
+ }
+ }
+ if (ns0.size() > 0 && ns1.size() > 0) {
+ for (auto& n0 : ns0) {
+ for (auto& n1 : ns1) {
+ if (n0.v != n1.v) {
+ valve_m vx[2];
+ vx[0] = vs[0];
+ vx[1] = vs[1];
+
+ vx[0].m -= n0.m;
+ vx[0].v = n0.v;
+ vx[1].m -= n1.m;
+ vx[1].v = n1.v;
+ flow(vx, visited, opened, os, total, max);
+ }
+ }
+ }
+ }
}
}
}