aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-05-25 11:50:13 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-05-25 11:50:13 +0800
commit0b2be1ecc648f44785299c7d3c3c04ce06c45905 (patch)
treebd72e12001e1cf4c80849f446396111047aafedd
parent5b85fda638c61b6d8c24e9781ec45eb559563011 (diff)
downloadadvent-of-code-0b2be1ecc648f44785299c7d3c3c04ce06c45905.tar.gz
advent-of-code-0b2be1ecc648f44785299c7d3c3c04ce06c45905.zip
2017 day9
-rw-r--r--src/2017/day9/aoc.cpp12
-rw-r--r--src/2017/day9/aoc.h2
-rw-r--r--test/test_2017.cpp12
3 files changed, 16 insertions, 10 deletions
diff --git a/src/2017/day9/aoc.cpp b/src/2017/day9/aoc.cpp
index 0b1cda6..9047aef 100644
--- a/src/2017/day9/aoc.cpp
+++ b/src/2017/day9/aoc.cpp
@@ -13,7 +13,7 @@ static void take(const char* p, std::stack<char>& gs, int* t) {
}
}
-static bool is_valid(const char* p, std::stack<char>& g) {
+static bool is_valid(const char* p, std::stack<char>& g, int* t) {
if (g.empty()) {
if (*p == '!') {
g.push(*p);
@@ -25,11 +25,14 @@ static bool is_valid(const char* p, std::stack<char>& g) {
if (g.top() == '!') {
g.pop();
} else {
+ *t += 1;
if (*p == '>') {
g.pop();
+ *t -= 1;
}
if (*p == '!') {
g.push(*p);
+ *t -= 1;
}
}
}
@@ -37,18 +40,19 @@ static bool is_valid(const char* p, std::stack<char>& g) {
return g.empty();
}
-int day9(line_view file) {
+std::pair<int, int> day9(line_view file) {
int t0{0};
+ int t1{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)) {
+ if (is_valid(p, gv, &t1)) {
take(p, gs, &t0);
}
p++;
}
- return t0;
+ return {t0, t1};
}
} // namespace aoc2017
diff --git a/src/2017/day9/aoc.h b/src/2017/day9/aoc.h
index 7fb26e7..0a8ac58 100644
--- a/src/2017/day9/aoc.h
+++ b/src/2017/day9/aoc.h
@@ -3,5 +3,5 @@
namespace aoc2017 {
-int day9(line_view);
+std::pair<int, int> day9(line_view);
}
diff --git a/test/test_2017.cpp b/test/test_2017.cpp
index 3be7b16..19d71d2 100644
--- a/test/test_2017.cpp
+++ b/test/test_2017.cpp
@@ -82,16 +82,18 @@ TEST_CASE("I Heard You Like Registers", "[2017]") {
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[] = {"{}",
- // "{{{}}}",
- // "{{},{}}",
+ REQUIRE(7640 == p.first);
+ REQUIRE(4368 == p.second);
+ // const char* ps[] = {"{<{o\"i!a,<{i<a>}",
+ // "{{{<<<<>}}}",
+ // "{{<!!!>>},{<{!>}>}}",
// "{{{},{},{{}}}}",
// "{<a>,<a>,<a>,<a>}",
// "{{<ab>},{<ab>},{<ab>},{<ab>}}",
// "{{<!!>},{<!!>},{<!!>},{<!!>}}",
// "{{<a!>},{<a!>},{<a!>},{<ab>}}"};
// for (auto& p : ps) {
- // printf("%s -> %d\n", p, aoc2017::day9(p));
+ // auto x = aoc2017::day9(p);
+ // printf("%s -> %d, %d\n", p, x.first, x.second);
// }
}