diff --git a/2023/day01/python/src/part2.py b/2023/day01/python/src/part2.py new file mode 100644 index 0000000..4dbe2fb --- /dev/null +++ b/2023/day01/python/src/part2.py @@ -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) \ No newline at end of file