aoc 2023 01

This commit is contained in:
Julian Brammer 2023-12-01 15:43:13 +01:00
parent 6f3185baf4
commit fd62dfe536
4 changed files with 1054 additions and 0 deletions

1000
2023/01/input.txt Normal file

File diff suppressed because it is too large Load Diff

43
2023/01/main.py Normal file
View File

@ -0,0 +1,43 @@
#! 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))

4
2023/01/test_input.txt Normal file
View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

7
2023/01/test_input2.txt Normal file
View File

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