aboutsummaryrefslogtreecommitdiff
path: root/src/2017/day9/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-05-25 11:39:46 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-05-25 11:39:46 +0800
commit5b85fda638c61b6d8c24e9781ec45eb559563011 (patch)
treeb6e44b19e27d2aa9c3a34b4e541e9262a8258e88 /src/2017/day9/aoc.cpp
parentb2de16ed14b4d4d8ee9c02e5a509b124be116bf1 (diff)
downloadadvent-of-code-5b85fda638c61b6d8c24e9781ec45eb559563011.tar.gz
advent-of-code-5b85fda638c61b6d8c24e9781ec45eb559563011.zip
2017 day9 part1
Diffstat (limited to 'src/2017/day9/aoc.cpp')
-rw-r--r--src/2017/day9/aoc.cpp49
1 files changed, 49 insertions, 0 deletions
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