2023/day1/part2.py
Fedaya fbf1c66a39 Nouveau Repository
Je perd l'historique, mais ne stocke plus les fichiers input.txt
entre autre choses
2023-12-02 12:45:07 +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)