diff --git a/res/2016/day07/test_input.txt b/res/2016/day07/test_input.txt index e69de29..0d7963c 100644 --- a/res/2016/day07/test_input.txt +++ b/res/2016/day07/test_input.txt @@ -0,0 +1,8 @@ +abba[mnop]qrst +abcd[bddb]xyyx +aaaa[qwer]tyui +ioxxoj[asdfgh]zxcvbn +aba[bab]xyz +xyx[xyx]xyx +aaa[kek]eke +zazbz[bzb]cdb diff --git a/src/2016/Day07.cpp b/src/2016/Day07.cpp index 5ae30a0..2efefd1 100644 --- a/src/2016/Day07.cpp +++ b/src/2016/Day07.cpp @@ -2,30 +2,165 @@ namespace y2016::day07 { - REGISTER_DAY(2016, Day07, std::vector, int); + struct IP + { + std::vector s; + std::vector hypernets; - REGISTER_TEST_EXAMPLE(2016, Day07, ExampleInput, 1, 0); - REGISTER_TEST(2016, Day07, Input, 1, 0); - REGISTER_TEST_EXAMPLE(2016, Day07, ExampleInput, 2, 0); - REGISTER_TEST(2016, Day07, Input, 2, 0); + friend std::ostream& operator<<(std::ostream& os, const IP& ip) + { + for (int i = 0; i < ip.s.size() + ip.hypernets.size(); i++) + { + if (i % 2 == 0) + os << ip.s[i / 2] << " "; + else + os << "[" << ip.hypernets[i / 2] << "] "; + } + return os; + } + }; + + REGISTER_DAY(2016, Day07, std::vector, int); + + REGISTER_TEST_EXAMPLE(2016, Day07, ExampleInput, 1, 2); + REGISTER_TEST(2016, Day07, Input, 1, 115); + REGISTER_TEST_EXAMPLE(2016, Day07, ExampleInput, 2, 3); + REGISTER_TEST(2016, Day07, Input, 2, 231); READ_INPUT(input) { - std::vector vec; + std::vector vec; std::string str; while (getline(input, str)) { + IP ip; + bool readHypernet = false; + char c = '['; + size_t prevPos = 0; + size_t pos = str.find(c); + while (pos != std::string::npos) + { + if (!readHypernet) + { + c = ']'; + ip.s.emplace_back(str.substr(prevPos, pos - prevPos)); + } + else + { + c = '['; + ip.hypernets.emplace_back(str.substr(prevPos, pos - prevPos)); + } + prevPos = pos + 1; + pos = str.find(c, prevPos); + readHypernet = !readHypernet; + } + ip.s.emplace_back(str.substr(prevPos)); + vec.emplace_back(ip); } return vec; } + bool HasABBA(const std::string& str) + { + for (size_t i = 0; i < str.size() - 3; i++) + { + if (str[i] == str[i + 3] && str[i + 1] == str[i + 2] && str[i] != str[i + 1]) + return true; + } + + return false; + } + + bool SupportsTLS(const IP& ip) + { + for (const auto& hypernet : ip.hypernets) + { + if (HasABBA(hypernet)) + { + return false; + } + } + + for (const auto& s : ip.s) + { + if (HasABBA(s)) + { + return true; + } + } + return false; + } + + std::vector> GetABAs(const std::string& str) + { + std::vector> result; + for (size_t i = 0; i < str.size() - 2; i++) + { + if (str[i] == str[i + 2] && str[i] != str[i + 1]) + result.emplace_back(str[i], str[i + 1]); + } + return result; + } + + bool HasABA(const std::string& str, char c1, char c2) + { + for (size_t i = 0; i < str.size() - 2; i++) + { + if (str[i] == c1 && str[i + 1] == c2 && str[i + 2] == c1) + return true; + } + return false; + } + + bool HasABA(const std::vector& hypernets, char c1, char c2) + { + for (const auto& hypernet : hypernets) + { + if (HasABA(hypernet, c1, c2)) + return true; + } + return false; + } + + bool SupportsSSL(const IP& ip) + { + for (const auto& s : ip.s) + { + std::vector> abas = GetABAs(s); + for (const auto& aba : abas) + { + if (HasABA(ip.hypernets, aba.second, aba.first)) + { + return true; + } + } + } + return false; + } + OUTPUT1(input) { - return 0; + int count = 0; + for (const auto& ip : input) + { + if (SupportsTLS(ip)) + { + count++; + } + } + return count; } OUTPUT2(input) { - return 0; + int count = 0; + for (const auto& ip : input) + { + if (SupportsSSL(ip)) + { + count++; + } + } + return count; } }