Advent_of_Code_2023_Intermate/Questions/Day 1.py

49 lines
1.2 KiB
Python

from utils import load_data
def question_1(data):
sum = 0
for line in data:
line = "".join(filter(str.isdigit, line))
sum += int(line[0]+line[-1])
print(f"Answer to question 1 is: {sum}")
def question_2(data: list[str]):
numbers = {"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9}
sum = 0
for line in data:
new_word = ""
current_word = ""
for character in line:
if character.isdigit():
new_word += character
current_word = ""
else:
current_word += character
for number in numbers:
if number in current_word:
new_word += str(numbers[number])
current_word = current_word[-1:]
sum += int(new_word[0] + new_word[-1])
print(f"Answer to question 2 is: {sum}")
if __name__ == "__main__":
filepath = "Data/Day1.txt"
data = load_data(filepath)
# question_1(data)
question_2(data)