|
|
|
@ -1,12 +1,10 @@
|
|
|
|
|
fn fuel_for_mass1(mass: u64) -> u64 {
|
|
|
|
|
mass / 3 - 2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fuel_for_modules1<I>(input: I) -> u64
|
|
|
|
|
where
|
|
|
|
|
I: IntoIterator<Item = u64>,
|
|
|
|
|
{
|
|
|
|
|
input.into_iter().map(fuel_for_mass1).sum()
|
|
|
|
|
fn fuel_for_modules1(input: &str) -> i64 {
|
|
|
|
|
let mut fuel: i64 = 0;
|
|
|
|
|
for module in input.split_whitespace() {
|
|
|
|
|
let module: i64 = module.parse().unwrap();
|
|
|
|
|
fuel += (module / 3) - 2;
|
|
|
|
|
}
|
|
|
|
|
fuel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fuel_for_mass2(mass: u64) -> u64 {
|
|
|
|
@ -15,42 +13,17 @@ fn fuel_for_mass2(mass: u64) -> u64 {
|
|
|
|
|
fuel + fuel_for_mass2(fuel)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fuel_for_modules2<I>(input: I) -> u64
|
|
|
|
|
where
|
|
|
|
|
I: IntoIterator<Item = u64>,
|
|
|
|
|
{
|
|
|
|
|
input.into_iter().map(fuel_for_mass2).sum()
|
|
|
|
|
fn fuel_for_modules2(input: &str) -> u64 {
|
|
|
|
|
let mut fuel: u64 = 0;
|
|
|
|
|
for module in input.split_whitespace() {
|
|
|
|
|
let module: u64 = module.parse().unwrap();
|
|
|
|
|
fuel += fuel_for_mass2(module);
|
|
|
|
|
}
|
|
|
|
|
fuel
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let input = include_str!("input.txt").split_whitespace().map(|s| s.parse::<u64>().unwrap());
|
|
|
|
|
println!("Fuel 1: {}", fuel_for_modules1(input.clone()));
|
|
|
|
|
let input = include_str!("input.txt");
|
|
|
|
|
println!("Fuel 1: {}", fuel_for_modules1(input));
|
|
|
|
|
println!("Fuel 2: {}", fuel_for_modules2(input));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod day1test {
|
|
|
|
|
#[test]
|
|
|
|
|
fn example1() {
|
|
|
|
|
// Original descriptions:
|
|
|
|
|
// > For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
|
|
|
|
|
// > For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
|
|
|
|
|
// > For a mass of 1969, the fuel required is 654.
|
|
|
|
|
// > For a mass of 100756, the fuel required is 33583.
|
|
|
|
|
assert_eq!(super::fuel_for_mass1(12), 2);
|
|
|
|
|
assert_eq!(super::fuel_for_mass1(14), 2);
|
|
|
|
|
assert_eq!(super::fuel_for_mass1(1969), 654);
|
|
|
|
|
assert_eq!(super::fuel_for_mass1(100756), 33583);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn example2() {
|
|
|
|
|
// Original descriptions:
|
|
|
|
|
// > A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
|
|
|
|
|
// > At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
|
|
|
|
|
// > The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
|
|
|
|
|
assert_eq!(super::fuel_for_mass2(14), 2);
|
|
|
|
|
assert_eq!(super::fuel_for_mass2(1969), 966);
|
|
|
|
|
assert_eq!(super::fuel_for_mass2(100756), 50346);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|