diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-19 11:34:13 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-19 11:34:13 +0800 |
commit | 23b38a4aae582af3a48d80a14b9b277ffc6a476b (patch) | |
tree | 14b820f98a8642547001d7bfb801d2d8d95a470c | |
parent | 8b657a93954bca14c1e16b713c76357734d8c761 (diff) | |
download | advent-of-code-23b38a4aae582af3a48d80a14b9b277ffc6a476b.tar.gz advent-of-code-23b38a4aae582af3a48d80a14b9b277ffc6a476b.zip |
test day12 part2
-rw-r--r-- | src/2015/day12/aoc.cpp | 64 | ||||
-rw-r--r-- | src/2015/day12/aoc.h | 1 | ||||
-rw-r--r-- | test/test_2015.cpp | 1 |
3 files changed, 60 insertions, 6 deletions
diff --git a/src/2015/day12/aoc.cpp b/src/2015/day12/aoc.cpp index 3b1ed2b..33db5d0 100644 --- a/src/2015/day12/aoc.cpp +++ b/src/2015/day12/aoc.cpp @@ -1,8 +1,9 @@ #include "aoc.h" +#include <stack> namespace aoc2015 { -int parse_day12(const char* p1, const char* p2) { +int get_number(const char* p1, const char* p2) { int sign = *(p1 - 1) == '-' ? -1 : 1; int d{0}; while (p1 != p2) { @@ -12,12 +13,12 @@ int parse_day12(const char* p1, const char* p2) { return sign * d; } -int day12(line_view file) { +int parse_day12(line_view s) { int total = 0; - const char* p1 = file.line; + const char* p1 = s.line; const char* p2 = p1; bool to_parse = false; - while (p2 < file.line + file.length) { + while (p2 < s.line + s.length) { if (*p2 >= '0' && *p2 <= '9') { if (!to_parse) { p1 = p2; @@ -25,16 +26,67 @@ int day12(line_view file) { } } else { if (to_parse) { - total += parse_day12(p1, p2); + total += get_number(p1, p2); to_parse = !to_parse; } } p2++; } if (to_parse) { - total += parse_day12(p1, p2); + total += get_number(p1, p2); } return total; } +int day12(line_view file) { return parse_day12(file); } + +struct day12_object { + const char* p1; + + int fold(const char* p2) { + line_view lv{p1, p2}; + const char* p = lv.contains("red"); + return (p != nullptr) ? 0 : parse_day12(lv); + } +}; + +// {{x} x} = 0 +// {{x} ...} = ... +// {{...} x} = 0 +// {{...} .} = ... + . +int day12_part2(line_view file) { + int total = 0; + std::stack<day12_object> os; + std::stack<int> is; + const char* p = file.line; + const char* p1 = p; + while (p < file.line + file.length) { + if (*p == '{') { + if (os.empty()) { + total += parse_day12({p1, p}); + p1 = p; + } + os.push({p}); + } + if (*p == '}') { + auto o = os.top(); + is.push(o.fold(p)); + os.pop(); + if (os.empty()) { + while (!is.empty()) { + total += is.top(); + is.pop(); + } + p1 = p; + } else { + auto& oo = os.top(); + oo.p1 = p; + } + } + p++; + } + total += parse_day12({p1, p}); + return total; +} + } // namespace aoc2015 diff --git a/src/2015/day12/aoc.h b/src/2015/day12/aoc.h index 66d7164..10114a6 100644 --- a/src/2015/day12/aoc.h +++ b/src/2015/day12/aoc.h @@ -4,5 +4,6 @@ namespace aoc2015 { int day12(line_view file); +int day12_part2(line_view file); } diff --git a/test/test_2015.cpp b/test/test_2015.cpp index f5bfb26..6c23e57 100644 --- a/test/test_2015.cpp +++ b/test/test_2015.cpp @@ -142,4 +142,5 @@ TEST_CASE("Corporate Policy", "[day11]") { TEST_CASE("", "[day12]") { line_view lv = load_file("../src/2015/day12/input"); REQUIRE(156366 == aoc2015::day12(lv)); + printf("%d\n", aoc2015::day12_part2("{{red1} 2 {red3 {4}}} 10")); } |