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
use std::{
    collections::{HashMap, VecDeque},
    fmt::Display,
};

use super::{operation::Operation, test::Test};
use advent_of_code::errors::{Error, Result};

#[derive(Debug)]
pub struct Monkey {
    pub index: usize,
    item_worry_levels: VecDeque<i64>,
    operation: Operation,
    pub test: Test,
}

impl Monkey {
    pub fn get_thrown(&mut self) -> Option<Vec<(i64, usize)>> {
        let mut variables = HashMap::new();
        let mut output = Vec::with_capacity(self.item_worry_levels.capacity());
        while let Some(item) = self.item_worry_levels.pop_front() {
            variables.insert("old", item);
            let new = self.operation.apply(&variables)? / 3;
            let to = self.test.test(new);
            output.push((new, to));
        }

        Some(output)
    }

    pub fn get_thrown_without_worry(&mut self, divisor: i64) -> Option<Vec<(i64, usize)>> {
        let mut variables = HashMap::new();
        let mut output = Vec::with_capacity(self.item_worry_levels.capacity());
        while let Some(item) = self.item_worry_levels.pop_front() {
            variables.insert("old", item);
            let new = self.operation.apply_mod(divisor, &variables)?;
            let to = self.test.test(new);
            output.push((new, to));
        }

        Some(output)
    }

    pub fn add_item(&mut self, item: i64) {
        self.item_worry_levels.push_back(item);
    }

    pub fn from_lines<S: AsRef<str>, L: Iterator<Item = S>>(lines: &mut L) -> Result<Self> {
        let need_field = || Error::InvalidParseError("Need field to make a monkey".to_owned());
        let index = lines
            .next()
            .ok_or_else(need_field)?
            .as_ref()
            .trim()
            .split_whitespace()
            .last()
            .ok_or_else(need_field)?
            .split_once(':')
            .ok_or_else(need_field)?
            .0
            .parse()?;

        let item_worry_levels = lines
            .next()
            .ok_or_else(need_field)?
            .as_ref()
            .trim()
            .split_once(':')
            .ok_or_else(need_field)?
            .1
            .split(',')
            .filter(|s| !s.is_empty())
            .map(|s| s.trim().parse::<i64>().map_err(|e| e.into()))
            .collect::<Result<_>>()?;

        let operation = lines
            .next()
            .ok_or_else(need_field)?
            .as_ref()
            .trim()
            .split_once(' ')
            .ok_or_else(need_field)?
            .1
            .split_once('=')
            .ok_or_else(need_field)?
            .1
            .trim()
            .parse()?;

        let test = Test::from_lines(lines)?;

        Ok(Monkey {
            index,
            item_worry_levels,
            operation,
            test,
        })
    }
}

impl Display for Monkey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "M {}: {:?}", self.index, self.item_worry_levels)
    }
}