-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_b.py
executable file
·42 lines (33 loc) · 901 Bytes
/
part_b.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
#!/usr/bin/env python3
import utils
from year_2018.day_01.part_a import parse_shifts
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
73272
"""
return find_repeating_frequency(parse_shifts(_input))
def find_repeating_frequency(shifts, start=0):
"""
>>> find_repeating_frequency([1, -1])
0
>>> find_repeating_frequency([3, 3, 4, -2, -4])
10
>>> find_repeating_frequency([-6, 3, 8, 5, -6])
5
>>> find_repeating_frequency([7, 7, -2, -7, -4])
14
"""
frequency = start
seen = {frequency}
for shift in repeating(shifts):
frequency += shift
if frequency in seen:
return frequency
seen.add(frequency)
def repeating(items):
while True:
yield from items
Challenge.main()
challenge = Challenge()