Advent of Code: Day 1

The Advent of Code is back! In past iterations of the Advent of Code, a bunch of my friends and I would share all our solutions. This year, I thought why not go the extra step and publicly write up my solutions. I have had some success with Advent of Code in the past. In 2015, I came in 12th overall for the Advent of Code. Let’s see how well I do this year! I’ll be competing this year with Go.


Being the first day of the contest, this problem was an easy one. You are given the masses of each part of a spacecraft and need to calculate the total fuel needed to fly the spacecraft. To determine the amount of mass needed for an individual part, you “take its mass, divide by three, round down, and subtract 2”. The total fuel needed for the spacecraft is the sum of the fuel needed for the individual parts.

Part one took me less than two minutes to solve. This problem can be solved in two steps. First read the masses from the input file, then sum the fuel needed for each part. I managed to solve it extremely fast by reusing code I had used last year for reading a list of numbers from a file. That part looks like:

There’s nothing too interesting here. We iterate through each line of the file, use strconv.Atoi to parse the mass from the line, and build up a list of the masses. I also reused a main function which calls readInput and passes the result to a yet to be defined function solve:

Solve has a straightforward definition. We take the list of part masses and for each part, sum the amount of fuel needed:

And that’s it for part 1!


For part 2, not only do we need to calculate the amount of fuel needed for the ship parts, but we also need to calculate the amount of fuel needed for the fuel. There’s also the fuel needed for the extra fuel needed for the original fuel (and so on). For each ship part, you are supposed to calculate the amount of fuel needed, then consider the fuel to be a new mass you need to calculate fuel for. You repeat this process until the amount of fuel needed is less than or equal to zero. This part took me about seven minutes to solve, largely because I had made a few mistakes along the way.

To solve part2, we only need to modify our solve function to take the new fuel needed into account. We have a loop that repeats the fuel calculation process. Until the amount of extra fuel needed is zero, we add the extra fuel needed to the total and then repeat the process with the fuel we just added. Our new solve function looks like:

And that gives us our answer to part 2!

You can find the full code for my solutions in this GitHub repository. Until next time!

 

2 thoughts on “Advent of Code: Day 1

  1. I am always been a keen reader of your blog, especially Lisp posts. If you’ll have time, would you consider having some Advent of Code entries using Common Lisp too? Thank you for the high quality posts!

    1. I’m not planning on doing any in Common Lisp. To be honest, I haven’t written much Lisp code in the last few years.

Leave a Reply

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