day 1 part 2
This commit is contained in:
parent
d4fa8165f0
commit
836fd9941a
2 changed files with 25 additions and 9 deletions
11
example.txt
11
example.txt
|
@ -1,4 +1,7 @@
|
|||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
||||
|
|
23
solve.py
Normal file → Executable file
23
solve.py
Normal file → Executable 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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue