diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-05-16 21:46:51 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-05-16 21:46:51 +0800 |
commit | 4c8cf61f3bd0d509791f46e0fbfc1f9f1ad6e9d9 (patch) | |
tree | 2b7ed37347e015b468541a8684e7eda6e9436c22 /src/2021/day8/aoc.cpp | |
parent | 450c678de3712a83b2259475f9a2711eb6340705 (diff) | |
download | advent-of-code-4c8cf61f3bd0d509791f46e0fbfc1f9f1ad6e9d9.tar.gz advent-of-code-4c8cf61f3bd0d509791f46e0fbfc1f9f1ad6e9d9.zip |
2021 day8 part1
Diffstat (limited to 'src/2021/day8/aoc.cpp')
-rw-r--r-- | src/2021/day8/aoc.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/2021/day8/aoc.cpp b/src/2021/day8/aoc.cpp index 7a3c895..9f95991 100644 --- a/src/2021/day8/aoc.cpp +++ b/src/2021/day8/aoc.cpp @@ -1,5 +1,34 @@ #include "aoc.h" +#include <algorithm> namespace aoc2021 { +static void get_length(const char** pp, int* d) { + const char* p = *pp; + *d = 0; + while (*p >= 'a' && *p <= 'g') { + *d += 1; + p++; + } + *pp = p; } + +int day8(line_view file) { + // int digits[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; + int total{0}; + int ls[4] = {0}; + per_line(file, [&ls, &total](line_view lv) { + const char* p = lv.contains("|") + 2; + int index{0}; + while (p < lv.line + lv.length) { + get_length(&p, &ls[index]); + total += int(ls[index] == 2 || ls[index] == 3 || ls[index] == 4 || ls[index] == 7); + index++; + p++; + } + return true; + }); + return total; +} + +} // namespace aoc2021 |