summary refs log tree commit diff
path: root/01/src/main.rs
blob: b7cdbbfa91c4db3e214bb1ba7b81297ade1adbc0 (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
use advent_lib::prelude::*;

use std::collections::BTreeSet;


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 mut input = advent_lib::read_int_file(&filename)?;

  input.sort();

  let mut input_set = BTreeSet::new();
  for item in &input {
    input_set.insert(item);
  }

  for i in 0 .. input.len() {
    let a = input[i];
    if a > 2020 {
      break;
    }

    let b = 2020 - a;
    if input_set.contains(&b) {
      let product = a * b;
      println!("a: {:?}, b: {:?}, a*b: {:?}", a, b, product);
      break;
    }
  }

  let mut done = false;
  for i in 0 .. input.len() {
    if done {
      break;
    }

    let a = input[i];
    if a > 2020 {
      break;
    }

    for j in i+1 .. input.len() {
      let b = input[j];

      if a + b > 2020 {
        break;
      }

      let c = 2020 - a - b;
      if input_set.contains(&c) {
        let product = a * b * c;
        println!("a: {:?}, b: {:?}, c: {:?}, a*b*c: {:?}", a, b, c, product);

        done = true;
        break;
      }
    }
  }

  Ok(())
}