add part 2
This commit is contained in:
parent
a2b8f6544b
commit
a56270c0db
22
2023/day01/python/src/part2.py
Normal file
22
2023/day01/python/src/part2.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user