Published on

Advent of Code 2023: Day 1 Solution

Authors
  • avatar
    Name
    Tinker Assist
    Twitter

tailwind-nextjs-banner

Advent of Code is back! I was so excited for it to kick of this year that I stayed up to solve day 1 at midnight ET.

It looks like the theme of the year is going to be the Elves' battle to boost global snow production.

Day 1 this year was a bit trickier than those of years past. Let's discuss the solution.

Table of Contents

Part 1 Solution

Read the Puzzle Input File

We will save our puzzle input in "day1_input.txt" and then read from that file.

file = open("day1_input.txt", "r")
input = file.read()

We will then break up the input by line:

input_list = input.split('\n')

Create Solution Structure

I started by creating a for loop to go through each line in my input. Then, I added a nested for loop that goes through each character in my input line.

for line in input_list: # loop through all letters in input line
    for letter in line: # loop through all characters in input line
        # where we will read each character to determine if it is a number

Find numerical characters

In my nested loop, I identified whether or not each character in my input line is a number by using the python isnumeric() method. If a character is numeric, I want to store it somewhere so when I reach the end of my input line I have a list of the numbers that I discovered. I will create a list nums for this purpose.

for line in input_list: # loop through all letters in input line
    nums = []
    for letter in line: # loop through all characters in input line
        if letter.isnumeric():
            nums.append(letter) # append to our list of integer characters

Concatenate and add to sum

Then, after looping through every character in each input line, concat the first and last character in our list nums and add the integer representation of that to our sum

# part 1
sum = 0 # create our sum integer
for line in input_list: # loop through each line in our puzzle input
    nums = [] # craate empty list that we will use to track all integers in list
    for letter in line: # loop through all characters in input line
        if letter.isnumeric():
            nums.append(letter) # append to our list of integer characters

    # concat the first and last characters of our list and add their integer representation to our sum
    sum += int(nums[0] + nums[-1])

print(sum) # print solution to part 1

That's it! We've solved part 1!

Part 1 Visualization


Part 2 Solution

Explanation

In this part, we need to also recognize numbers spelled out in english. Thus, we will create a list called integer_names from zero to nine with equivalent indices so we can include them in the search loop.

We will create another nested for loop that, for each letter, looks ahead only the necessary number of characters to see if any spelled-out integers are present.

The Code

# part 2
sum = 0 # create our sum integer
# create list of integers spelled-out in english
integer_names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
for inp in input_list:
    nums = [] # craate empty list that we will use to track all integers in list
    for i, letter in enumerate(inp): # loop through all letters in input line
        for val, name in enumerate(integer_names): # loop through all spelled-out number options
            if name in inp[i:i+len(name)]: # if the current spelled-out number we are looking for is found starting at index i, add it to our nums list
                nums.append(str(val)) # append to our list of integer characters
        if ord(letter) <= 57: # if unicode value of character is <=57, we know it's an integer
            nums.append(letter) # append to our list of integer characters

    # concat the first and last characters of our list and add their integer representation to our sum
    sum += int(nums[0] + nums[-1])

print(sum) # print solution to part 2

Part 2 Visualization


Full code

Get the full, cleaned-up code here.

Other Good Solution Resources