diff --git a/2022/puzzle-06-01.cc b/2022/puzzle-06-01.cc new file mode 100644 index 0000000..a8419b7 --- /dev/null +++ b/2022/puzzle-06-01.cc @@ -0,0 +1,30 @@ +// +// Created by Matthew Gretton-Dann on 05/12/2021. +// + +#include +#include +#include +#include + +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 bits; + for (UInt i{0}; i != line.size(); ++i) { + bits.flip(static_cast(line[i])); + if (i >= match_len) { + bits.flip(static_cast(line[i - match_len])); + } + if (i >= (match_len - 1) && bits.count() == match_len) { + std::cout << "First match at " << i + 1 << '\n'; + break; + } + } + } + return 0; +} diff --git a/2022/puzzle-06-02.cc b/2022/puzzle-06-02.cc new file mode 100644 index 0000000..cd7ac01 --- /dev/null +++ b/2022/puzzle-06-02.cc @@ -0,0 +1,30 @@ +// +// Created by Matthew Gretton-Dann on 05/12/2021. +// + +#include +#include +#include +#include + +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 bits; + for (UInt i{0}; i != line.size(); ++i) { + bits.flip(static_cast(line[i])); + if (i >= match_len) { + bits.flip(static_cast(line[i - match_len])); + } + if (i >= (match_len - 1) && bits.count() == match_len) { + std::cout << "First match at " << i + 1 << '\n'; + break; + } + } + } + return 0; +}