From d128bfa35f52916e190c2515249be721b68a8db0 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Tue, 6 Dec 2022 07:53:44 +0100 Subject: day 6 --- 22.6/main.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 22.6/main.cpp diff --git a/22.6/main.cpp b/22.6/main.cpp new file mode 100644 index 0000000..e7eee34 --- /dev/null +++ b/22.6/main.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +using namespace std; + +int +part1(string signal, int target) +{ + constexpr int amt = 'z' - 'a' + 1; + int last[amt]; + int last_rep = -1; + fill(begin(last), end(last), -10); + for (int i = 0; i < signal.length(); i++) { + int c = signal[i] - 'a'; + if (c >= amt) throw "bad input"; + last_rep = max(last_rep, last[c]); + if (i - last_rep >= target) { + return i + 1; + } + last[c] = i; + } + return -1; +} + +int +main() +{ + for (string line; getline(cin, line); ) { + cout << part1(line, 4) << endl; + cout << part1(line, 14) << endl; + } +} -- cgit 1.4.1-2-gfad0