diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-18 11:39:05 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-18 11:39:05 +0800 |
commit | 96f4ab93a21e6e0fc7213b2b24757110f8146d71 (patch) | |
tree | 01c674e5634703036a3d9599afccafb77564ae49 /src/2015/day8/aoc.cpp | |
parent | 4e6854698f384eda4040925fbf647b823f223050 (diff) | |
download | advent-of-code-96f4ab93a21e6e0fc7213b2b24757110f8146d71.tar.gz advent-of-code-96f4ab93a21e6e0fc7213b2b24757110f8146d71.zip |
day8 part1
Diffstat (limited to 'src/2015/day8/aoc.cpp')
-rw-r--r-- | src/2015/day8/aoc.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/2015/day8/aoc.cpp b/src/2015/day8/aoc.cpp index 1c33c66..feae5dc 100644 --- a/src/2015/day8/aoc.cpp +++ b/src/2015/day8/aoc.cpp @@ -1,5 +1,47 @@ #include "aoc.h" namespace aoc2015 { +// a -> 1 +// \\ -> 1 +// \" -> 1 +// \xXX -> 1 +int count_day8(line_view lv) { + int total = 0; + const char* p1 = lv.line; + const char* p2 = lv.line + lv.length; + const char* p = p1; + while (p != p2) { + if (*p != '\\' || p == p2 - 1) { + total += 1; + p++; + continue; + } else { + if (*(p + 1) == '\\' || *(p + 1) == '"') { + total += 1; + p += 2; + continue; + } + if (*(p + 1) == 'x') { + total += 1; + p += 4; + continue; + } + total += 1; + p++; + } + } + return total; +} +int day8(line_view file) { + int t1 = 0; + int t2 = 0; + per_line(file, [&t1, &t2](line_view lv) { + t1 += lv.length - 1; + t2 += count_day8({lv.line + 1, lv.line + lv.length - 2}); + return true; + }); + return t1 - t2; } + +} // namespace aoc2015 |