AdventOfCode2023/day1-2.py
2023-12-02 16:50:31 -06:00

73 lines
2.3 KiB
Python

import time
with open("input.txt") as file:
lines = [line.rstrip() for line in file]
first_digit = None
last_digit = None
things = []
stupid_letters = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
stupid_letters_rev = ["eno", "owt", "eerht", "ruof", "evif", "xis", "neves", "thgie", "enin"]
for line in lines:
first_digit = None
last_digit = None
letter_index = 0
for letter in line:
if letter.isdigit():
if first_digit is None:
first_digit = letter
first_digit_index = letter_index
last_digit = letter
last_digit_index = letter_index
else:
last_digit = letter
last_digit_index = letter_index
letter_index+=1
keep_going = True
#find first digit in stupid format
while (keep_going):
match = False
for stupid_letter in stupid_letters:
if line.find(stupid_letter) == -1:
continue
if line.find(stupid_letter) < first_digit_index:
first_digit = stupid_letters.index(stupid_letter)+1
first_digit_index = line.find(stupid_letter)
match = True
if not match:
keep_going = False
#find last digit in stupid format by reversing the whole string bc i am very smart
rev_line = line[::-1]
new_last_digit_index = (len(line)-1) - last_digit_index
keep_going = True
while (keep_going):
match = False
for stupid_letter in stupid_letters_rev:
#print(stupid_letter)
#time.sleep(1)
if rev_line.find(stupid_letter) == -1:
continue
if rev_line.find(stupid_letter) < new_last_digit_index:
#print("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
last_digit = stupid_letters_rev.index(stupid_letter)+1
new_last_digit_index = rev_line.find(stupid_letter)
match = True
if not match:
keep_going = False
things.append(int(f"{first_digit}{last_digit}"))
print(int(f"{first_digit}{last_digit}"))
sum = 0
for thing in things:
sum += thing
print(sum)