summary refs log tree commit diff
path: root/22.6
diff options
context:
space:
mode:
authordzwdz2022-12-06 07:53:44 +0100
committerdzwdz2022-12-06 07:53:44 +0100
commitd128bfa35f52916e190c2515249be721b68a8db0 (patch)
tree17b5d151a65b66c7bb1031a321b29b091c263ae7 /22.6
parentf3a189826ac3d8bc69ea5c512e846ecb64b2069e (diff)
day 6
Diffstat (limited to '22.6')
-rw-r--r--22.6/main.cpp32
1 files changed, 32 insertions, 0 deletions
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 <iostream>
+#include <algorithm>
+#include <string>
+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;
+	}
+}