From d57308ea8334480f30c1f3aea3fc7ebc82b55a2d Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Tue, 6 Dec 2022 10:51:17 +0000 Subject: [PATCH] Add 2022 Day 6. --- 2022/puzzle-06-01.cc | 30 ++++++++++++++++++++++++++++++ 2022/puzzle-06-02.cc | 30 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 2022/puzzle-06-01.cc create mode 100644 2022/puzzle-06-02.cc 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; +}