day 1 part 2

This commit is contained in:
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 two1nine
pqr3stu8vwx eightwothree
a1b2c3d4e5f abcone2threexyz
treb7uchet xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

23
solve.py Normal file → Executable file
View file

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