diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-04 09:59:21 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-04 09:59:21 +0800 |
commit | e3d42a551d838e3fef28a132912f33740095c20b (patch) | |
tree | b37ea8ef02acfb6901f2d95ccc7a5864c1fc9eab /src/2017/day1/aoc.cpp | |
parent | ecdce42a63557bcc653247ad77eaa30846a021f5 (diff) | |
download | advent-of-code-e3d42a551d838e3fef28a132912f33740095c20b.tar.gz advent-of-code-e3d42a551d838e3fef28a132912f33740095c20b.zip |
2017 day1
Diffstat (limited to 'src/2017/day1/aoc.cpp')
-rw-r--r-- | src/2017/day1/aoc.cpp | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/src/2017/day1/aoc.cpp b/src/2017/day1/aoc.cpp index 9381e0c..b0e3558 100644 --- a/src/2017/day1/aoc.cpp +++ b/src/2017/day1/aoc.cpp @@ -1,3 +1,37 @@ #include "aoc.h" -namespace aoc2017 {} +namespace aoc2017 { + +int day1(line_view lv) { + int sum{0}; + const char* p1 = lv.line; + const char* p2 = p1 + 1; + while (*p2 >= '0' && *p2 <= '9') { + if (*p1 == *p2) { + sum += *p1 - '0'; + } + p1++; + p2++; + } + if (*p1 == *lv.line) { + sum += *p1 - '0'; + } + return sum; +} + +int day1part2(line_view lv) { + int sum{0}; + size_t size = lv.length % 2 == 0 ? lv.length : lv.length - 1; + const char* p1 = lv.line; + const char* p = lv.line + size / 2; + while (*p1 >= '0' && *p1 <= '9') { + const char* p2 = p1 < p ? (p + (p1 - lv.line)) : (lv.line + (p1 - p)); + if (*p1 == *p2) { + sum += *p1 - '0'; + } + p1++; + } + return sum; +} + +} // namespace aoc2017 |