44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
|
|
#! Part 1
|
|
|
|
input_file = open("input.txt", "r")
|
|
sum = 0
|
|
|
|
for line in input_file.readlines():
|
|
left = -1
|
|
right = -1
|
|
|
|
for i in range(len(line)):
|
|
if line[i].isdigit():
|
|
if left == -1:
|
|
left = i
|
|
right = max(right, i)
|
|
# print(str(line) + ": " + str(int(str(line[left]) + str(line[right]))) + "\n")
|
|
sum += int(str(line[left]) + str(line[right]))
|
|
print("Part 1: " + str(sum))
|
|
|
|
|
|
|
|
#! Part 2
|
|
# I am not proud of this solutions asymptotic runtime, but it works!
|
|
|
|
input_file = open("input.txt", "r")
|
|
sum = 0
|
|
digits_as_str = {"zero": "z0o", "one": "o1e", "two": "t2o", "three": "t3e", "four": "f4r", "five": "f5e", "six": "s6x", "seven": "s7n", "eight": "e8t", "nine": "n9e"}
|
|
|
|
for line in input_file.readlines():
|
|
for digit_str in digits_as_str:
|
|
line = line.replace(digit_str, str(digits_as_str[digit_str]))
|
|
|
|
left = -1
|
|
right = -1
|
|
|
|
for i in range(len(line)):
|
|
if line[i].isdigit():
|
|
if left == -1:
|
|
left = i
|
|
right = max(right, i)
|
|
# print(str(line) + ": " + str(int(str(line[left]) + str(line[right]))) + "\n")
|
|
sum += int(str(line[left]) + str(line[right]))
|
|
print("Part 2: " + str(sum))
|