SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 90 in 24 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. aoc19 #1
  3. part1:
  4. Fuel required to launch a given module is based on its mass.
  5. Specifically, to find the fuel required for a module,
  6. take its mass, divide by three, round down, and subtract 2.
  7.  */
  8. const fs = require('fs');
  9. const requiredFuel = (mass) => Math.floor(mass / 3) - 2;
  10.  
  11. fs.readFile('./input.txt', (_, data) => {
  12.     console.log(
  13.         data.toString()
  14.         .split('\n')
  15.         .reduce((acc, e) => {
  16.             return acc += requiredFuel(parseInt(e));
  17.         }, 0)
  18.     );
  19. });
  20. /* part2
  21. ... for each module mass, calculate its fuel and add it to the total.
  22. Then, treat the fuel amount you just calculated as the input mass
  23. and repeat the process, continuing until a fuel requirement
  24. is zero or negative.
  25.  */
  26. const sumFuelRequirements = (mass) => {
  27.     if (requiredFuel(mass) > 0) {
  28.         return requiredFuel(mass) + sumFuelRequirements(requiredFuel(mass));
  29.     } else {
  30.         return 0;
  31.     }
  32. };
  33.  
  34. fs.readFile('./input.txt', (_, data) => {
  35.     console.log(data.toString()
  36.         .split('\n')
  37.         .reduce((acc, e) => {
  38.             return acc += sumFuelRequirements(parseInt(e));
  39.         }, 0)
  40.     );
  41. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top