-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.py
43 lines (39 loc) · 1.07 KB
/
day1.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
import fileinput
import re
def convert_digit(alphanum):
match alphanum:
case "one":
return 1
case "two":
return 2
case "three":
return 3
case "four":
return 4
case "five":
return 5
case "six":
return 6
case "seven":
return 7
case "eight":
return 8
case "nine":
return 9
case _:
return int(alphanum)
pattern = r'\d'
pattern2 = r'(?=(\d|one|two|three|four|five|six|seven|eight|nine))' # positive lookahead to get overlapping numbers
sum = 0
sum_part_2 = 0
for line in fileinput.input():
digits = re.findall(pattern, line[:-1])
if len(digits) > 0:
value = int(digits[0]) * 10 + int(digits[-1])
sum += value
digits_part_2 = re.findall(pattern2, line[:-1])
if len(digits_part_2) > 0:
value_part_2 = convert_digit(digits_part_2[0]) * 10 + convert_digit(digits_part_2[-1])
sum_part_2 += value_part_2
print("Part 1:", sum)
print("Part 2:", sum_part_2)