diff --git a/2016/puzzle-07-01.cc b/2016/puzzle-07-01.cc new file mode 100644 index 0000000..afcaeaf --- /dev/null +++ b/2016/puzzle-07-01.cc @@ -0,0 +1,40 @@ +// +// Created by Matthew Gretton-Dann on 04/12/2021. +// + +#include +#include +#include + +auto abba(std::string const& line) +{ + enum State { outside, inside }; + std::array seen{false, false}; + State state{State::outside}; + for (std::size_t idx{0}; idx < line.size() - 3; ++idx) { + if (line[idx] == '[') { + state = State::inside; + } + else if (line[idx] == ']') { + state = State::outside; + } + else if (line[idx] != line[idx + 1] && line[idx + 1] == line[idx + 2] && + line[idx] == line[idx + 3]) { + seen[static_cast(state)] = true; + } + } + return seen[State::outside] && !seen[State::inside]; +} + +auto main() -> int +{ + std::string line; + unsigned good_count{0}; + while (std::getline(std::cin, line)) { + if (abba(line)) { + ++good_count; + } + } + std::cout << "ABBA lines that support TLS: " << good_count << '\n'; + return 0; +} diff --git a/2016/puzzle-07-02.cc b/2016/puzzle-07-02.cc new file mode 100644 index 0000000..0f43080 --- /dev/null +++ b/2016/puzzle-07-02.cc @@ -0,0 +1,63 @@ +// +// Created by Matthew Gretton-Dann on 04/12/2021. +// + +#include +#include +#include +#include + +auto aba(std::string const& line) -> bool +{ + enum State { outside, inside }; + using AbaArray = std::array; + std::vector abas; + std::vector pending_abas; + + State state{outside}; + for (std::size_t idx{0}; idx < line.size() - 2; ++idx) { + if (line[idx] == '[') { + state = inside; + } + else if (line[idx] == ']') { + state = outside; + } + else if (line[idx] != line[idx + 1] && line[idx] == line[idx + 2]) { + if (state == outside) { + char a{line[idx]}; + char b{line[idx + 1]}; + abas.push_back({a, b}); + if (std::find_if(pending_abas.begin(), pending_abas.end(), [a, b](auto const& it) { + return it[0] == a && it[1] == b; + }) != pending_abas.end()) { + return true; + } + } + else { + char b{line[idx]}; + char a{line[idx + 1]}; + if (std::find_if(abas.begin(), abas.end(), [a, b](auto const& it) { + return it[0] == a && it[1] == b; + }) != abas.end()) { + return true; + } + pending_abas.push_back({a, b}); + } + } + } + + return false; +} + +auto main() -> int +{ + std::string line; + unsigned good_count{0}; + while (std::getline(std::cin, line)) { + if (aba(line)) { + ++good_count; + } + } + std::cout << "ABBA lines that support SSL: " << good_count << '\n'; + return 0; +}