diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/2015/day12/README.md | 9 | ||||
-rw-r--r-- | src/2015/day12/aoc.cpp | 35 | ||||
-rw-r--r-- | src/2015/day12/aoc.h | 2 |
3 files changed, 46 insertions, 0 deletions
diff --git a/src/2015/day12/README.md b/src/2015/day12/README.md index 0784931..f242cd3 100644 --- a/src/2015/day12/README.md +++ b/src/2015/day12/README.md @@ -15,4 +15,13 @@ You will not encounter any strings containing numbers. What is the sum of all numbers in the document? +--- Part Two --- +Uh oh - the Accounting-Elves have realized that they double-counted everything red. + +Ignore any object (and all of its children) which has any property with the value "red". Do this only for objects ({...}), not arrays ([...]). + + [1,2,3] still has a sum of 6. + [1,{"c":"red","b":2},3] now has a sum of 4, because the middle object is ignored. + {"d":"red","e":[1,2,3,4],"f":5} now has a sum of 0, because the entire structure is ignored. + [1,"red",5] has a sum of 6, because "red" in an array has no effect. diff --git a/src/2015/day12/aoc.cpp b/src/2015/day12/aoc.cpp index 1c33c66..3b1ed2b 100644 --- a/src/2015/day12/aoc.cpp +++ b/src/2015/day12/aoc.cpp @@ -2,4 +2,39 @@ namespace aoc2015 { +int parse_day12(const char* p1, const char* p2) { + int sign = *(p1 - 1) == '-' ? -1 : 1; + int d{0}; + while (p1 != p2) { + d = d * 10 + *p1 - '0'; + p1++; + } + return sign * d; } + +int day12(line_view file) { + int total = 0; + const char* p1 = file.line; + const char* p2 = p1; + bool to_parse = false; + while (p2 < file.line + file.length) { + if (*p2 >= '0' && *p2 <= '9') { + if (!to_parse) { + p1 = p2; + to_parse = !to_parse; + } + } else { + if (to_parse) { + total += parse_day12(p1, p2); + to_parse = !to_parse; + } + } + p2++; + } + if (to_parse) { + total += parse_day12(p1, p2); + } + return total; +} + +} // namespace aoc2015 diff --git a/src/2015/day12/aoc.h b/src/2015/day12/aoc.h index c439c03..66d7164 100644 --- a/src/2015/day12/aoc.h +++ b/src/2015/day12/aoc.h @@ -3,4 +3,6 @@ namespace aoc2015 { +int day12(line_view file); + } |