1
0

AOC Day 1 puzzle solved

This commit is contained in:
Nigel Barink 2023-12-02 15:17:41 +01:00
commit 3d212a0c85
6 changed files with 2079 additions and 0 deletions

1000
Day-1/Part 1/input.txt Normal file

File diff suppressed because it is too large Load Diff

32
Day-1/Part 1/solution.py Normal file
View File

@ -0,0 +1,32 @@
def findFirstDigitInString(string: str) -> str:
length_of_string = len(string)
i = 0
while i < length_of_string :
if ( string[i].isdigit()):
return string[i]
i+=1
numbers = []
with open("input.txt") as file:
line = file.readline()
while line:
firstdigit =findFirstDigitInString(line)
lastdigit = findFirstDigitInString(line[::-1]) # doing the same for the string in reverse will get the last digit
digit = firstdigit + lastdigit
digit = int(digit)
print(f"found: {digit}")
numbers.append(digit)
line = file.readline()
print(numbers)
# now we'll need to sum them
sum = 0
for n in numbers:
sum += n
print(f"sum: {sum}")

1000
Day-1/Part 2/input.txt Normal file

File diff suppressed because it is too large Load Diff

39
Day-1/Part 2/solution.py Normal file
View File

@ -0,0 +1,39 @@
spelled_out_digits = [
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
]
def findFirstDigitInString(string: str) -> str:
lenght_of_string = len(string)
i = 0
while i < lenght_of_string:
substring = string[i:i+5]
if(string[i].isdigit()):
return string[i]
if (string[i].isalpha() ):
digit = [x for x in spelled_out_digits if substring.find(x) == 0 or substring.find(x[::-1]) == 0]
if len(digit) != 0:
return str(spelled_out_digits.index(digit[0]) + 1)
i+=1
numbers = []
with open("input.txt") as file:
line = file.readline()
while line :
firstdigit = findFirstDigitInString(line)
lastdigit = findFirstDigitInString(line[::-1])
numbers.append(int(f"{firstdigit}{lastdigit}"))
line = file.readline()
sum = 0
for n in numbers:
sum += n
print(f"sum: {sum}")

7
Day-1/Part 2/test.txt Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

1
README.md Normal file
View File

@ -0,0 +1 @@
# Advent of Code 2023