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

use std::collections::BTreeMap;



fn main() -> Result<()> {
  let args = std::env::args();
  if args.len() != 1 {
    eprintln!("Usage: advent");
  }

  let starting_numbers = vec![2, 15, 0, 9, 1, 20];

  let mut history: BTreeMap<usize, usize> = BTreeMap::new();
  let mut output = 0;
  let mut next_output = 0;

  for i in 0 .. starting_numbers.len() {
    output = starting_numbers[i];
    next_output = match history.get(&output) {
      Some(previous) => i - previous,
      None => 0,
    };
    history.insert(output, i);
  }

  for i in starting_numbers.len() .. 2020 {
    output = next_output;
    next_output = match history.get(&output) {
      Some(previous) => i - previous,
      None => 0,
    };
    history.insert(output, i);
  }

  println!("{}", output);

  for i in 2020 .. 30000000 {
    output = next_output;
    next_output = match history.get(&output) {
      Some(previous) => i - previous,
      None => 0,
    };
    history.insert(output, i);
  }

  println!("{}", output);

  Ok(())
}