aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-18 22:53:42 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-18 22:53:42 +0800
commit70f20b9317a11f443d01b4863969857173c63f14 (patch)
tree73ad7eda461a00a4e4293c867e650d31f26ffcb3
parent0ef81936a3d4925db0d1e809a946153b33e65343 (diff)
downloadadvent-of-code-70f20b9317a11f443d01b4863969857173c63f14.tar.gz
advent-of-code-70f20b9317a11f443d01b4863969857173c63f14.zip
2017 day7
-rw-r--r--src/2017/day7/aoc.cpp24
-rw-r--r--src/2017/day7/aoc.h1
-rw-r--r--src/2017/day7/input013
-rw-r--r--test/test_2017.cpp2
4 files changed, 40 insertions, 0 deletions
diff --git a/src/2017/day7/aoc.cpp b/src/2017/day7/aoc.cpp
index 86e2e45..32a59ed 100644
--- a/src/2017/day7/aoc.cpp
+++ b/src/2017/day7/aoc.cpp
@@ -27,6 +27,27 @@ void depth(disc* d, int* dh) {
}
}
+void weight(disc* d, int* w) {
+ int total{0};
+ for (disc* x : d->subs) {
+ weight(x, &x->total);
+ total += x->total;
+ }
+ *w += d->weight + total;
+}
+
+void print(disc* d, int depth) {
+ for (int i = 0; i < depth; i++) {
+ printf(" ");
+ }
+ std::cout << d->name << "(" << d->total << "," << d->weight << ")" << std::endl;
+ if (depth < 3) {
+ for (disc* x : d->subs) {
+ print(x, depth + 1);
+ }
+ }
+}
+
void day7(line_view file, char name[]) {
std::unordered_map<line_view, disc*> ds;
per_line(file, [&ds](line_view lv) {
@@ -52,6 +73,9 @@ void day7(line_view file, char name[]) {
name[i++] = c;
return true;
});
+
+ weight(ds[x], &ds[x]->total);
+ // print(ds[x], 0);
}
} // namespace aoc2017
diff --git a/src/2017/day7/aoc.h b/src/2017/day7/aoc.h
index f3b9104..1a5a7d9 100644
--- a/src/2017/day7/aoc.h
+++ b/src/2017/day7/aoc.h
@@ -7,6 +7,7 @@ namespace aoc2017 {
struct disc {
line_view name;
int weight;
+ int total = 0;
std::vector<disc*> subs;
const char* xp = nullptr;
diff --git a/src/2017/day7/input0 b/src/2017/day7/input0
new file mode 100644
index 0000000..8a41324
--- /dev/null
+++ b/src/2017/day7/input0
@@ -0,0 +1,13 @@
+pbga (66)
+xhth (57)
+ebii (61)
+havc (66)
+ktlj (57)
+fwft (72) -> ktlj, cntj, xhth
+qoyq (66)
+padx (45) -> pbga, havc, qoyq
+tknk (41) -> ugml, padx, fwft
+jptl (61)
+ugml (68) -> gyxo, ebii, jptl
+gyxo (61)
+cntj (57)
diff --git a/test/test_2017.cpp b/test/test_2017.cpp
index 694899d..f5f3c5b 100644
--- a/test/test_2017.cpp
+++ b/test/test_2017.cpp
@@ -62,8 +62,10 @@ TEST_CASE("Memory Reallocation", "[2017]") {
}
TEST_CASE("Recursive Circus", "[2017]") {
+ // line_view lv = load_file("../src/2017/day7/input0");
line_view lv = load_file("../src/2017/day7/input");
char x[10] = {0};
aoc2017::day7(lv, x);
+ // REQUIRE(strcmp(x, "tknk") == 0);
REQUIRE(strcmp(x, "svugo") == 0);
}