Add 2022 Day 6.

This commit is contained in:
2022-12-06 10:51:17 +00:00
parent 0b06899b68
commit d57308ea83
2 changed files with 60 additions and 0 deletions

30
2022/puzzle-06-01.cc Normal file
View File

@@ -0,0 +1,30 @@
//
// Created by Matthew Gretton-Dann on 05/12/2021.
//
#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using UInt = std::uint64_t;
auto main() -> int
{
std::string line;
unsigned const match_len{4};
while (std::getline(std::cin, line) && !line.empty()) {
std::bitset<UCHAR_MAX> bits;
for (UInt i{0}; i != line.size(); ++i) {
bits.flip(static_cast<unsigned char>(line[i]));
if (i >= match_len) {
bits.flip(static_cast<unsigned char>(line[i - match_len]));
}
if (i >= (match_len - 1) && bits.count() == match_len) {
std::cout << "First match at " << i + 1 << '\n';
break;
}
}
}
return 0;
}

30
2022/puzzle-06-02.cc Normal file
View File

@@ -0,0 +1,30 @@
//
// Created by Matthew Gretton-Dann on 05/12/2021.
//
#include <bitset>
#include <iostream>
#include <string>
#include <vector>
using UInt = std::uint64_t;
auto main() -> int
{
std::string line;
unsigned const match_len{14};
while (std::getline(std::cin, line) && !line.empty()) {
std::bitset<UCHAR_MAX> bits;
for (UInt i{0}; i != line.size(); ++i) {
bits.flip(static_cast<unsigned char>(line[i]));
if (i >= match_len) {
bits.flip(static_cast<unsigned char>(line[i - match_len]));
}
if (i >= (match_len - 1) && bits.count() == match_len) {
std::cout << "First match at " << i + 1 << '\n';
break;
}
}
}
return 0;
}