-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart_two.py
44 lines (30 loc) · 1012 Bytes
/
part_two.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
from itertools import cycle
from typing import override
from infrastructure.solutions.base import Solution
class Year2018Day1Part2Solution(Solution):
@classmethod
@override
def parse_input(cls, text_input: str) -> dict[str, list[int]]:
frequency_changes = []
for changes in text_input.split('\n'):
for change in changes.split(', '):
frequency_changes.append(int(change))
return {'frequency_changes': frequency_changes}
@classmethod
@override
def solve(cls, frequency_changes: list[int]) -> int:
"""
Time: O(n)
Space: O(n)
Where n - number of frequency changes
"""
frequency = 0
seen = {frequency}
for change in cycle(frequency_changes):
frequency += change
if frequency in seen:
return frequency
seen.add(frequency)
return -1
if __name__ == '__main__':
print(Year2018Day1Part2Solution.main())