diff --git a/december_7/src/main.rs b/december_7/src/main.rs index e7a11a9..b9695a3 100644 --- a/december_7/src/main.rs +++ b/december_7/src/main.rs @@ -1,3 +1,33 @@ +use std::{fs::read_to_string}; + fn main() { - println!("Hello, world!"); + let input_char_vec = read_to_string("./input.txt").expect("ERROR reading file"); + let split_input = input_char_vec.split(",").collect::>(); + + let mut min_fuel = u32::MAX; + let mut cheapest_position: u32 = 0; + + let crab_positions: Vec = split_input.iter().map( + |x| x.parse::().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); }