aoc2022/d06/part2.py

24 lines
548 B
Python
Raw Permalink Normal View History

2022-12-06 15:27:05 +00:00
import sys
def window_iter(it, size):
from itertools import tee
wt = tee(it, size) # tuple(n iterables)
# shift each iterator by its place in the window
for (i,x) in enumerate(wt):
for _ in range(i):
next(x)
while True:
try:
yield tuple(next(x) for x in wt)
except RuntimeError: # RuntimeError: generator raised StopIteration
break
L = sys.stdin.read()
from itertools import combinations as comb
for (i, T) in enumerate(window_iter(L,14)):
if all(map(lambda x: x[0] != x[1], comb(T,2))):
print(i+14, L[i:i+14])
break