day01 use successors
This commit is contained in:
@@ -22,19 +22,21 @@ fn task1(content: &String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn task2(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
|
let fuel: i32 = content
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| line.parse::<i32>().unwrap())
|
.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();
|
.sum();
|
||||||
|
|
||||||
println!("Task 2: {}", fuel);
|
println!("Task 2: {}", fuel);
|
||||||
|
|||||||
Reference in New Issue
Block a user