From d34efaa5d1da540e731435c2f0f9d2094bc2c5da Mon Sep 17 00:00:00 2001 From: setop Date: Tue, 6 Dec 2022 16:27:05 +0100 Subject: [PATCH] day 6 --- d06/part1.py | 8 ++++++++ d06/part2.py | 23 +++++++++++++++++++++++ d06/part2b.py | 13 +++++++++++++ d06/part2c.py | 6 ++++++ 4 files changed, 50 insertions(+) create mode 100644 d06/part1.py create mode 100644 d06/part2.py create mode 100644 d06/part2b.py create mode 100644 d06/part2c.py diff --git a/d06/part1.py b/d06/part1.py new file mode 100644 index 0000000..c7d0155 --- /dev/null +++ b/d06/part1.py @@ -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 diff --git a/d06/part2.py b/d06/part2.py new file mode 100644 index 0000000..480c9c9 --- /dev/null +++ b/d06/part2.py @@ -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 diff --git a/d06/part2b.py b/d06/part2b.py new file mode 100644 index 0000000..d817ffd --- /dev/null +++ b/d06/part2b.py @@ -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) diff --git a/d06/part2c.py b/d06/part2c.py new file mode 100644 index 0000000..fc7872f --- /dev/null +++ b/d06/part2c.py @@ -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