Return to JUNTO

JUNTO Practice: Advent of Code 2019: Day 1

Discussed on December 17, 2019.

Advent of Code 2019: Day 1

The Tyranny of the Rocket Equation

Solutions

Click to see:

John Lekberg

import math
import numbers
import pathlib

# Part 1

def required_fuel(mass: numbers.Real) -> numbers.Real:
    return math.floor(mass / 3) - 2

with open("input") as infile:
    total_fuel_requirements = sum(
        required_fuel(int(x))
        for x in infile
    )

print("part 1, total fuel requirements", total_fuel_requirements)

# Part 2

def recursive_required_fuel(mass: numbers.Real) -> numbers.Real:
    if mass <= 0:
        return 0
    else:
        x = required_fuel(mass)
        return max(0, x + recursive_required_fuel(x))

with open("input") as infile:
    total_fuel_requirements = sum(
        recursive_required_fuel(int(x))
        for x in infile
    )

print("part 2, total fuel requirements", total_fuel_requirements)

Daniel Bassett

Part 1

const calculate = (input) => {
  let fuel = input
    .split('\n')
    .map(x => (Math.floor(parseInt(x) / 3)) - 2)
    .reduce((total, current) => {
      return total += current;
    }, 0);

    return fuel;
}

Part 2

const calculateTotal = (input) => {
  const calculateInd = (moduleMass) => {
    const calculate = (input) => {
      return (Math.floor(parseInt(input) / 3)) - 2;
    }; 

    let temp = calculate(moduleMass);
    
    let totalTemp = 0;

    while (temp > 0) {
      totalTemp += temp;
      temp = calculate(temp);
    }

    return totalTemp;
  }

  let fuel = input
    .split('\n')
    .map(x => {
      return calculateInd(x);
    })
    .reduce((total, current) => {
      return total += current;
    }, 0);
  
  return fuel;
}