1
0
Fork 0
This commit is contained in:
Lars Martens 2021-12-07 08:06:09 +01:00
parent 4b4d4bdadd
commit 14f660b50f
3 changed files with 29 additions and 0 deletions

27
src/bin/07.rs Normal file
View file

@ -0,0 +1,27 @@
use std::iter;
fn main() {
println!("First solution: {}", find_fuel_min(|n| n));
println!("Second solution: {}", find_fuel_min(|n| n * (n + 1) / 2));
}
fn find_fuel_min(cost: fn(i64) -> i64) -> i64 {
let mut min = i64::MAX;
let crabs = input();
for i in 0..3000 {
let fuel = crabs
.iter()
.zip(iter::repeat(i))
.map(|(&a, b)| cost((a - b).abs()))
.sum();
min = min.min(fuel);
}
min
}
fn input() -> Vec<i64> {
include_str!("../../input/07.txt")
.split(',')
.map(|n| n.parse().unwrap())
.collect()
}