aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-12 14:28:01 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-12 14:28:01 +0800
commit97241ea1ee652fdae0f761b3d0338aaed85f5945 (patch)
tree4fa3e65e6fd69330938722d74b5fd825f5a91e53
parent589afb0cf13a8f6c8f7808d3e09b76831d8dba50 (diff)
downloadadvent-of-code-97241ea1ee652fdae0f761b3d0338aaed85f5945.tar.gz
advent-of-code-97241ea1ee652fdae0f761b3d0338aaed85f5945.zip
2022 day16 part2
-rw-r--r--src/2022/day16/aoc.cpp47
1 files changed, 25 insertions, 22 deletions
diff --git a/src/2022/day16/aoc.cpp b/src/2022/day16/aoc.cpp
index 015aa6f..1c767d1 100644
--- a/src/2022/day16/aoc.cpp
+++ b/src/2022/day16/aoc.cpp
@@ -102,14 +102,14 @@ void flow(int m, valve* v, std::map<valve*, int> opened, int os, int total, int*
}
}
-struct valve_mm {
- valve_m vm1;
- valve_m vm2;
-
- friend bool operator<(valve_mm m1, valve_mm m2) {
- return m1.vm1 < m2.vm1 ? true : m1.vm1 > m2.vm1 ? false : m1.vm2 < m2.vm2;
- }
-};
+// struct valve_mm {
+// valve_m vm1;
+// valve_m vm2;
+//
+// friend bool operator<(valve_mm m1, valve_mm m2) {
+// return m1.vm1 < m2.vm1 ? true : m1.vm1 > m2.vm1 ? false : m1.vm2 < m2.vm2;
+// }
+// };
std::vector<valve_m> cango(valve* v, int m0, int m, const std::map<valve*, int>& opened) {
std::vector<valve_m> t;
@@ -121,7 +121,7 @@ std::vector<valve_m> cango(valve* v, int m0, int m, const std::map<valve*, int>&
return t;
}
-void flow(int m, valve_m vs[2], std::map<valve_mm, int>& visited, std::map<valve*, int> opened, int os, int total,
+void flow(int m, valve_m vs[2], std::map<int, int>& visited, std::map<valve*, int> opened, int os, int total,
int* max) {
update_total(&total, opened, m);
if (m == 0) {
@@ -130,14 +130,15 @@ void flow(int m, valve_m vs[2], std::map<valve_mm, int>& visited, std::map<valve
*max = total;
}
} else {
- auto p = visited.insert({{vs[0], vs[1]}, total});
+ auto p = visited.insert({m, total});
if (!p.second) {
- if (p.first->second >= total) {
+ if (p.first->second > total) {
return;
}
p.first->second = total;
}
- std::cout << os << " opened, " << m << "/" << vs[0].m << "/" << vs[1].m << " m left, total " << total << std::endl;
+ // std::cout << os << " opened, " << m << "/" << vs[0].m << "/" << vs[1].m << " m left, total " << total <<
+ // std::endl;
if (os >= maxopened) {
flow(m - 1, vs, visited, opened, os, total, max);
} else {
@@ -186,16 +187,18 @@ void flow(int m, valve_m vs[2], std::map<valve_mm, int>& visited, std::map<valve
if (ns0.size() > 0 && ns1.size() > 0) {
for (auto& n0 : ns0) {
for (auto& n1 : ns1) {
- valve_m vx[2];
- vx[0] = vs[0];
- vx[1] = vs[1];
+ 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;
- auto v = visited;
- flow(m - 1, vx, v, opened, os, total, max);
+ vx[0].m -= n0.m;
+ vx[0].v = n0.v;
+ vx[1].m -= n1.m;
+ vx[1].v = n1.v;
+ auto v = visited;
+ flow(m - 1, vx, v, opened, os, total, max);
+ }
}
}
}
@@ -249,7 +252,7 @@ std::pair<int, int> day16(line_view file) {
{get("AA"), 26},
{get("AA"), 26},
};
- std::map<valve_mm, int> visited;
+ std::map<int, int> visited;
flow(26, vs, visited, opened, 0, 0, &m2);
printf("%d %d\n", m1, m2);