Skip to content

Commit

Permalink
2023 day 1.
Browse files Browse the repository at this point in the history
  • Loading branch information
starkindustries committed Dec 1, 2023
1 parent 2aac0d2 commit 9af33f0
Show file tree
Hide file tree
Showing 4 changed files with 1,065 additions and 0 deletions.
54 changes: 54 additions & 0 deletions 2023/day1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

with open("input.txt") as file:
lines = file.readlines()

# part 1

total = 0
for line in lines:
line = line.strip()
for my_char in line:
if my_char.isdigit():
first_number = my_char
break
for my_char in line[::-1]:
if my_char.isdigit():
second_number = my_char
break
str_number = first_number + second_number
int_number = int(str_number)
print(int_number)
total += int_number
print(total)

# part 2

str_digits = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]



total = 0
for line in lines:
numbers = []
line = line.strip()
for i in range(len(line)):
my_char = line[i]
if my_char.isdigit():
numbers.append(my_char)
continue
for j, digit in enumerate(str_digits):
# python's string slicing prevents IndexError so don't need to worry about going out of bounds
my_digit = line[i:i+len(digit)]
# print("mydigit ", my_digit, ":", digit, ":", line)
if digit == my_digit:
number = str(j + 1)
numbers.append(number)
break

print("numbers", numbers)
str_number = numbers[0] + numbers[-1]
int_number = int(str_number)
print(int_number)
total += int_number
print("part2")
print(total)
Loading

0 comments on commit 9af33f0

Please sign in to comment.