Add solution for 2016 Day 7
This commit is contained in:
@@ -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
|
||||||
|
|||||||
+143
-8
@@ -2,30 +2,165 @@
|
|||||||
|
|
||||||
namespace y2016::day07
|
namespace y2016::day07
|
||||||
{
|
{
|
||||||
REGISTER_DAY(2016, Day07, std::vector<int>, int);
|
struct IP
|
||||||
|
{
|
||||||
|
std::vector<std::string> s;
|
||||||
|
std::vector<std::string> hypernets;
|
||||||
|
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day07, ExampleInput, 1, 0);
|
friend std::ostream& operator<<(std::ostream& os, const IP& ip)
|
||||||
REGISTER_TEST(2016, Day07, Input, 1, 0);
|
{
|
||||||
REGISTER_TEST_EXAMPLE(2016, Day07, ExampleInput, 2, 0);
|
for (int i = 0; i < ip.s.size() + ip.hypernets.size(); i++)
|
||||||
REGISTER_TEST(2016, Day07, Input, 2, 0);
|
{
|
||||||
|
if (i % 2 == 0)
|
||||||
|
os << ip.s[i / 2] << " ";
|
||||||
|
else
|
||||||
|
os << "[" << ip.hypernets[i / 2] << "] ";
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
REGISTER_DAY(2016, Day07, std::vector<IP>, 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)
|
READ_INPUT(input)
|
||||||
{
|
{
|
||||||
std::vector<int> vec;
|
std::vector<IP> vec;
|
||||||
std::string str;
|
std::string str;
|
||||||
while (getline(input, 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;
|
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<std::pair<char, char>> GetABAs(const std::string& str)
|
||||||
|
{
|
||||||
|
std::vector<std::pair<char, char>> 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<std::string>& 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<std::pair<char, char>> abas = GetABAs(s);
|
||||||
|
for (const auto& aba : abas)
|
||||||
|
{
|
||||||
|
if (HasABA(ip.hypernets, aba.second, aba.first))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
OUTPUT1(input)
|
OUTPUT1(input)
|
||||||
{
|
{
|
||||||
return 0;
|
int count = 0;
|
||||||
|
for (const auto& ip : input)
|
||||||
|
{
|
||||||
|
if (SupportsTLS(ip))
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
OUTPUT2(input)
|
OUTPUT2(input)
|
||||||
{
|
{
|
||||||
return 0;
|
int count = 0;
|
||||||
|
for (const auto& ip : input)
|
||||||
|
{
|
||||||
|
if (SupportsSSL(ip))
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user