25 lines
619 B
Python
25 lines
619 B
Python
import re
|
|
|
|
def number_with_regex(line):
|
|
matches = re.findall(r"\d", line)
|
|
return int(matches[0] + matches[-1])
|
|
|
|
def number_with_ascii(line):
|
|
first = None
|
|
for character in line:
|
|
if character >= '0' and character <= '9':
|
|
if not first:
|
|
first = character
|
|
last = character
|
|
return int(first + last)
|
|
|
|
regex_total = 0
|
|
ascii_total = 0
|
|
with open("input.txt", "r") as f:
|
|
for line in f.readlines():
|
|
line = line.strip()
|
|
regex_total += number_with_regex(line)
|
|
ascii_total += number_with_ascii(line)
|
|
|
|
print(regex_total)
|
|
print(ascii_total) |