day 1 part 2 shorter

This commit is contained in:
setop 2021-12-02 01:23:47 +01:00
parent dd69954b09
commit e9578d9baa
2 changed files with 28 additions and 7 deletions

View File

@ -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<b] for (a,b) in Y ]
#print([(l,m,p) for (l,m,p) in zip(L,M,P)])
print(sum(P))
L = [int(l[:-1]) for l in sys.stdin.readlines()]
print(sum([0,1][a<b] for (a,b) in zip(L, L[3:])))

26
sliding.py Normal file
View File

@ -0,0 +1,26 @@
def sliding(iterable,window=1):
# pairwise('ABCDEFG') --> 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))