use std::error::Error; use std::fs::File; use std::io::{self, BufRead}; use std::vec::Vec; fn main() -> Result<(), Box> { let file = File::open("inputs/day7.txt")?; let crabs: Vec = io::BufReader::new(file) .lines() .nth(0) .unwrap() .unwrap() .split(",") .map(|f| f.parse().unwrap()) .collect(); let max_crab = crabs.iter().max().unwrap(); println!("Max pos: {}", max_crab); let mut min_fuel = i64::max_value(); for aligned_pos in 0..max_crab + 1 { let mut fuel = 0; for crab in crabs.iter() { fuel += (aligned_pos - crab).abs(); } if fuel < min_fuel { min_fuel = fuel; } println!("{} : {}", aligned_pos, fuel); } println!("Answer1: {}", min_fuel); let mut min_fuel = i64::max_value(); for aligned_pos in 0..max_crab + 1 { let mut fuel = 0; for crab in crabs.iter() { let n = (aligned_pos - crab).abs(); // Gauß fuel fuel += (n * (n + 1)) / 2; } if fuel < min_fuel { min_fuel = fuel; } println!("{} : {}", aligned_pos, fuel); } println!("Answer2: {}", min_fuel); Ok(()) }