aoc2023/solve.py

37 lines
710 B
Python
Executable file

#!/usr/bin/env python3
import sys
import re
num_values = dict(
on="1",
tw="2",
thre="3",
four="4",
fiv="5",
six="6",
seve="7",
eigh="8",
nin="9",
)
nums_re = r"on(?=e)|tw(?=o)|thre(?=e)|four|fiv(?=e)|six|seve(?=n)|eigh(?=t)|nin(?=e)|1|2|3|4|5|6|7|8|9"
sum = 0
for line in sys.stdin:
first = len(line)
last = 0
nums = []
for m in re.finditer(nums_re, line):
nums.append((m.start(), m.group(0)))
nums.sort()
first_word = nums[0][1]
last_word = nums[-1][1]
first_word = num_values.get(first_word, first_word)
last_word = num_values.get(last_word, last_word)
value = int(first_word + last_word)
sum += value
print(sum)