diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/2016/day7/README.md | 11 | ||||
-rw-r--r-- | src/2016/day7/aoc.cpp | 71 | ||||
-rw-r--r-- | src/2016/day7/aoc.h | 2 |
3 files changed, 84 insertions, 0 deletions
diff --git a/src/2016/day7/README.md b/src/2016/day7/README.md index fb460dd..72a6f59 100644 --- a/src/2016/day7/README.md +++ b/src/2016/day7/README.md @@ -11,4 +11,15 @@ aaaa[qwer]tyui does not support TLS (aaaa is invalid; the interior characters mu ioxxoj[asdfgh]zxcvbn supports TLS (oxxo is outside square brackets, even though it's within a larger string). How many IPs in your puzzle input support TLS? +--- Part Two --- +You would also like to know which IPs support SSL (super-secret listening). +An IP supports SSL if it has an Area-Broadcast Accessor, or ABA, anywhere in the supernet sequences (outside any square bracketed sections), and a corresponding Byte Allocation Block, or BAB, anywhere in the hypernet sequences. An ABA is any three-character sequence which consists of the same character twice with a different character between them, such as xyx or aba. A corresponding BAB is the same characters but in reversed positions: yxy and bab, respectively. + +For example: + +aba[bab]xyz supports SSL (aba outside square brackets with corresponding bab within square brackets). +xyx[xyx]xyx does not support SSL (xyx, but no corresponding yxy). +aaa[kek]eke supports SSL (eke in supernet with corresponding kek in hypernet; the aaa sequence is not related, because the interior character must be different). +zazbz[bzb]cdb supports SSL (zaz has no corresponding aza, but zbz has a corresponding bzb, even though zaz and zbz overlap). +How many IPs in your puzzle input support SSL? diff --git a/src/2016/day7/aoc.cpp b/src/2016/day7/aoc.cpp index be28c50..b319a1a 100644 --- a/src/2016/day7/aoc.cpp +++ b/src/2016/day7/aoc.cpp @@ -1,4 +1,75 @@ #include "aoc.h" +#include <vector> namespace aoc2016 { + +bool is_abba(const char* p) { return *p == *(p + 3) && *(p + 1) == *(p + 2) && *p != *(p + 1); } +bool is_aba(const char* p) { return *p == *(p + 2) && *p != *(p + 1); } +bool is_reverse(const char* p1, const char* p2) { return *p1 == *(p2 + 1) && *(p1 + 1) == *p2; } + +std::vector<line_view> aba(const std::vector<line_view>& ls) { + std::vector<line_view> vs; + for (auto& l : ls) { + const char* p = l.line; + while (p < l.line + l.length - 2) { + if (is_aba(p)) { + vs.emplace_back(p, 3); + } + p++; + } + } + return vs; +} + +bool match_aba(const std::vector<line_view>& ls1, const std::vector<line_view>& ls2) { + for (auto& l1 : ls1) { + for (auto& l2 : ls2) { + if (is_reverse(l1.line, l2.line)) { + return true; + } + } + } + return false; +} + +bool has_abba(const std::vector<line_view>& ls) { + for (auto& l : ls) { + const char* p = l.line; + while (p < l.line + l.length - 3) { + if (is_abba(p)) { + return true; + } + p++; + } + } + return false; } + +std::pair<int, int> day7(line_view file) { + int t0{0}, t1{0}; + per_line(file, [&t0, &t1](line_view lv) { + std::vector<line_view> g1; + std::vector<line_view> g2; + const char* p1 = lv.line; + const char* p = p1; + while (p < lv.line + lv.length) { + if (*p == '[') { + g1.emplace_back(p1, p); + p1 = p + 1; + } + if (*p == ']') { + g2.emplace_back(p1, p); + p1 = p + 1; + } + p++; + } + g1.emplace_back(p1, p); + + t0 += int(has_abba(g1) && !has_abba(g2)); + t1 += int(match_aba(aba(g1), aba(g2))); + return true; + }); + return {t0, t1}; +} + +} // namespace aoc2016 diff --git a/src/2016/day7/aoc.h b/src/2016/day7/aoc.h index c73e513..ca88226 100644 --- a/src/2016/day7/aoc.h +++ b/src/2016/day7/aoc.h @@ -2,4 +2,6 @@ #include "common.h" namespace aoc2016 { + +std::pair<int, int> day7(line_view); } |