day1: change to iterator style
This commit is contained in:
parent
45a381165b
commit
17a271e24e
@ -1,10 +1,12 @@
|
|||||||
fn fuel_for_modules1(input: &str) -> i64 {
|
fn fuel_for_mass1(mass: u64) -> u64 {
|
||||||
let mut fuel: i64 = 0;
|
mass / 3 - 2
|
||||||
for module in input.split_whitespace() {
|
|
||||||
let module: i64 = module.parse().unwrap();
|
|
||||||
fuel += (module / 3) - 2;
|
|
||||||
}
|
}
|
||||||
fuel
|
|
||||||
|
fn fuel_for_modules1<I>(input: I) -> u64
|
||||||
|
where
|
||||||
|
I: IntoIterator<Item = u64>,
|
||||||
|
{
|
||||||
|
input.into_iter().map(fuel_for_mass1).sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fuel_for_mass2(mass: u64) -> u64 {
|
fn fuel_for_mass2(mass: u64) -> u64 {
|
||||||
@ -13,17 +15,15 @@ fn fuel_for_mass2(mass: u64) -> u64 {
|
|||||||
fuel + fuel_for_mass2(fuel)
|
fuel + fuel_for_mass2(fuel)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fuel_for_modules2(input: &str) -> u64 {
|
fn fuel_for_modules2<I>(input: I) -> u64
|
||||||
let mut fuel: u64 = 0;
|
where
|
||||||
for module in input.split_whitespace() {
|
I: IntoIterator<Item = u64>,
|
||||||
let module: u64 = module.parse().unwrap();
|
{
|
||||||
fuel += fuel_for_mass2(module);
|
input.into_iter().map(fuel_for_mass2).sum()
|
||||||
}
|
|
||||||
fuel
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = include_str!("input.txt");
|
let input = include_str!("input.txt").split_whitespace().map(|s| s.parse::<u64>().unwrap());
|
||||||
println!("Fuel 1: {}", fuel_for_modules1(input));
|
println!("Fuel 1: {}", fuel_for_modules1(input.clone()));
|
||||||
println!("Fuel 2: {}", fuel_for_modules2(input));
|
println!("Fuel 2: {}", fuel_for_modules2(input));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user