DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 1: The Tyranny of the Rocket Equation

Collapse
 
coolshaurya profile image
Shaurya

My solution using Rust.


fn part_a(input: Vec<u32>) {
    let output: u32 = input.iter().map(|val| (val - (val % 3)) / 3 - 2).sum();
    println!("{:?}", output);
}

fn part_b(input: Vec<u32>) {
    let output: u32 = input.iter().map(|val| rec_fuel_calc(*val)).sum();
    println!("{:?}", output);
}

fn rec_fuel_calc(mass: u32) -> u32 {
    let answer = ((mass - (mass % 3)) / 3) - 2;
    if answer < 9 {
        return answer;
    } else {
        return answer + rec_fuel_calc(answer);
    }
}