aboutsummaryrefslogtreecommitdiff
path: root/src/2016/day9/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2016/day9/aoc.cpp')
-rw-r--r--src/2016/day9/aoc.cpp52
1 files changed, 38 insertions, 14 deletions
diff --git a/src/2016/day9/aoc.cpp b/src/2016/day9/aoc.cpp
index 4f741d5..83e72c6 100644
--- a/src/2016/day9/aoc.cpp
+++ b/src/2016/day9/aoc.cpp
@@ -12,7 +12,31 @@ static void get_number(const char** pp, int* d) {
*pp = p;
}
-static void check(const char** pp, int* t) {
+static void check0(int t, const char* p1, const char* p2, size_t* t1) {
+ size_t x{0};
+ while (p1 < p2) {
+ if (*p1 == '(') {
+ int d[2] = {0};
+ int i = 0;
+ while (*p1 != ')') {
+ if (*p1 >= '0' && *p1 <= '9') {
+ get_number(&p1, d + i++);
+ }
+ if (*p1 == '(' || *p1 == 'x') {
+ p1++;
+ }
+ }
+ check0(d[1], p1 + 1, p1 + d[0] + 1, &x);
+ p1 += d[0] + 1;
+ } else {
+ p1++;
+ x++;
+ }
+ }
+ *t1 += t * x;
+}
+
+static void check(const char** pp, int* t0, size_t* t1) {
int d[2] = {0};
const char* p = *pp;
int i = 0;
@@ -24,30 +48,30 @@ static void check(const char** pp, int* t) {
p++;
}
}
- p += d[0];
- *t += d[1] * d[0];
- *pp = p;
+ *t0 += d[1] * d[0];
+ check0(d[1], p + 1, p + d[0] + 1, t1);
+ *pp = p + d[0] + 1;
}
-int day9(line_view file) {
+std::pair<int, size_t> day9(line_view file) {
int t0{0};
+ size_t t1{0};
const char* p1 = file.line;
- const char* p2 = p1 + file.length - 1;
- const char* p = p1;
+ const char* p2 = p1 + file.length;
- while (p < p2) {
- if (*p == '(') {
- check(&p, &t0);
- p++;
+ while (p1 < p2) {
+ if (*p1 == '(') {
+ check(&p1, &t0, &t1);
} else {
- if (*p != ' ') {
+ if (*p1 != ' ' && *p1 != '\n') {
t0++;
+ t1++;
}
- p++;
+ p1++;
}
}
- return t0;
+ return {t0, t1};
}
} // namespace aoc2016