aboutsummaryrefslogtreecommitdiff
path: root/src/2015/day12/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-19 09:13:14 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-19 09:13:14 +0800
commit8b657a93954bca14c1e16b713c76357734d8c761 (patch)
treea5d2ef9bda0eea03e1f19b05616340d33f9a64cd /src/2015/day12/aoc.cpp
parent90230f2ec6365af9889f0b7e9cbee48e5ea0f79e (diff)
downloadadvent-of-code-8b657a93954bca14c1e16b713c76357734d8c761.tar.gz
advent-of-code-8b657a93954bca14c1e16b713c76357734d8c761.zip
day12 part1
Diffstat (limited to 'src/2015/day12/aoc.cpp')
-rw-r--r--src/2015/day12/aoc.cpp35
1 files changed, 35 insertions, 0 deletions
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