-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart_one.py
55 lines (38 loc) · 1.22 KB
/
part_one.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
47
48
49
50
51
52
53
54
55
from typing import override
from infrastructure.solutions.base import Solution
class Year2023Day1Part1Solution(Solution):
@classmethod
@override
def parse_input(cls, text_input: str) -> dict[str, list[str]]:
lines = []
for line in text_input.split('\n'):
if not line:
continue
lines.append(line)
return {'lines': lines}
@classmethod
@override
def solve(cls, lines: list[str]) -> int:
"""
Time: O(n*m)
Space: O(1)
Where n - number of lines,
m - max line length
"""
total_calibration = 0
for line in lines:
total_calibration += cls.get_calibration(line)
return total_calibration
@classmethod
def get_calibration(cls, line: str) -> int:
m = len(line)
left, right = 0, m - 1
while left <= m - 1 and not line[left].isdigit():
left += 1
while right >= 0 and not line[right].isdigit():
right -= 1
if line[left].isdigit() and line[right].isdigit():
return int(line[left] + line[right])
return 0
if __name__ == '__main__':
print(Year2023Day1Part1Solution.main())