summary refs log tree commit diff
path: root/07/src/main.rs
blob: a22129ae3545a6c20acb627a30d371bb6f488a81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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(())
}