From e9578d9baa496340b5a8ab08fed5dba4be25eb1a Mon Sep 17 00:00:00 2001 From: setop Date: Thu, 2 Dec 2021 01:23:47 +0100 Subject: [PATCH] day 1 part 2 shorter --- d01/d01_2.py | 9 ++------- sliding.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 sliding.py diff --git a/d01/d01_2.py b/d01/d01_2.py index 9e78654..c74a859 100644 --- a/d01/d01_2.py +++ b/d01/d01_2.py @@ -1,8 +1,3 @@ import sys -L = [ int(l[:-1]) for l in sys.stdin.readlines() ] -Z = zip(L, L[1:], L[2:]) -M = [ a+b+c for (a,b,c) in Z] -Y = zip(M, M[1:]) -P = [ [0,1][a AB BC CD DE EF FG + from itertools import tee + a, b = tee(iterable) + IT = [ ] + for _ in range(window): + next(b, None) + return zip(a, b) + +def sliding_window(iterable, n): + from collections import deque + from itertools import islice + # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG + it = iter(iterable) + window = deque(islice(it, n), maxlen=n) + #window = deque(it, maxlen=n) + if len(window) == n: + yield tuple(window) + for x in it: + window.append(x) + yield tuple(window) + +import sys + +print([t for t in sliding_window(map(lambda x: int(x[:-1]), sys.stdin.readlines()),4)]) +#print(a for a in sliding(map(lambda x: int(x[:-1]), sys.stdin.readlines()),3))