summary refs log tree commit diff
path: root/07/src/main.rs
blob: e8e12b5986a8fdf1aa32f7c7ec12594452af2d89 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use advent_lib::prelude::*;

use std::collections::BTreeMap;
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 input = advent_lib::read_lines_file(&filename)?;

  let mut rules: BTreeMap<String, BTreeSet<(i64, String)>> =
      BTreeMap::new();

  for line in &input {
    let mut words = Vec::new();
    for word in line.split(' ') {
      words.push(word);
    }

    let key = two_words(words[0], words[1]);
    let mut value = BTreeSet::new();

    if words[4] != "no" {
      let mut i = 4;
      while i + 2 < words.len() {
        let quantity = words[i].parse::<i64>()?;
        let color = two_words(words[i+1], words[i+2]);
        value.insert((quantity, color));
        i += 4;
      }
    }

    rules.insert(key, value);
  }

  let mut closure: BTreeSet<String> = BTreeSet::new();

  for (container, rhs) in rules.iter() {
    for (_, containee) in rhs.iter() {
      if containee == "shiny gold" {
        closure.insert(container.to_string());
      }
    }
  }

  loop {
    let mut added_any = false;
    for (container, rhs) in rules.iter() {
      if closure.contains(container) {
        continue;
      }

      for (_, containee) in rhs.iter() {
        if closure.contains(containee) {
          closure.insert(container.to_string());
          added_any = true;
        }
      }
    }

    if !added_any {
      break;
    }
  }

  println!("{}", closure.len());

  println!("{}", recursive_count(&rules, "shiny gold".to_string()) - 1);

  Ok(())
}


fn two_words(a: &str, b: &str) -> String {
  let mut result = String::new();
  result.push_str(a);
  result.push_str(" ");
  result.push_str(b);
  result
}


fn recursive_count(rules: &BTreeMap<String, BTreeSet<(i64, String)>>, container: String) -> i64 {
  let mut sum = 1;

  let rhs: &BTreeSet<(i64, String)> = rules.get(&container).unwrap();
  for (quantity, containee) in rhs.iter() {
    let count_below = recursive_count(rules, containee.to_string());
    sum += quantity * count_below;
  }

  sum
}