2019 - day1 - part 2

This commit is contained in:
genofire 2020-09-07 21:29:27 +02:00
parent d865b87052
commit b46919a2e5

View file

@ -1,5 +1,5 @@
fn main() { fn main() {
let all_modules: [u32; 100] = [ let all_modules: [i32; 100] = [
83568, 132382, 65095, 105082, 138042, 59055, 79113, 123950, 59773, 55031, 56499, 122835, 83568, 132382, 65095, 105082, 138042, 59055, 79113, 123950, 59773, 55031, 56499, 122835,
123608, 82848, 109981, 115633, 126241, 137240, 54983, 129523, 101517, 90879, 82446, 105897, 123608, 82848, 109981, 115633, 126241, 137240, 54983, 129523, 101517, 90879, 82446, 105897,
108653, 130530, 113607, 140338, 125646, 112605, 68080, 105466, 93462, 147116, 127370, 108653, 130530, 113607, 140338, 125646, 112605, 68080, 105466, 93462, 147116, 127370,
@ -15,13 +15,25 @@ fn main() {
for i in all_modules.iter() { for i in all_modules.iter() {
result += fuel_calc(*i); result += fuel_calc(*i);
} }
println!("I need {} fuel for all modules", result); println!("Part1: I need {} fuel for all modules", result);
//all_modules.map(fuel_calc).sum()
//all_modules.map(fuel_calc).sum()
println!("Part2: I need {} fuel for all modules", all_modules.iter().fold(0, |sum, x | fuel_calc_with_fuel(*x) + sum ));
} }
fn fuel_calc(mass: u32) -> u32 { fn fuel_calc(mass: i32) -> i32 {
mass / 3 - 2 mass / 3 - 2
} }
fn fuel_calc_with_fuel(mass: i32) -> i32 {
let fuel = mass / 3 - 2;
if fuel <= 0 {
return 0;
}
fuel+fuel_calc_with_fuel(fuel)
}
#[test] #[test]
fn test_it() { fn test_it() {
assert_eq!(fuel_calc(12), 2); assert_eq!(fuel_calc(12), 2);