Part 1 prompt:
The Elves quickly load you into a spacecraft and prepare to launch.
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 get4
, then subtract 2 to get2
. - For a mass of
14
, dividing by 3 and rounding down still yields4
, so the fuel required is also2
. - For a mass of
1969
, the fuel required is654
. - For a mass of
100756
, the fuel required is33583
.
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?
Part 1 answer/discussion:
This is a very simple problem. We are given a list of masses for modules, 1 per line. All we need to do to answer this prompt is look at each line in the input, divide it by 3 (dropping any fractional part), and subtract 2. We keep track of that number and add it to a total. That total after looping over the entire input list will be the fuel requirement for all modules.
#!/bin/bash filename='input.txt' exec 4<$filename totalmass=0 addme=0 while read -u4 i; do #echo $i addme=`echo "($i/3)" | bc` # echo "afer bc is $addme" # addme= `echo $addme | cut -f1 -d "."` addme=$(expr $addme - 2) echo "addme is $addme" totalmass=`expr $totalmass + $addme` done echo $totalmass
This code is simple enough to not need any comments added, but I did leave some comments I had while testing. I wasn’t sure if bc would do integer math or floating point so I had a comment to print that out. If it didn’t I would have chopped up its output on the ‘.’ character and taken just the first part, before the decimal point. Running the script against our input we get that 3272935 units of fuel are required.
Part 2: Rewrite needed!?
Prompt:
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
requires2
fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is0
, which would call for a negative fuel), so the total fuel required is still just2
. - At first, a module of mass
1969
requires654
fuel. Then, this fuel requires216
more fuel (654 / 3 - 2
).216
then requires70
more fuel, which requires21
fuel, which requires5
fuel, which requires no further fuel. So, the total fuel required for a module of mass1969
is654 + 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.)
Part 2 discussion/solution:
OK, so basically in part 1 we calculated how much fuel would be needed to carry all our modules. However, adding fuel to our rocket means we need more fuel to carry that fuel. Looking back, a proper solution to this would have just used recursion but in the crunch it didn’t read to me that way. Basically, now every time we add fuel we need to add the fuel needed to carry that fuel and then the fuel needed to carry THAT fuel and so on. Once the amount of fuel needed to carry the new fuel is zero or negative, we can move on to the next module.
I thought it would be best to just move the fuel calculation to its own function, so I did that. Then I ran that function over and over until the amount of fuel was zero or negative and added all the fuel to the total. A smart programmer would have done this in the first place but for me it meant a good portion of my code would have to change.
#!/bin/bash filename='input.txt' exec 4<$filename totalmass=0 addme=0 inputmass=0 calcmass() { addme=`echo "($1/3)" | bc` addme=$(expr $addme - 2) echo $addme } while read -u4 i; do inputmass=$i while [ $inputmass -gt 0 ]; do addme="$(calcmass $inputmass)" if [ $addme -gt 0 ]; then totalmass=`expr $totalmass + $addme` fi inputmass=$addme done done echo $totalmass
I think this is pretty simple to read too, so I didn’t really add any comments even after the fact. Even if you don’t know bash it should be fairly easy to follow. The output on this one we would expect to be higher than before, and it is – we get 4921542 for our answer against our input.
This was a lot of fun and I’d like to carry on and do some more puzzles but I won’t be staying up until midnight every day for it. I’ll do my best to record my actual start and end times but I might not even find time to do the puzzle each day between working quarter 4 retail and finishing up college.