diff options
Diffstat (limited to '07/src')
-rw-r--r-- | 07/src/main.rs | 58 |
1 files changed, 58 insertions, 0 deletions
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::<i64>()?; + 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(()) +} |