DEV Community

Cover image for The Tyranny of the Rocket Equation
Robert Mion
Robert Mion

Posted on

The Tyranny of the Rocket Equation

Advent of Code 2019 Day 1

Task: Solve for X where...

X = the sum of the fuel requirements
Enter fullscreen mode Exit fullscreen mode

Example input

12
14
1969
100756
Enter fullscreen mode Exit fullscreen mode

It represents:

  • The mass each module

Part 1

A written description of my working algorithm

Split the input at each newline character to generate an array of strings
Coerce each string to a number
For each number, accumulate a number - starting at 0
  Add to the accumulating number the sum of that number and the current number divided by 3, rounded down, and decremented by 2
Return the accumulated number
Enter fullscreen mode Exit fullscreen mode

A visual depiction of my working algorithm

Algorithm visualization

Part 2

A written description of my working algorithm

Sub-routine: Recursive Fuel
- Accept a number as input
If the number is less than or equal to 0
  Return 0
Else
  Return the sum of the input number and the number returned from calling this sub-routine with the number resulting from dividing the input number by 3, rounding down, and decrementing by 2

Split the input at each newline character to generate an array of strings
Coerce each string to a number
For each number, accumulate a number - starting at 0
  Add to the accumulating number the number returned from calling Recursive Fuel with the number resulting from dividing the current number by 3, rounding down, and decrementing by 2
Return the accumulated number
Enter fullscreen mode Exit fullscreen mode

A visual depiction of my working algorithm

Algorithm visualization

Reduction. Recursion. Ready for the next one.

Top comments (0)