You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.1 KiB
Python
50 lines
1.1 KiB
Python
import re
|
|
|
|
NUMBERS = re.compile(r"([0-9]|zero|one|two|three|four|five|six|seven|eight|nine)")
|
|
NUMBERS_MAP = dict(
|
|
zip(
|
|
list(map(str, range(0, 10)))
|
|
+ [
|
|
"zero",
|
|
"one",
|
|
"two",
|
|
"three",
|
|
"four",
|
|
"five",
|
|
"six",
|
|
"seven",
|
|
"eight",
|
|
"nine",
|
|
],
|
|
list(range(0, 10)) + list(range(0, 10)),
|
|
)
|
|
)
|
|
|
|
|
|
def process_line(line):
|
|
first = None
|
|
|
|
match = re.search(NUMBERS, line)
|
|
|
|
while match:
|
|
line = line[(match.start() + 1) :]
|
|
|
|
if not first:
|
|
first = NUMBERS_MAP[match.group(1)]
|
|
last = NUMBERS_MAP[match.group(1)]
|
|
|
|
match = re.search(NUMBERS, line)
|
|
|
|
return 10 * first + last
|
|
|
|
|
|
with open("day01/data/input200.txt") as infile:
|
|
result = sum(map(process_line, infile))
|
|
print(result)
|
|
print(result == 281)
|
|
|
|
with open("day01/data/input201.txt") as infile:
|
|
result = sum(map(process_line, infile))
|
|
print(result)
|
|
print(result == 54094)
|