aboutsummaryrefslogtreecommitdiff
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
parent5ef6bab90d7e268ef6831ca0c05a9aa8fba4d3e2 (diff)
downloadadvent-of-code-8749bd4dfcfc86f2f99b5480ec0c289166da8c15.tar.gz
advent-of-code-8749bd4dfcfc86f2f99b5480ec0c289166da8c15.zip
2022 day17 setup
-rw-r--r--src/2022/day16/aoc.cpp53
-rw-r--r--src/2022/day17/README.md0
-rw-r--r--src/2022/day17/aoc.cpp9
-rw-r--r--src/2022/day17/aoc.h7
-rw-r--r--src/2022/day17/input0
-rw-r--r--src/2022/day17/input00
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--test/test_2022.cpp14
8 files changed, 57 insertions, 27 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; });
diff --git a/src/2022/day17/README.md b/src/2022/day17/README.md
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/2022/day17/README.md
diff --git a/src/2022/day17/aoc.cpp b/src/2022/day17/aoc.cpp
new file mode 100644
index 0000000..c35000b
--- /dev/null
+++ b/src/2022/day17/aoc.cpp
@@ -0,0 +1,9 @@
+#include "aoc.h"
+
+namespace aoc2022 {
+
+std::pair<int, int> day17(line_view) {
+ return {0, 0};
+}
+}
+
diff --git a/src/2022/day17/aoc.h b/src/2022/day17/aoc.h
new file mode 100644
index 0000000..d60e69a
--- /dev/null
+++ b/src/2022/day17/aoc.h
@@ -0,0 +1,7 @@
+#include "common.h"
+#include <vector>
+
+namespace aoc2022 {
+
+std::pair<int, int> day17(line_view);
+}
diff --git a/src/2022/day17/input b/src/2022/day17/input
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/2022/day17/input
diff --git a/src/2022/day17/input0 b/src/2022/day17/input0
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/2022/day17/input0
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 4e17647..5b9c91a 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -101,6 +101,7 @@ set(SOLUTION_FILES
"2022/day14/aoc.cpp"
"2022/day15/aoc.cpp"
"2022/day16/aoc.cpp"
+ "2022/day17/aoc.cpp"
)
add_library(solution SHARED ${SOLUTION_FILES})
diff --git a/test/test_2022.cpp b/test/test_2022.cpp
index 04be125..ba5ebef 100644
--- a/test/test_2022.cpp
+++ b/test/test_2022.cpp
@@ -14,6 +14,7 @@
#include "2022/day14/aoc.h"
#include "2022/day15/aoc.h"
#include "2022/day16/aoc.h"
+#include "2022/day17/aoc.h"
#include "catch.hpp"
#include <stdio.h>
#include <string.h>
@@ -125,9 +126,16 @@ TEST_CASE("Beacon Exclusion Zone", "[2022]") {
REQUIRE(0 == p.second);
}
-TEST_CASE("Proboscidea Volcanium", "[2022]") {
- line_view lv = load_file("../src/2022/day16/input0");
- auto p = aoc2022::day16(lv);
+//TEST_CASE("Proboscidea Volcanium", "[2022]") {
+// line_view lv = load_file("../src/2022/day16/input0");
+// auto p = aoc2022::day16(lv);
+// REQUIRE(0 == p.first);
+// REQUIRE(0 == p.second);
+//}
+
+TEST_CASE("", "[2022]") {
+ line_view lv = load_file("../src/2022/day17/input0");
+ auto p = aoc2022::day17(lv);
REQUIRE(0 == p.first);
REQUIRE(0 == p.second);
}