From 9d11afb01dbd9b62fdc1a17e573b0bdbe0c816b9 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 6 Dec 2022 17:42:41 +0100 Subject: [PATCH] 2022 Day 6 --- 2022/day6.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 2022/day6.cpp diff --git a/2022/day6.cpp b/2022/day6.cpp new file mode 100644 index 0000000..a20d0e0 --- /dev/null +++ b/2022/day6.cpp @@ -0,0 +1,28 @@ +/* Compile using `clang day6.cpp -o day6 -lstdc++ -std=c++11` */ + +#include +#include +#include +#include + +int find_marker(std::string input, int size) { + auto start = input.begin(); + while (start + size != input.end()) { + auto group = std::set(start, start + size); + if (group.size() == size) { + return start - input.begin() + size; + } + start++; + } + + return -1; +} + +int main() { + std::string input; + std::ifstream file("day6.input"); + std::getline(file, input); + + std::cout << "Part 1: First marker after " << find_marker(input, 4) << " characters\n"; + std::cout << "Part 2: First marker after " << find_marker(input, 14) << " characters\n"; +}