aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day16/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-10 15:53:45 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-10 15:53:45 +0800
commit8b39eae89438440c76ea2fb0dbe83f261672756e (patch)
tree36ab6227e56c781d313f8c525e2b67502cd41001 /src/2022/day16/aoc.cpp
parent235b939496f887021b88b2f36964cfb765389886 (diff)
downloadadvent-of-code-8b39eae89438440c76ea2fb0dbe83f261672756e.tar.gz
advent-of-code-8b39eae89438440c76ea2fb0dbe83f261672756e.zip
2022 day16 part1 too many
Diffstat (limited to 'src/2022/day16/aoc.cpp')
-rw-r--r--src/2022/day16/aoc.cpp56
1 files changed, 14 insertions, 42 deletions
diff --git a/src/2022/day16/aoc.cpp b/src/2022/day16/aoc.cpp
index b19eae5..658357c 100644
--- a/src/2022/day16/aoc.cpp
+++ b/src/2022/day16/aoc.cpp
@@ -52,60 +52,32 @@ void rank_neighbours(valve* v, std::map<valve*, int>& visited) {
}
}
-void update_total(int m, int* total, std::map<int, std::set<valve*>>& opened) {
- for (auto& kv : opened) {
- if (m > kv.first) {
- for (valve* v : kv.second) {
- *total += (m - kv.first) * v->rate;
- }
- }
- }
-}
-
-bool is_opened(valve* v, int m, std::map<int, std::set<valve*>> opened) {
- for (auto& kv : opened) {
- if (kv.first < m) {
- if (kv.second.find(v) != kv.second.end()) {
- return true;
- }
- }
+void update_total(int* total, std::set<valve*>& opened) {
+ for (valve* v : opened) {
+ *total += v->rate;
}
- return false;
}
// m minutes left at v
-void flow(int m, valve* v, std::map<int, std::set<valve*>> opened, int total, int* max) {
+void flow(int m, valve* v, std::set<valve*> opened, int total, int* max) {
if (m == 0) {
if (*max < total) {
*max = total;
}
} else {
- update_total(m, &total, opened);
- auto p = opened.insert({m, {}});
- auto& os = p.first->second; // std::set<valve*>
- if (os.size() >= maxopened) {
+ update_total(&total, opened);
+ std::cout << "at " << v->name << " when " << m << " minutes left, total is " << total << std::endl;
+ if (opened.size() >= maxopened) {
flow(m - 1, v, opened, total, max);
} else {
- if (!p.second) {
- if (os.find(v) == os.end()) {
- if (v->rate > 0) {
- m -= 1;
- os.insert(v);
- }
- } else {
- // already done this
- return;
- }
- } else {
- if (v->rate > 0) {
- m -= 1;
- os.insert(v);
- }
+ if (v->rate > 0 && opened.find(v) == opened.end()) {
+ m -= 1;
+ opened.insert(v);
}
+
// choose next
- auto& ns = diagram[v];
- for (auto& n : ns) {
- if (m > n.m && !is_opened(n.v, m, opened)) {
+ for (auto& n : diagram[v]) {
+ if (m >= n.m) {
flow(m - n.m, n.v, opened, total, max);
}
}
@@ -148,7 +120,7 @@ std::pair<int, int> day16(line_view file) {
// }
int max{INT32_MIN};
- std::map<int, std::set<valve*>> opened;
+ std::set<valve*> opened;
flow(30, get("AA"), opened, 0, &max);
printf("%d\n", max);