Day 01
This commit is contained in:
20
day01/part2.py
Normal file
20
day01/part2.py
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
import re
|
||||
|
||||
STRINGS = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
||||
|
||||
lines = (x.strip() for x in open("input.txt"))
|
||||
total = 0
|
||||
digitRegex = f"(\\d|{'|'.join(STRINGS)})"
|
||||
regexFirst = re.compile(f"^.*?(\\d|{'|'.join(STRINGS)})")
|
||||
regexLast = re.compile(f"^.*(\\d|{'|'.join(STRINGS)})")
|
||||
|
||||
def parse(value):
|
||||
return str(STRINGS.index(value) + 1) if value in STRINGS else value
|
||||
|
||||
for line in lines:
|
||||
first = parse(regexFirst.match(line).group(1))
|
||||
last = parse(regexLast.match(line).group(1))
|
||||
total += int(first + last)
|
||||
|
||||
print(total)
|
||||
Reference in New Issue
Block a user