Advent of Code 2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
630 B

use std::io::BufRead;
pub fn part1<F: BufRead> (input: F) {
let res: i32 = input.lines()
.filter_map(|line| line.unwrap().parse::<i32>().ok())
.map(|mass| mass / 3 - 2)
.sum();
println!("{}", res);
}
fn recursive_fuel_mass(mass: i32) -> i32 {
let fuel_mass = mass / 3 - 2;
if fuel_mass <= 0 {
0
} else {
fuel_mass + recursive_fuel_mass(fuel_mass)
}
}
pub fn part2<F: BufRead> (input: F) {
let res: i32 = input.lines()
.filter_map(|line| line.unwrap().parse::<i32>().ok())
.map(recursive_fuel_mass)
.sum();
println!("{}", res);
}