AoC2023/day1/day1.py

21 lines
581 B
Python

numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "hoppasfanintedenhärstringenfinnsiinputfilen",
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
file = open("./input", "r")
sum = 0
for line in file:
first = 999999
last = i = 0
index = [0, 0]
for number in numbers:
if (line.find(number)) != -1:
if (first := min(line.find(number), first)) is line.find(number):
index[0] = i
if (last := max(line.rfind(number), last)) is line.rfind(number):
index[1] = i
i += 1
sum += (index[0] % 10) * 10 + index[1] % 10
print(sum)