Add 2016 day 7 puzzles

This commit is contained in:
2021-12-04 17:00:16 +00:00
parent ef3676f990
commit 3f72fbad3b
2 changed files with 103 additions and 0 deletions

40
2016/puzzle-07-01.cc Normal file
View File

@@ -0,0 +1,40 @@
//
// Created by Matthew Gretton-Dann on 04/12/2021.
//
#include <array>
#include <iostream>
#include <string>
auto abba(std::string const& line)
{
enum State { outside, inside };
std::array<bool, 2> 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<unsigned>(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;
}