From aba16a255631bb153120f1a77943f38a2fc68673 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 6 Dec 2022 07:49:02 +0100 Subject: [PATCH] 2022: d06: ex1: add solution --- 2022/d06/ex1/ex1.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 2022/d06/ex1/ex1.py diff --git a/2022/d06/ex1/ex1.py b/2022/d06/ex1/ex1.py new file mode 100755 index 0000000..954bded --- /dev/null +++ b/2022/d06/ex1/ex1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +import collections +import itertools +import sys +from collections.abc import Iterable +from typing import TypeVar + +T = TypeVar("T") + + +def sliding_window(iterable: Iterable[T], n: int) -> Iterable[tuple[T, ...]]: + it = iter(iterable) + window = collections.deque(itertools.islice(it, n), maxlen=n) + if len(window) == n: + yield tuple(window) + for x in it: + window.append(x) + yield tuple(window) + + +def solve(input: str) -> int: + SIZE = 4 + for i, tup in enumerate(sliding_window(input, SIZE), start=SIZE): + if len(set(tup)) == SIZE: + return i + assert False + + +def main() -> None: + input = sys.stdin.read().replace("\n", "") + print(solve(input)) + + +if __name__ == "__main__": + main()