diff options
-rw-r--r-- | src/2017/day9/README.md | 13 | ||||
-rw-r--r-- | src/2017/day9/aoc.cpp | 49 | ||||
-rw-r--r-- | src/2017/day9/aoc.h | 1 | ||||
-rw-r--r-- | test/test_2017.cpp | 17 |
4 files changed, 80 insertions, 0 deletions
diff --git a/src/2017/day9/README.md b/src/2017/day9/README.md index c57880c..162ef71 100644 --- a/src/2017/day9/README.md +++ b/src/2017/day9/README.md @@ -40,3 +40,16 @@ Your goal is to find the total score for all groups in your input. Each group is {{<a!>},{<a!>},{<a!>},{<ab>}}, score of 1 + 2 = 3. What is the total score for all groups in your input? +--- Part Two --- +Now, you're ready to remove the garbage. + +To prove you've removed it, you need to count all of the characters within the garbage. The leading and trailing < and > don't count, nor do any canceled characters or the ! doing the canceling. + +<>, 0 characters. +<random characters>, 17 characters. +<<<<>, 3 characters. +<{!>}>, 2 characters. +<!!>, 0 characters. +<!!!>>, 0 characters. +<{o"i!a,<{i<a>, 10 characters. +How many non-canceled characters are within the garbage in your puzzle input? diff --git a/src/2017/day9/aoc.cpp b/src/2017/day9/aoc.cpp index ed2d9d3..0b1cda6 100644 --- a/src/2017/day9/aoc.cpp +++ b/src/2017/day9/aoc.cpp @@ -1,5 +1,54 @@ #include "aoc.h" +#include <stack> namespace aoc2017 { +static void take(const char* p, std::stack<char>& gs, int* t) { + if (*p == '{') { + gs.push(*p); + } + if (*p == '}') { + *t += gs.size(); + gs.pop(); + } } + +static bool is_valid(const char* p, std::stack<char>& g) { + if (g.empty()) { + if (*p == '!') { + g.push(*p); + } + if (*p == '<') { + g.push(*p); + } + } else { + if (g.top() == '!') { + g.pop(); + } else { + if (*p == '>') { + g.pop(); + } + if (*p == '!') { + g.push(*p); + } + } + } + + return g.empty(); +} + +int day9(line_view file) { + int t0{0}; + std::stack<char> gs; + std::stack<char> gv; + const char* p = file.line; + while (p < file.line + file.length) { + if (is_valid(p, gv)) { + take(p, gs, &t0); + } + p++; + } + return t0; +} + +} // namespace aoc2017 diff --git a/src/2017/day9/aoc.h b/src/2017/day9/aoc.h index 7aacc4c..7fb26e7 100644 --- a/src/2017/day9/aoc.h +++ b/src/2017/day9/aoc.h @@ -3,4 +3,5 @@ namespace aoc2017 { +int day9(line_view); } diff --git a/test/test_2017.cpp b/test/test_2017.cpp index d5c4f60..3be7b16 100644 --- a/test/test_2017.cpp +++ b/test/test_2017.cpp @@ -78,3 +78,20 @@ TEST_CASE("I Heard You Like Registers", "[2017]") { REQUIRE(4567 == p.first); REQUIRE(5636 == p.second); } + +TEST_CASE("Stream Processing", "[2017]") { + line_view lv = load_file("../src/2017/day9/input"); + auto p = aoc2017::day9(lv); + REQUIRE(7640 == p); + // const char* ps[] = {"{}", + // "{{{}}}", + // "{{},{}}", + // "{{{},{},{{}}}}", + // "{<a>,<a>,<a>,<a>}", + // "{{<ab>},{<ab>},{<ab>},{<ab>}}", + // "{{<!!>},{<!!>},{<!!>},{<!!>}}", + // "{{<a!>},{<a!>},{<a!>},{<ab>}}"}; + // for (auto& p : ps) { + // printf("%s -> %d\n", p, aoc2017::day9(p)); + // } +} |