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
|
two1nine
|
||||||
pqr3stu8vwx
|
eightwothree
|
||||||
a1b2c3d4e5f
|
abcone2threexyz
|
||||||
treb7uchet
|
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 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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue