33 lines
769 B
Python
33 lines
769 B
Python
def findFirstDigitInString(string: str) -> str:
|
|
length_of_string = len(string)
|
|
i = 0
|
|
while i < length_of_string :
|
|
if ( string[i].isdigit()):
|
|
return string[i]
|
|
i+=1
|
|
|
|
numbers = []
|
|
|
|
with open("input.txt") as file:
|
|
line = file.readline()
|
|
while line:
|
|
firstdigit =findFirstDigitInString(line)
|
|
lastdigit = findFirstDigitInString(line[::-1]) # doing the same for the string in reverse will get the last digit
|
|
|
|
digit = firstdigit + lastdigit
|
|
digit = int(digit)
|
|
|
|
print(f"found: {digit}")
|
|
numbers.append(digit)
|
|
|
|
line = file.readline()
|
|
|
|
print(numbers)
|
|
# now we'll need to sum them
|
|
sum = 0
|
|
for n in numbers:
|
|
sum += n
|
|
|
|
|
|
print(f"sum: {sum}")
|