day01 use successors

This commit is contained in:
Johannes
2019-12-01 10:00:24 +01:00
parent ec5920a9ce
commit 9e1a52e931

View File

@@ -22,19 +22,21 @@ fn task1(content: &String) {
}
fn task2(content: &String) {
fn while_fun(mass: i32) -> i32 {
let to_add = mass / 3 - 2;
if to_add < 1 {
0
} else {
to_add + while_fun(to_add)
}
};
let fuel: i32 = content
.lines()
.map(|line| line.parse::<i32>().unwrap())
.map(while_fun)
.map(|mass| {
std::iter::successors(Some(mass), |m| {
let needed_fuel = m / 3 - 2;
if needed_fuel > 0 {
Some(needed_fuel)
} else {
None
}
})
.skip(1)
.sum::<i32>()
})
.sum();
println!("Task 2: {}", fuel);