summary refs log tree commit diff
path: root/09/src/main.rs
blob: ca978cbe1e59750f557283d148afe7e5474120e5 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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 ciphertext: Vec<i64> = Vec::new();
  for line in &input {
    ciphertext.push(line.parse::<i64>().unwrap());
  }

  let mut first_key = 0;

  for i in 25 .. ciphertext.len() {
    let sum = ciphertext[i];
    let mut found_addends = false;

    for j in 0 .. 25 {
      let a = ciphertext[i - 25 + j];
      let b = sum - a;
      for k in j + 1 .. 25 {
        if ciphertext[i - 25 + k] == b {
          found_addends = true;
          break;
        }
      }
      if found_addends {
        break;
      }
    }

    if !found_addends {
      println!("{}", sum);
      first_key = sum;
      break;
    }
  }

  for run_length in 2 .. ciphertext.len() {
    let mut sum = 0;
    let found_solution = false;

    for i in 0 .. ciphertext.len() - run_length {
      sum += ciphertext[i];

      if i >= run_length {
        sum -= ciphertext[i - run_length];
      }

      if sum == first_key {
        let mut min = 0;
        let mut max = 0;

        for j in 0 .. run_length {
          let item = ciphertext[i - run_length + j + 1];
          if j == 0 || item < min {
            min = item;
          }
          if j == 0 || item > max {
            max = item;
          }
        }

        println!("{}", min + max);
        break;
      }
    }

    if found_solution {
      break;
    }
  }

  Ok(())
}