aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day9/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2017/day9/aoc.cpp')
-rw-r--r--src/2017/day9/aoc.cpp12
1 files changed, 8 insertions, 4 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