2022-12-14 16:55:01 -05:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
use std::ops::*;
|
|
|
|
|
2022-12-15 08:02:20 -05:00
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
|
|
|
enum WorryType {
|
|
|
|
Normal,
|
|
|
|
Extra,
|
|
|
|
}
|
|
|
|
|
2022-12-14 16:55:01 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct Operation {
|
|
|
|
operator: char,
|
|
|
|
operand: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Operation {
|
2022-12-15 08:02:20 -05:00
|
|
|
fn run(&self, old: u128) -> u128 {
|
2022-12-14 16:55:01 -05:00
|
|
|
let other = match self.operand.as_str() {
|
|
|
|
"old" => old,
|
2022-12-15 08:02:20 -05:00
|
|
|
_ => self.operand.parse::<u128>().unwrap(),
|
2022-12-14 16:55:01 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
match self.operator {
|
|
|
|
'+' => old.add(other),
|
|
|
|
'-' => old.sub(other),
|
|
|
|
'*' => old.mul(other),
|
|
|
|
_ => panic!("Invalid operator"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-14 13:16:17 -05:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Monkey {
|
2022-12-15 08:02:20 -05:00
|
|
|
items: VecDeque<u128>,
|
2022-12-14 16:55:01 -05:00
|
|
|
operation: Operation,
|
|
|
|
test: usize,
|
|
|
|
pass_monkey: usize,
|
|
|
|
fail_monkey: usize,
|
2022-12-15 08:02:20 -05:00
|
|
|
inspection_count: u128,
|
|
|
|
inspection_worry: WorryType,
|
2022-12-14 16:55:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Monkey {
|
2022-12-15 08:02:20 -05:00
|
|
|
fn from_behavior(raw: &str, inspection_worry: WorryType) -> Self {
|
2022-12-14 16:55:01 -05:00
|
|
|
let lines: Vec<&str> = raw.lines().collect();
|
|
|
|
|
|
|
|
let item_parts: Vec<&str> = lines[1].split(": ").collect();
|
2022-12-15 08:02:20 -05:00
|
|
|
let items: VecDeque<u128> = item_parts[1]
|
2022-12-14 16:55:01 -05:00
|
|
|
.split(", ")
|
2022-12-15 08:02:20 -05:00
|
|
|
.map(|i| i.parse::<u128>().unwrap())
|
2022-12-14 16:55:01 -05:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let op_parts: Vec<&str> = lines[2].split(" = ").collect();
|
|
|
|
let [_, operator, operand]: [&str; 3] = op_parts[1]
|
|
|
|
.split_ascii_whitespace()
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let [test, pass_monkey, fail_monkey]: [usize; 3] = lines[3..]
|
|
|
|
.iter()
|
|
|
|
.map(|line| line.split_ascii_whitespace().collect::<Vec<&str>>())
|
|
|
|
.map(|parts| parts[parts.len() - 1])
|
|
|
|
.map(|n| n.parse::<usize>().unwrap())
|
|
|
|
.collect::<Vec<usize>>()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Monkey {
|
|
|
|
items,
|
|
|
|
operation: Operation {
|
|
|
|
operator: operator.chars().next().unwrap(),
|
|
|
|
operand: operand.to_string(),
|
|
|
|
},
|
|
|
|
test,
|
|
|
|
pass_monkey,
|
|
|
|
fail_monkey,
|
|
|
|
inspection_count: 0,
|
2022-12-15 08:02:20 -05:00
|
|
|
inspection_worry,
|
2022-12-14 16:55:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 08:02:20 -05:00
|
|
|
fn run_test(&self, item: u128) -> usize {
|
|
|
|
if item % (self.test as u128) == 0 {
|
2022-12-14 16:55:01 -05:00
|
|
|
self.pass_monkey
|
|
|
|
} else {
|
|
|
|
self.fail_monkey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 08:02:20 -05:00
|
|
|
pub fn inspect(&mut self, item: u128) -> (usize, u128) {
|
2022-12-14 16:55:01 -05:00
|
|
|
self.inspection_count += 1;
|
|
|
|
|
2022-12-15 08:02:20 -05:00
|
|
|
let worry = if self.inspection_worry == WorryType::Normal {
|
|
|
|
self.operation.run(item) / 3
|
|
|
|
} else {
|
|
|
|
self.operation.run(item)
|
|
|
|
};
|
2022-12-14 16:55:01 -05:00
|
|
|
|
|
|
|
let new_monkey = self.run_test(worry);
|
|
|
|
|
|
|
|
(new_monkey, worry)
|
|
|
|
}
|
|
|
|
|
2022-12-15 08:02:20 -05:00
|
|
|
pub fn catch(&mut self, item: u128) {
|
2022-12-14 16:55:01 -05:00
|
|
|
self.items.push_back(item);
|
|
|
|
}
|
2022-12-14 13:16:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct MonkeyGame {
|
|
|
|
monkeys: Vec<Monkey>,
|
|
|
|
}
|
|
|
|
|
2022-12-14 16:55:01 -05:00
|
|
|
impl MonkeyGame {
|
2022-12-15 08:02:20 -05:00
|
|
|
pub fn from_file_str(file_str: &'static str, inspection_worry: WorryType) -> Self {
|
2022-12-14 16:55:01 -05:00
|
|
|
let behaviors = file_str.split("\n\n");
|
|
|
|
|
|
|
|
Self {
|
2022-12-15 08:02:20 -05:00
|
|
|
monkeys: behaviors
|
|
|
|
.map(|m| Monkey::from_behavior(m, inspection_worry))
|
|
|
|
.collect(),
|
2022-12-14 16:55:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 08:02:20 -05:00
|
|
|
fn throw(&mut self, item: u128, to: usize) {
|
2022-12-14 16:55:01 -05:00
|
|
|
self.monkeys[to].catch(item);
|
|
|
|
}
|
|
|
|
|
2022-12-14 22:01:54 -05:00
|
|
|
fn do_round(&mut self) {
|
2022-12-14 16:55:01 -05:00
|
|
|
for m in 0..self.monkeys.len() {
|
|
|
|
while let Some(worry) = self.monkeys[m].items.pop_front() {
|
|
|
|
let (monkey_idx, worry) = self.monkeys[m].inspect(worry);
|
|
|
|
self.throw(worry, monkey_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-14 22:01:54 -05:00
|
|
|
|
|
|
|
pub fn do_rounds(&mut self, rounds: usize) {
|
|
|
|
for _ in 0..rounds {
|
|
|
|
self.do_round();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 08:02:20 -05:00
|
|
|
pub fn get_inspection_counts(&self) -> Vec<u128> {
|
|
|
|
let mut counts: Vec<u128> = self.monkeys.iter().map(|m| m.inspection_count).collect();
|
2022-12-14 22:01:54 -05:00
|
|
|
|
|
|
|
counts.sort();
|
|
|
|
|
|
|
|
counts.into_iter().rev().collect()
|
|
|
|
}
|
2022-12-14 16:55:01 -05:00
|
|
|
}
|
|
|
|
|
2022-12-14 13:16:17 -05:00
|
|
|
fn main() {
|
|
|
|
let file_str = include_str!("input.txt");
|
2022-12-15 08:02:20 -05:00
|
|
|
let mut game = MonkeyGame::from_file_str(file_str, WorryType::Normal);
|
2022-12-14 22:01:54 -05:00
|
|
|
game.do_rounds(20);
|
2022-12-14 16:55:01 -05:00
|
|
|
|
2022-12-14 22:01:54 -05:00
|
|
|
let inspections = game.get_inspection_counts();
|
|
|
|
let monkey_business = inspections[0] * inspections[1];
|
|
|
|
|
|
|
|
println!("Part 1 monkey business: {}", monkey_business);
|
2022-12-15 08:02:20 -05:00
|
|
|
|
|
|
|
// let mut game = MonkeyGame::from_file_str(file_str, WorryType::Extra);
|
|
|
|
// game.do_rounds(10_000);
|
|
|
|
|
|
|
|
// let inspections = game.get_inspection_counts();
|
|
|
|
// let monkey_business = inspections[0] * inspections[1];
|
|
|
|
|
|
|
|
// println!("Part 2 monkey business: {}", monkey_business);
|
2022-12-14 13:16:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn get_test_data() -> &'static str {
|
|
|
|
include_str!("test-input.txt")
|
|
|
|
}
|
2022-12-14 16:55:01 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_monkey_round() {
|
2022-12-15 08:02:20 -05:00
|
|
|
let mut game = MonkeyGame::from_file_str(get_test_data(), WorryType::Normal);
|
2022-12-14 16:55:01 -05:00
|
|
|
game.do_round();
|
|
|
|
|
|
|
|
assert_eq!(game.monkeys[0].items, VecDeque::from([20, 23, 27, 26]));
|
|
|
|
assert_eq!(
|
|
|
|
game.monkeys[1].items,
|
|
|
|
VecDeque::from([2080, 25, 167, 207, 401, 1046])
|
|
|
|
);
|
|
|
|
assert_eq!(game.monkeys[2].items, VecDeque::new());
|
|
|
|
assert_eq!(game.monkeys[3].items, VecDeque::new());
|
|
|
|
}
|
2022-12-14 22:01:54 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_monkey_20_rounds() {
|
2022-12-15 08:02:20 -05:00
|
|
|
let mut game = MonkeyGame::from_file_str(get_test_data(), WorryType::Normal);
|
2022-12-14 22:01:54 -05:00
|
|
|
game.do_rounds(20);
|
|
|
|
|
|
|
|
assert_eq!(game.monkeys[0].inspection_count, 101);
|
|
|
|
assert_eq!(game.monkeys[3].inspection_count, 105);
|
|
|
|
}
|
2022-12-14 16:55:01 -05:00
|
|
|
}
|