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;
}

63
2016/puzzle-07-02.cc Normal file
View File

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