aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day16/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-11 22:17:30 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-11 22:17:30 +0800
commit4533046d103ebdbd20b676c9488d8192141a4372 (patch)
tree11540609d9c8e12576b40a81ece9389a63c30858 /src/2022/day16/aoc.cpp
parent28bc467fb9b92866c84f1601f1c3e7f76e9040de (diff)
downloadadvent-of-code-4533046d103ebdbd20b676c9488d8192141a4372.tar.gz
advent-of-code-4533046d103ebdbd20b676c9488d8192141a4372.zip
2022 day16 part2
Diffstat (limited to 'src/2022/day16/aoc.cpp')
-rw-r--r--src/2022/day16/aoc.cpp32
1 files changed, 20 insertions, 12 deletions
diff --git a/src/2022/day16/aoc.cpp b/src/2022/day16/aoc.cpp
index 704d6e2..fd911db 100644
--- a/src/2022/day16/aoc.cpp
+++ b/src/2022/day16/aoc.cpp
@@ -122,12 +122,11 @@ std::vector<valve_m> cango(valve* v, int m, const std::map<valve*, int>& opened)
}
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);
- }
+ auto m = std::min(vs[0].m, vs[1].m);
+ update_total(&total, opened, m);
if (vs[0].m == 0 || vs[1].m == 0) {
if (*max < total) {
- // printf("total is %d\n", total);
+ printf("total is %d\n", total);
*max = total;
}
} else {
@@ -153,28 +152,37 @@ void flow(valve_m vs[2], std::map<valve_mm, int>& visited, std::map<valve*, int>
os += 1;
vs[i].m -= 1;
}
+ // std::cout << vs[i].v->name << " opened with " << vs[i].m << " minutes left" << std::endl;
opened.insert({vs[i].v, vs[i].m});
}
}
if (os >= maxopened) {
- vs[0].m -= vs[0].m > 0 ? 1 : 0;
- vs[1].m -= vs[1].m > 0 ? 1 : 0;
+ // vs[0].m -= vs[0].m > 0 ? 1 : 0;
+ // vs[1].m -= vs[1].m > 0 ? 1 : 0;
flow(vs, visited, opened, os, total, max);
} else {
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);
+ valve_m vx[2];
+ vx[0] = vs[0];
+ vx[1] = vs[1];
+
+ vx[1].m -= n.m;
+ vx[1].v = n.v;
+ flow(vx, 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);
+ valve_m vx[2];
+ vx[0] = vs[0];
+ vx[1] = vs[1];
+
+ vx[0].m -= n.m;
+ vx[0].v = n.v;
+ flow(vx, visited, opened, os, total, max);
}
}
if (ns0.size() > 0 && ns1.size() > 0) {