Advent of Code: Day 1

I assure you, it’s not Christmas yet! However, it has recently been on my TODO list to learn Python. Advent of Code is a great platform that creates challenges for you to do throughout the month of December. These are Christmas themed, so its obligatory to wear a Santa hat whilst you complete them, even in Summer. This post will walk through Day 1 of the 2022 Advent of Code challenges with the challenge and the solution.

The Challenge

The full challenge can be seen on their website, this is to provide a summary of the challenge. Note, you must complete the challenges to view the next one. This means that you may not be able to view later challenges in this series if you have not also completed the ones prior. Luckily, this is Day 1, so you will likely be able to see them.

Santa’s elves have been tasked with a mission to collect star fruit for Santa’s reindeer. They need to collect a minimum of 50 stars by Christmas. Our mission is to collect star fruit with the elves to make sure there is enough for the reindeers. This is done by solving puzzles (aka the code challenges we have to complete). You can complete them in whatever language you want, but I have chose Python.

Challenge 1

The Elves are each carrying a certain number of calories during the expedition so that they have energy to collect the fruit. Our puzzle input is a list of these calories, where the numbers for each elf are split by a space. See the example of the file input below:

1000
2000
3000

4000

5000
6000

7000
8000
9000

10000

The list shows the inventory for 5 elves. We need to calculate which elf is carrying the most calories. This means we need to sum the calories carried by each elf and find the max number out of all the elves. In this example it is elf 4.

The Code

from more_itertools import split_at

def open_file(file_path):
    try:
        return open(file_path, "r")
    except:
        print("Unable to open_file.")

def read_file():

    #open file
    file = open_file("C:\\Path\to\calories.txt")
    with file as f:
        file_lines = f.readlines()

    file_lines = [x.strip() for x in file_lines]
    #print items in list one by one
    sum_list = []
    counter = 0
    for line in file_lines:
        if line == "":
            sum_list.append(counter)
            counter = 0
        else:
            new_line = int(line)
            counter += new_line
    
    #print out the largest value from the list
    print(max(sum_list))

# Call the method to run the logic
read_file()

Conclusion

You will notice that the code reads in a file. This file is taken from the challenge website. You will also be able to get the file from there. I have also installed more-itertools when working through solutions, however, this is not used. The following pseudo code explains the code in a more readable format.

  1. Read in the calories.txt file given to us.
  2. open the file object and read the lines of that file.
  3. Strip any space at the beginning and end of the line.
  4. Create an empty array to store the summary of calories for each Elf.
  5. Create a counter object set to 0, which will be used to increment through the file_lines.
  6. Get the line for line in the file_lines object.
  7. Check to see if the line is empty.
  8. If the line is empty then append the counter to the sum_list array.
  9. Reset the counter to zero.
  10. Else if the line has a value then create a new_line object which is assigned the integer value of the line.
  11. Add the integer value of the line (the calories) to the counter object.
  12. To explain this a little further, the += operator adds a value to the existing value of counter. This means that the counter is adding together the values in the line of file_lines, until the line is empty to get the sum of the calories for each elf. When the line is empty then the counter is appended to the array, so we store the new values for each elf to sum_list and then the counter is reset to iterate through the other values.
  13. we then print out the max value from the list sum_list.
  14. We need to make sure we call the function read_file for the logic to run.

That’s all for this post! It was very simple and very quick, but a lot of fun was had starting up this series! Let me know what you think, or if you have any questions!

Sarah <3

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.