summary refs log tree commit diff
path: root/10/src/main.rs
blob: d50471143f57d2637d09a5d68487c5c6a77dc963 (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::*;

use std::convert::TryFrom;


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 adaptors: Vec<i64> = Vec::new();
  for line in &input {
    adaptors.push(line.parse::<i64>().unwrap());
  }
  adaptors.sort();

  let mut buckets = Vec::new();
  buckets.push(0);
  buckets.push(0);
  buckets.push(0);
  buckets.push(0);

  for i in 0 .. adaptors.len() {
    let mut prev_joltage = 0;
    if i > 0 {
      prev_joltage = adaptors[i - 1];
    }
    let difference = adaptors[i] - prev_joltage;
    buckets[usize::try_from(difference).unwrap()] += 1;
  }

  buckets[3] += 1;

  println!("{}", buckets[1]*buckets[3]);

  let mut reversed_adaptors = Vec::new();
  for i in 0 .. adaptors.len() {
    reversed_adaptors.push(adaptors[adaptors.len() - i - 1]);
  }
  reversed_adaptors.push(0);

  let mut memoized_counts: Vec<i64> = Vec::new();
  memoized_counts.push(1);

  for i in 1 .. reversed_adaptors.len() {
    let mut count = 0;
    for j in 1 .. i+1 {
      let difference = reversed_adaptors[i - j] - reversed_adaptors[i];
      if difference <= 3 {
        count += memoized_counts[i - j];
      } else {
        break;
      }
    }
    memoized_counts.push(count);
  }

  println!("{}", memoized_counts[memoized_counts.len()-1]);

  Ok(())
}