34 lines
1.1 KiB
Rust
34 lines
1.1 KiB
Rust
use std::{fs::read_to_string};
|
|
|
|
fn main() {
|
|
let input_char_vec = read_to_string("./input.txt").expect("ERROR reading file");
|
|
let split_input = input_char_vec.split(",").collect::<Vec<&str>>();
|
|
|
|
let mut min_fuel = u32::MAX;
|
|
let mut cheapest_position: u32 = 0;
|
|
|
|
let crab_positions: Vec<u32> = split_input.iter().map(
|
|
|x| x.parse::<u32>().unwrap()
|
|
).collect();
|
|
let max_depth = crab_positions.iter().max().unwrap().to_owned();
|
|
|
|
|
|
for test_target_depth in 0..=max_depth {
|
|
let mut needed_fuel_for_depth = 0;
|
|
for crab in crab_positions.iter() {
|
|
let distance = (test_target_depth as i32 - crab.to_owned() as i32).abs() as u32;
|
|
let mut needed_fuel = 0;
|
|
for d in 0..=distance {
|
|
needed_fuel += d;
|
|
}
|
|
needed_fuel_for_depth += needed_fuel;
|
|
}
|
|
if needed_fuel_for_depth < min_fuel {
|
|
min_fuel = needed_fuel_for_depth;
|
|
cheapest_position = test_target_depth;
|
|
}
|
|
}
|
|
|
|
println!("Cheapest Postion is {} with a fuel usage of {}", cheapest_position, min_fuel);
|
|
}
|