advent of code 2023 edition
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aoc_2023/aoc_01.py

23 lines
607 B

10 months ago
#!python3
import re
digits = "one two three four five six seven eight nine".split(" ")
nums_of_digits = dict((b, str(a+1)) for (a, b) in enumerate(digits))
or_of_digits = "|".join(digits)
first_re = re.compile("\D*?(\d|" + or_of_digits + ").*")
second_re = re.compile(".*(\d|" + or_of_digits + ")\D*?")
with open("input01d", "r") as f:
s = 0
for line in f.readlines():
first = first_re.match(line).group(1)
second = second_re.match(line).group(1)
if first in digits:
first = nums_of_digits[first]
if second in digits:
second = nums_of_digits[second]
s += int(first + second)
print(s)