advent-of-code/2023/1/script.py
2024-12-25 12:59:49 +01:00

31 lines
719 B
Python

#!/usr/bin/env python3
import re
digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
group = "|".join(digits + ["[0-9]"])
tot = 0
with open("lines.txt") as lines:
for line in lines.readlines():
print()
line = line.rstrip()
print(line)
last = re.search(rf"^.*({group})", line)
first = re.search(rf"({group}).*$", line)
print(first, last)
f = first[1]
l = last[1]
print(f, l)
if f in digits:
f = str(digits.index(f))
if l in digits:
l = str(digits.index(l))
print(f, l)
numb = int(f + l)
tot += numb
print(numb)
print()
print(tot)