summary refs log tree commit diff
path: root/01/src/main.rs
blob: 5c4348bdc7fbaa3a66f92ab84499559e76d5ad3e (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
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_int_file(&filename)?;

  {
    let mut last_item = None;
    let mut n_increases = 0;
    for item in &input {
      match last_item {
        Some(value) => {
          if *item > value {
            n_increases += 1;
          }
        }
        None => { }
      }

      last_item = Some(*item);
    }

    println!("{}", n_increases);
  }

  {
    let mut window = Vec::new();
    let mut last_sum = None;
    let mut n_increases = 0;
    for item in &input {
      window.push(item);
      if window.len() > 3 {
        let _ = window.remove(0);
      }

      if window.len() == 3 {
        let mut sum = 0;
        for a in &window {
          sum += *a;
        }

        match last_sum {
          None => { }
          Some(value) => {
            if sum > value {
              n_increases += 1;
            }
          }
        }

        last_sum = Some(sum);
      }
    }

    println!("{}", n_increases);
  }

  Ok(())
}