-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.py
46 lines (36 loc) · 995 Bytes
/
01.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
36
37
38
39
40
41
42
43
44
45
46
from itertools import cycle
from lib import stdin_get_lines
def day_two_part_one(lines):
"""
Iterate over lines and keep a running total of the frequency
"""
res = 0
for item in lines:
sign = item[0]
value = item[1:]
if sign == '+':
res += int(value)
else:
res -= int(value)
return res
def day_two_part_two(lines):
"""
Iterate over lines until a frequency is reached twice. Cycle over lines as many times as needed.
"""
res = 0
frequencies = set()
frequencies.add(res)
for item in cycle(lines):
sign = item[0]
value = item[1:]
if sign == '+':
res += int(value)
else:
res -= int(value)
if res in frequencies:
return res
frequencies.add(res)
stdin = stdin_get_lines()
print('Day One')
print('Part One: {0}'.format(day_two_part_one(stdin)))
print('Part Two: {0}'.format(day_two_part_two(stdin)))