2023/day1/part2.py
Fedaya 142696bf6e Day 4 completed
+ refactored day 1 to 3 with black -l 119
2023-12-04 07:44:36 +01:00

45 lines
1.1 KiB
Python

import re
digits = {
"0": "0",
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7",
"8": "8",
"9": "9",
"zero": "0",
"one": "1",
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
"seven": "7",
"eight": "8",
"nine": "9",
}
accumulator = 0
with open("input.txt") as input:
while line := input.readline():
if match := re.match(
".*?([0-9]|zero|one|two|three|four|five|six|seven|eight|nine).*([0-9]|zero|one|two|three|four|five|six|seven|eight|nine).*?",
line,
):
# print(match.groups(1))
number = int(digits[match.groups(1)[0]] + digits[match.groups(1)[1]])
# print(number)
accumulator += number
elif match := re.match(".*?([0-9]|zero|one|two|three|four|five|six|seven|eight|nine).*?", line):
# print(match.groups(1))
number = int(digits[match.groups(1)[0]] + digits[match.groups(1)[0]])
# print(number)
accumulator += number
else:
print(line)
print(accumulator)