From 8079dd2a895f8a55368761940eba5670e24d0411 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Tue, 7 Dec 2021 03:14:11 -0800 Subject: 07 --- 07/src/main.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 07/src/main.rs (limited to '07/src') diff --git a/07/src/main.rs b/07/src/main.rs new file mode 100644 index 0000000..a22129a --- /dev/null +++ b/07/src/main.rs @@ -0,0 +1,58 @@ +use advent_lib::prelude::*; + + +fn main() -> Result<()> { + let mut args = std::env::args(); + if args.len() != 2 { + eprintln!("Usage: advent input"); + } + let _ = args.next(); + let filename = args.next().unwrap(); + + let input = advent_lib::read_lines_file(&filename)?; + + let mut start_positions = Vec::new(); + for word in input[0].split(',') { + let position = word.parse::()?; + start_positions.push(position); + } + start_positions.sort(); + + { + let median = start_positions[start_positions.len() / 2]; + let mut cost = 0; + for position in &start_positions { + cost += (median - position).abs(); + } + println!("{}", cost); + } + + { + let min = start_positions[0]; + let max = start_positions[start_positions.len() - 1]; + let mut best_cost = None; + + for destination in min .. max + 1 { + let mut cost = 0; + for position in &start_positions { + let distance = (destination - position).abs(); + cost += distance * (distance + 1) / 2; + } + + match best_cost { + None => { + best_cost = Some(cost); + }, + Some(old_best) => { + if cost < old_best { + best_cost = Some(cost); + } + }, + } + } + + println!("{}", best_cost.unwrap()); + } + + Ok(()) +} -- cgit 1.4.1