day 1 part 2

main
Andy Teijelo 2023-12-01 10:23:08 -05:00
parent d4fa8165f0
commit 836fd9941a
2 changed files with 25 additions and 9 deletions

View File

@ -1,4 +1,7 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

23
solve.py 100644 → 100755
View File

@ -1,13 +1,26 @@
#!/usr/bin/env python3
import sys
import re
import regex as re
num_values = dict(
one=1,
two=2,
three=3,
four=4,
five=5,
six=6,
seven=7,
eight=8,
nine=9,
)
sum = 0
for line in sys.stdin:
line = re.sub(r'[^\d]', '', line)
first = line[0]
last = line[-1]
nums = re.findall(r"one|two|three|four|five|six|seven|eight|nine|[1-9]", line, overlapped=True)
nums = [str(num_values.get(x, x)) for x in nums]
first = nums[0]
last = nums[-1]
value = int(first + last)
sum += value
print(sum)