AdventOfCode2023/Day 1/1.py

25 lines
695 B
Python
Raw Permalink Normal View History

2023-12-01 14:30:04 +00:00
def getDigitsOfLine(line) -> int:
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
first_digit = 0
last_digit: int = None
for i in range(len(line)):
if line[i] in digits:
if first_digit == 0:
first_digit = digits.index(line[i])
else:
last_digit = digits.index(line[i])
if last_digit == None:
last_digit = first_digit
number = (first_digit * 10) + last_digit
print(f'Number found: {number}')
return number
2023-12-02 14:22:18 +00:00
lines = open('values1', 'r').readlines()
2023-12-01 14:30:04 +00:00
number_sum = 0
for line in lines:
if line != '':
number_sum += getDigitsOfLine(line)
2023-12-02 14:22:18 +00:00
print(f'Sum: {number_sum}') # 142