-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
46 lines (37 loc) · 1.07 KB
/
__init__.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
# Set with all digits 0-9 as strings
DIGITS = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
}
def part_1(lines: list[str]) -> int:
# The answer is the sum of each line's calibration values. We start with 0
result = 0
# For each line
for ln in lines:
# We start with an empty string (to construct our 2-digit number)
num_str = ''
# We look at each character from the line, from left to right
for ch in ln:
# If we find a digit, we append it to num_str and break
if ch in DIGITS:
num_str += ch
break
# We look again at each character, this time from right to left
for ch in ln[::-1]:
# If we find a digit, we append it to num_str and break
if ch in DIGITS:
num_str += ch
break
# We convert the value to a base 10 int and add it to the final result
result += int(num_str, 10)
return result
def part_2(lines: list[str]):
raise NotImplementedError