aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day16/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-12-17 10:45:55 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-12-17 10:45:55 +0800
commit8749bd4dfcfc86f2f99b5480ec0c289166da8c15 (patch)
tree8eded5fddbd2c4ce5dd712e0d7d91c94c36a2c77 /src/2022/day16/aoc.cpp
parent5ef6bab90d7e268ef6831ca0c05a9aa8fba4d3e2 (diff)
downloadadvent-of-code-8749bd4dfcfc86f2f99b5480ec0c289166da8c15.tar.gz
advent-of-code-8749bd4dfcfc86f2f99b5480ec0c289166da8c15.zip
2022 day17 setup
Diffstat (limited to 'src/2022/day16/aoc.cpp')
-rw-r--r--src/2022/day16/aoc.cpp53
1 files changed, 29 insertions, 24 deletions
diff --git a/src/2022/day16/aoc.cpp b/src/2022/day16/aoc.cpp
index 68213e8..17fd268 100644
--- a/src/2022/day16/aoc.cpp
+++ b/src/2022/day16/aoc.cpp
@@ -1,6 +1,7 @@
#include "aoc.h"
#include <algorithm>
#include <iostream>
+#include <set>
#include <vector>
namespace aoc2022 {
@@ -12,43 +13,41 @@ static valve* get(line_view lv) {
return p.first->second;
}
-std::pair<int, size_t> get_opened() {
+std::pair<int, size_t> get_opened(std::set<valve*>& opened) {
int total{0};
size_t n{0};
- for (auto& kv : valves) {
- if (kv.second->open) {
- total += kv.second->rate;
- n += 1;
- }
+ for (auto& v : opened) {
+ total += v->rate;
+ n += 1;
}
return {total, n};
}
-static char* indent(int s) {
- static char space[100] = {0};
- memset(space, 0, 100);
- for (int i = 0; i < s; i++) {
- space[i] = ' ';
- }
- return space;
-}
+// static char* indent(int s) {
+// static char space[100] = {0};
+// memset(space, 0, 100);
+// for (int i = 0; i < s; i++) {
+// space[i] = ' ';
+// }
+// return space;
+// }
-void flow(int minute, valve* v, int total, int* maxtotal) {
- auto p = get_opened();
+void flow(int minute, valve* v, std::set<valve*>& opened, int total, int* maxtotal) {
+ auto p = get_opened(opened);
total += p.first;
- std::cout << indent(minute) << v->name;
- printf(": %d %zu %d\n", p.first, p.second, total);
+ // std::cout << indent(minute) << v->name;
+ // printf(": %d %zu %d\n", p.first, p.second, total);
if (minute >= 30) {
if (total > *maxtotal) {
*maxtotal = total;
}
} else {
if (p.second == maxopened) {
- flow(minute + 1, v, total, maxtotal);
+ flow(minute + 1, v, opened, total, maxtotal);
} else {
if (v->rate == 0 || v->open) {
for (auto& o : v->others) {
- flow(minute + 1, get(o), total, maxtotal);
+ flow(minute + 1, get(o), opened, total, maxtotal);
}
} else { // v-rate > 0 && v->open == false
auto others = v->others;
@@ -56,9 +55,13 @@ void flow(int minute, valve* v, int total, int* maxtotal) {
for (auto& o : others) {
if (o == v->name) { // choose open
get(o)->open = true;
- minute += 1;
+ opened.insert(v);
+ flow(minute + 2, get(o), opened, total, maxtotal);
+ }
+ else {
+ auto os = opened;
+ flow(minute + 1, get(o), os, total, maxtotal);
}
- flow(minute + 1, get(o), total, maxtotal);
}
}
}
@@ -94,10 +97,12 @@ std::pair<int, int> day16(line_view file) {
}
int max{0};
- flow(1, get("AA"), 0, &max);
+ std::set<valve*> opened;
+ flow(1, get("AA"), opened, 0, &max);
for (auto& vm : vs) {
- flow(1, vm.v, 0, &vm.max);
+ std::set<valve*> os;
+ flow(1, vm.v, os, 0, &vm.max);
}
std::sort(vs.begin(), vs.end(), [](vmax v1, vmax v2) { return v1.max > v2.max; });