This commit is contained in:
Thibaud Gasser 2021-12-01 14:27:03 +01:00
commit 5963532b35
2 changed files with 2044 additions and 0 deletions

44
day1/day1.py Normal file
View File

@ -0,0 +1,44 @@
from itertools import islice
def window(seq, n=3):
"Returns a sliding window (of width n) over data from the iterable"
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
it = iter(seq)
result = tuple(islice(it, n))
if len(result) == n:
yield result
for elem in it:
result = result[1:] + (elem,)
yield result
def part1(infile):
with open(infile) as f:
lines = f.readlines()
previous = int(lines[0])
i = 0
for line in lines[1:]:
if int(line) > previous:
i += 1
previous = int(line)
print("Part 1 ", i)
def part2(infile):
with open(infile) as f:
lines = f.readlines()
previous = None
i = 0
for w in window(lines):
measure = sum(int(x) for x in w)
if previous is not None and measure > previous:
i += 1
previous = measure
print("Part 2 ", i)
if __name__ == "__main__":
part1("./input.txt")
part2("./input.txt")

2000
day1/input.txt Normal file

File diff suppressed because it is too large Load Diff