add part 2

This commit is contained in:
starnakin 2023-12-01 13:30:46 +01:00
parent a2b8f6544b
commit a56270c0db

View File

@ -0,0 +1,22 @@
text: str = open("input.txt", "r").read()
value: int = 0
digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
def get_index(text:str):
if (text[0].isdigit()):
return (int(text[0]))
for i, value, in enumerate(digits):
if (text.startswith(value)):
return i;
for line in text.split("\n"):
for i, chars in enumerate([line[::-1], line]):
for j in range(len(chars)):
digit: int = get_index(chars[j::(1, -1)[i == 0]])
if digit is not None:
value += digit * 10 ** i
break;
print(value)