Advent of Code 2024 🎄

It seems putting up our tree and decorations wasn't enough to get in the Christmas spirit this year, so i've decided to go a step further and take on this years Advent of Code using Python. As im not the greatest programmer, i'm not aiming to have the most efficient or beautiful solution, simply just getting the solution should be enough for me. Oh and yeah i've for some reason decide to blog the whole thing.

Table Of Contents:

  1. Day 1
  2. Day 2



Day 1 - Part One

The first challenge involves a problem that uses a string of numbers sorted into two lines (a left side and a right side). The goal here is to sort the left and right sides so that the smallest number on the left is next to the smallest number on the right. From there we need to calculate the distance between the number on the left from the one on the right. Finally with calculating the overall difference of all the numbers.

An example of the txt we need to sort and calculate (the real input needed is much larger, but we will use the small example for now to test with.):

3   4
4   3
2   5
1   3
3   9
3   3


The Logic

So based on the task and out data we know we need to do a few things:

  • Seperate the string of text into two lists (a right side and left side)
  • Sort each list to go from smallest to largest
  • Compare the item in one list with the corresponding indexed item in the other and calculate the difference
  • Sum the overall difference of all items


The Solution

Firstly we are going to read the input text file using and then split it into lines. This will make it easier to when assigning each side to a list in the correct index. We are also going to need to create our two lists.

file = open('day1/day1.txt', 'r')
content = file.read()
lists = content.splitlines()

list1 = []
list2 = []
  • open() function will simply open are file in read mode
  • read() method will read the contents and save it to our variable
  • splitlines() method will split the content at every new line and store it in a list


So now we have our list containing each line of the string. Although now we will need to split it into two list for the left and right item in the list. We will use a for loop and split each item in the list to then append it to one of our lists.

list1 = []
list2 = []

for line in lines:
    left, right = line.split()
    list1.append(int(left))
    list2.append(int(right))
  • split() method will split our lines and store each side into the left and right variable
  • We then simply append() the left and right variable into their own lists
  • While appending we also convert the the item from a string to an int (which we will need to sum the total later)


Now we have our left side and right of the columns in our original string in their own list. We now want to sort them so each list is smallest to largest.

sorted_list1 = sorted(list1)
sorted_list2 = sorted(list2)
  • sorted() function will simply just sort the contents of our lists (smallest to largest)


Okay, so we are almost there. We just now need to calculate the difference from the number on the left from the right at each index. With the final thing being to calculate the total difference of every indexes difference.

variances = []

for item1, item2 in zip(sorted_list1, sorted_list2):
    difference = abs(item1 - item2)
    variances.append(difference)

print(sum(variances))
  • The zip() function allows us to loop through more than one item at once
  • Using the abs() function we can calculate the absolute difference between the two items
  • We then append() each difference to out variances list and then sum the lists total


Full Solution

file = open('day1/day1.txt', 'r')
content = file.read()
lines = content.splitlines()

list1 = []
list2 = []

for line in lines:
    left, right = line.split()
    list1.append(int(left))
    list2.append(int(right))

sorted_list1 = sorted(list1)
sorted_list2 = sorted(list2)

variances = []

for item1, item2 in zip(sorted_list1, sorted_list2):
    difference = abs(item1 - item2)
    variances.append(difference)

print(sum(variances))


Day 1 - Part Two

The second part of day ones challenge builds off of part one. We will be using the same input data and we still require the two lists (left and right) that we created for part one. Although now we need to find a 'similarity score' for the two lists. So how do we calculate the similarity score? Well let's look our example input data and explain the calculation needed.

3   4
4   3
2   5
1   3
3   9
3   3

To put it simply the number on the left side must be multiplied by the amount of times in appears on the right side. So an example for the string above would be that the first number 3 appears in the right list 3 times. This means the similarity score increases by 3 * 3 = 9. The total similarity score for the example text would be 31 if we follow this calculation for every number on the left.


The Logic

Since we know we still need our two lists (left and right) we can reuse the code from part one. But we will need to do a few extra things:

  • Calculate the similarity score for every item in our left list based on our right list
  • Sum the total of every similarity score combined


The Solution

This one is pretty straight forward and shouldn't take much extra code after reusing some of the code from part one. We'll create a list to store all our duplicates similarity scores and then run a for loop for every item in our left list to count the amount of times each item shows up in our right list.

duplicates = []

for num in list1:
    total = list2.count(num)
    if total != 0:
        total *= num
        duplicates.append(total)

print(sum(duplicates))
  • The count() method is an easy way to check the number of times an item shows up somewhere else. We pass the num in list1 we are looping though and check it against list2 and return the total occurrences
  • We then take the total number of occurrences and multiply it by the current number we are checking for in the list
  • Finally we append each occurence number for each item to our duplicates list and sum the total

As you can see i also added an if statement to remove any occurrences of 0. While this isnt necessary as summing 0 is still 0 and wont affect the end result. Visualising all the 0's in the list while running in the for loop made me feel dirty so i made sure to omit them.


Final Solution

file = open('day1/day1.txt', 'r')
content = file.read()
lists = content.splitlines()

list1 = []
list2 = []

for line in lists:
    left, right = line.split()
    list1.append(int(left))
    list2.append(int(right))

duplicates = []

for num in list1:
    total = list2.count(num)
    if total != 0:
        total *= num
        duplicates.append(total)

print(sum(duplicates))


Day 2 - Part One

Coming soon!