aoc2023/solve.py

39 lines
719 B
Python
Raw Normal View History

2023-12-01 10:23:08 -05:00
#!/usr/bin/env python3
2023-12-01 09:29:09 -05:00
import sys
2023-12-01 12:01:22 -05:00
import re
2023-12-01 10:23:08 -05:00
num_values = dict(
2023-12-01 12:01:22 -05:00
one="1",
two="2",
three="3",
four="4",
five="5",
six="6",
seven="7",
eight="8",
nine="9",
2023-12-01 10:23:08 -05:00
)
2023-12-01 09:29:09 -05:00
2023-12-01 12:01:22 -05:00
patterns = "one two three four five six seven eight nine 1 2 3 4 5 6 7 8 9".split()
2023-12-01 09:29:09 -05:00
sum = 0
for line in sys.stdin:
2023-12-01 12:01:22 -05:00
first = len(line)
last = 0
nums = []
for pat in patterns:
for m in re.finditer(pat, line):
nums.append((m.start(), pat))
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)
2023-12-01 09:29:09 -05:00
sum += value
print(sum)