This commit is contained in:
setop 2022-12-06 16:27:05 +01:00
parent 7660607c7c
commit d34efaa5d1
4 changed files with 50 additions and 0 deletions

8
d06/part1.py Normal file
View File

@ -0,0 +1,8 @@
import sys
L = sys.stdin.read()
for (i, (a,b,c,d)) in enumerate(zip(L,L[1:],L[2:],L[3:])):
if not (b == a or c == a or c == b or d == a or d == b or d == c):
print(i+4, L[i:i+4])
break

23
d06/part2.py Normal file
View File

@ -0,0 +1,23 @@
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

13
d06/part2b.py Normal file
View File

@ -0,0 +1,13 @@
import sys
from collections import deque
dq = deque()
for (i,c) in enumerate(sys.stdin.read()):
equals = list(filter(lambda x: c == x[1], enumerate(dq)))
if len(equals)>0: # remove left part until a char equal to c, included
for _ in range(equals[-1][0]+1):
dq.popleft()
if len(dq) == 13: # if we have 13+1 distinct chars, we're good
print(i+1)
break
dq.append(c)

6
d06/part2c.py Normal file
View File

@ -0,0 +1,6 @@
import sys
s = sys.stdin.read()
for i in range(14, len(s)):
if len(set(s[i-14:i])) == 14:
print(i)
break