Day 1: The Tyranny of the Rocket Equation

Santa has become stranded at the edge of the Solar System while delivering presents to other planets! To accurately calculate his position in space, safely align his warp drive, and return to Earth in time to save Christmas, he needs you to bring him measurements from fifty stars.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

The Elves quickly load you into a spacecraft and prepare to launch.

Part One

At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They havenโ€™t determined the amount of fuel required yet.

Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.

For example:

For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2. For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2. For a mass of 1969, the fuel required is 654. For a mass of 100756, the fuel required is 33583. The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.

What is the sum of the fuel requirements for all of the modules on your spacecraft?

Get masses

df <- read.table("input1.txt") %>% as_tibble()
colnames(df) <- c("Module_Mass")
df
## # A tibble: 100 x 1
##    Module_Mass
##          <int>
##  1       56583
##  2       83363
##  3      127502
##  4      138143
##  5      113987
##  6      147407
##  7      111181
##  8       92655
##  9       79802
## 10       64636
## # ... with 90 more rows

Find fuel requirements

find_fuel_mass <- function(x){
    max(floor(x/3) - 2, 0)
}
df$Fuel_Req <- sapply(df$Module_Mass, find_fuel_mass) 
df
## # A tibble: 100 x 2
##    Module_Mass Fuel_Req
##          <int>    <dbl>
##  1       56583    18859
##  2       83363    27785
##  3      127502    42498
##  4      138143    46045
##  5      113987    37993
##  6      147407    49133
##  7      111181    37058
##  8       92655    30883
##  9       79802    26598
## 10       64636    21543
## # ... with 90 more rows

Sum Fuel Reqs

fuel_mass <- vector()
fuel_mass[1] <- df$Fuel_Req %>% sum()
fuel_mass[1]
## [1] 3303995

Part Two

During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.

Fuel itself requires fuel just like a module - take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.

So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example:

A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2. At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966. The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346. What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)

Iteratively calculate fuel mass

Create the iterator function

add_fuel_mass_iteratively <- function(module_mass) {
    
    i = 1
    fuel_mass <- find_fuel_mass(module_mass)
    fuel_to_add <- fuel_mass
    
    while (fuel_to_add > 0) {
        fuel_to_add <- find_fuel_mass(fuel_mass[i])
        fuel_mass[i+1] <- fuel_to_add
        i <- i + 1
    }
    
    sum(fuel_mass)
    
}

Apply iterator to data set

df$Total_Module_Fuel <- sapply(df$Module_Mass, add_fuel_mass_iteratively)
df
## # A tibble: 100 x 3
##    Module_Mass Fuel_Req Total_Module_Fuel
##          <int>    <dbl>             <dbl>
##  1       56583    18859             28260
##  2       83363    27785             41650
##  3      127502    42498             63718
##  4      138143    46045             69039
##  5      113987    37993             56961
##  6      147407    49133             73669
##  7      111181    37058             55556
##  8       92655    30883             46296
##  9       79802    26598             39870
## 10       64636    21543             32288
## # ... with 90 more rows

Total masses

Total_Fuel <- df$Total_Module_Fuel %>% sum
Total_Fuel
## [1] 4953118