diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-03-16 17:13:23 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-03-16 17:13:23 +0800 |
commit | 6d1b0e8ca4553c22c39fddbaf5ae97e5d5fe8add (patch) | |
tree | d63d6a89527b129b8615edf012bbbc2a9637581b /src/2015/day5/aoc.cpp | |
parent | 48da21bd1e54f1efd0bbca23b67cced4a94d1447 (diff) | |
download | advent-of-code-6d1b0e8ca4553c22c39fddbaf5ae97e5d5fe8add.tar.gz advent-of-code-6d1b0e8ca4553c22c39fddbaf5ae97e5d5fe8add.zip |
day 5
Diffstat (limited to 'src/2015/day5/aoc.cpp')
-rw-r--r-- | src/2015/day5/aoc.cpp | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/2015/day5/aoc.cpp b/src/2015/day5/aoc.cpp index 6c5ca47..86d142f 100644 --- a/src/2015/day5/aoc.cpp +++ b/src/2015/day5/aoc.cpp @@ -37,23 +37,44 @@ bool is_interleaved(line_view lv) { return false; } +bool has_no_overlap_pair(line_view lv) { + const char* p1 = lv.line; + const char* p2 = lv.line + lv.length; + while (p1 + 2 <= p2) { + line_view h{p1, 2}; + line_view t{p1 + 2, p2}; + if (t.contains(h) != nullptr) { + return true; + } else { + p1++; + } + } + return false; +} + bool is_nice(line_view lv, const char* disallowed[], size_t size) { return std::all_of(disallowed, disallowed + size, [&lv](const char* s) -> bool { return lv.contains(s) == nullptr; }); } -int day5(line_view file) { - int total = 0; +std::pair<int,int> day5(line_view file) { + int total0 = 0; + int total1 = 0; const char* subs[] = {"ab", "cd", "pq", "xy"}; - per_line(file, [&subs, &total](line_view lv) { + per_line(file, [&subs, &total0, &total1](line_view lv) { auto b1 = count_vowels(lv, "aeiou") >= 3; auto b2 = is_nice(lv, 2); auto b3 = is_nice(lv, subs, ARRAY_SIZE(subs)); + auto b4 = is_interleaved(lv); + auto b5 = has_no_overlap_pair(lv); if (b1 && b2 && b3) { - total += 1; + total0 += 1; + } + if (b4 && b5) { + total1 += 1; } return true; }); - return total; + return {total0, total1}; } } // namespace aoc2015 |