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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
func readInput() ([]int, error) { fileContents, err := ioutil.ReadFile("input.txt") if err != nil { return nil, errors.WithStack(err) } trimmedContents := strings.Trim(string(fileContents), "\n") result := []int{} for _, line := range strings.Split(trimmedContents, "\n") { val, err := strconv.Atoi(line) if err != nil { return nil, errors.WithStack(err) } result = append(result, val) } return result, nil } |

1 |

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:*

1 2 3 4 5 6 7 8 9 |
func main() { input, err := readInput() if err != nil { fmt.Printf("got error: %+v", err) return } fmt.Println(solve(input)) } |

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

1 2 3 4 5 6 7 8 9 |
func solve(partMasses []int) int { result := 0 for _, partMass := range partMasses { result += partMass/3 - 2 } return result } |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
func solve(partMasses []int) int { result := 0 for _, partMass := range partMasses { fuelNeeded := partMass/3 - 2 for fuelNeeded > 0 { result += fuelNeeded fuelNeeded = fuelNeeded/3 - 2 } } return result } |

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!

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!

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.