-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.2.py
35 lines (31 loc) · 814 Bytes
/
day1.2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import sys
def gen_measures(fd):
start, mid, end = [int(next(fd)) for i in range(3)]
yield sum((start, mid, end))
for l in fd:
start = mid
mid = end
end = int(l)
yield sum((start, mid, end))
def gen_events(fd):
gen = gen_measures(fd)
prev = next(gen)
for l in gen:
curr = int(l)
if curr > prev:
ev = 'increased'
elif curr < prev:
ev = 'decreased'
else:
ev = 'unchanged'
yield prev, curr, ev
prev = curr
def main(fd):
inc = [i for i in gen_events(fd) if i[2] == 'increased']
print(len(inc))
if __name__ == '__main__':
try:
with open(sys.argv[1]) as f:
main(f)
except IndexError:
print(f"python {sys.argv[0]} <input_file.txt>")