import re accumulator = 0 with open("input.txt") as input: while line := input.readline(): if match := re.match(".*?([0-9]).*([0-9]).*?", line): # print(match.groups(1)) number = int(match.groups(1)[0] + match.groups(1)[1]) # print(number) accumulator += number elif match := re.match(".*([0-9]).*", line): # print(match.groups(1)) number = int(match.groups(1)[0] + match.groups(1)[0]) # print(number) accumulator += number print(accumulator)