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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::collections::{BTreeMap, HashMap};

use advent_of_code::errors::{Error, Result};

use self::monkey::Monkey;

mod monkey;
mod operation;
mod test;

pub fn solution_pt1<S: AsRef<str>, L: Iterator<Item = S>>(lines: L) -> Result<usize> {
    let mut lines = lines.filter(|l| !l.as_ref().trim().is_empty());
    let mut monkies = BTreeMap::new();
    let mut visited = HashMap::new();

    while let Ok(monkey) = Monkey::from_lines(&mut lines) {
        visited.insert(monkey.index, 0);
        monkies.insert(monkey.index, monkey);
    }

    // run rounds
    for _ in 0..1 {
        let indices: Vec<usize> = monkies.keys().copied().collect();
        for i in indices {
            let monkey = monkies.get_mut(&i).unwrap();
            let thrown = monkey.get_thrown().unwrap();
            let active = visited.get_mut(&monkey.index).unwrap();
            *active += thrown.len();
            for (item, to) in thrown {
                monkies.get_mut(&to).unwrap().add_item(item);
            }
            println!("{}", i);
            for (_, m) in monkies.iter() {
                println!("\t{}", m);
            }
        }
    }

    let mut active: Vec<usize> = visited.into_iter().map(|(_, v)| v).collect();

    active.sort_unstable();
    active.reverse();
    let active = active[0] * active[1];

    Ok(active)
}

pub fn solution_pt2<S: AsRef<str>, L: Iterator<Item = S>>(lines: L) -> Result<usize> {
    let mut lines = lines.filter(|l| !l.as_ref().trim().is_empty());
    let mut monkies = BTreeMap::new();
    let mut visited = BTreeMap::new();

    while let Ok(monkey) = Monkey::from_lines(&mut lines) {
        visited.insert(monkey.index, 0);
        monkies.insert(monkey.index, monkey);
    }

    let mut common_denom = 1;

    for (_, m) in monkies.iter() {
        common_denom *= m.test.divisor as i64;
    }

    // run rounds
    for _ in 0..10_000 {
        let indices: Vec<usize> = monkies.keys().copied().collect();
        for i in indices {
            let monkey = monkies.get_mut(&i).unwrap();
            let thrown = monkey.get_thrown_without_worry(common_denom).unwrap();
            let active = visited.get_mut(&monkey.index).unwrap();
            *active += thrown.len();
            for (item, to) in thrown {
                monkies.get_mut(&to).unwrap().add_item(item);
            }

            // println!("{}", i);
            // for (_, m) in monkies.iter() {
            //     println!("{}", m);
            // }
        }
    }

    let mut active: Vec<usize> = visited.into_iter().map(|(_, v)| v).collect();

    active.sort_unstable();
    active.reverse();
    let active = active[0] * active[1];

    Ok(active)
}

#[cfg(test)]
mod tests {
    use super::{solution_pt1, solution_pt2};

    const PAGE_EXAMPLE: &str = r#"
Monkey 0:
  Starting items: 79, 98
  Operation: new = old * 19
  Test: divisible by 23
    If true: throw to monkey 2
    If false: throw to monkey 3

Monkey 1:
  Starting items: 54, 65, 75, 74
  Operation: new = old + 6
  Test: divisible by 19
    If true: throw to monkey 2
    If false: throw to monkey 0

Monkey 2:
  Starting items: 79, 60, 97
  Operation: new = old * old
  Test: divisible by 13
    If true: throw to monkey 1
    If false: throw to monkey 3

Monkey 3:
  Starting items: 74
  Operation: new = old + 3
  Test: divisible by 17
    If true: throw to monkey 0
    If false: throw to monkey 1
"#;

    #[test]
    fn page_example_1() {
        let lines = PAGE_EXAMPLE.lines();
        let actual = solution_pt1(lines).unwrap();
        assert_eq!(actual, 10605)
    }

    #[test]
    fn page_example_2() {
        let lines = PAGE_EXAMPLE.lines();
        let actual = solution_pt2(lines).unwrap();
        assert_eq!(actual, 2713310158)
    }
}