Day 11, puzzle 1
This commit is contained in:
		
							
								
								
									
										239
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										239
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							@ -15,9 +15,137 @@ dependencies = [
 | 
			
		||||
name = "aoc22"
 | 
			
		||||
version = "0.1.0"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "env_logger",
 | 
			
		||||
 "itertools",
 | 
			
		||||
 "log",
 | 
			
		||||
 "regex",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "bitflags"
 | 
			
		||||
version = "1.3.2"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "cc"
 | 
			
		||||
version = "1.0.78"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "cfg-if"
 | 
			
		||||
version = "1.0.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "either"
 | 
			
		||||
version = "1.8.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "env_logger"
 | 
			
		||||
version = "0.10.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "humantime",
 | 
			
		||||
 "is-terminal",
 | 
			
		||||
 "log",
 | 
			
		||||
 "regex",
 | 
			
		||||
 "termcolor",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "errno"
 | 
			
		||||
version = "0.2.8"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "errno-dragonfly",
 | 
			
		||||
 "libc",
 | 
			
		||||
 "winapi",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "errno-dragonfly"
 | 
			
		||||
version = "0.1.2"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "cc",
 | 
			
		||||
 "libc",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "hermit-abi"
 | 
			
		||||
version = "0.2.6"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "libc",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "humantime"
 | 
			
		||||
version = "2.1.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "io-lifetimes"
 | 
			
		||||
version = "1.0.3"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "libc",
 | 
			
		||||
 "windows-sys",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "is-terminal"
 | 
			
		||||
version = "0.4.1"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "927609f78c2913a6f6ac3c27a4fe87f43e2a35367c0c4b0f8265e8f49a104330"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "hermit-abi",
 | 
			
		||||
 "io-lifetimes",
 | 
			
		||||
 "rustix",
 | 
			
		||||
 "windows-sys",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "itertools"
 | 
			
		||||
version = "0.10.5"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "either",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "libc"
 | 
			
		||||
version = "0.2.138"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "linux-raw-sys"
 | 
			
		||||
version = "0.1.4"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "log"
 | 
			
		||||
version = "0.4.17"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "cfg-if",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "memchr"
 | 
			
		||||
version = "2.5.0"
 | 
			
		||||
@ -40,3 +168,114 @@ name = "regex-syntax"
 | 
			
		||||
version = "0.6.28"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "rustix"
 | 
			
		||||
version = "0.36.5"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "a3807b5d10909833d3e9acd1eb5fb988f79376ff10fce42937de71a449c4c588"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "bitflags",
 | 
			
		||||
 "errno",
 | 
			
		||||
 "io-lifetimes",
 | 
			
		||||
 "libc",
 | 
			
		||||
 "linux-raw-sys",
 | 
			
		||||
 "windows-sys",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "termcolor"
 | 
			
		||||
version = "1.1.3"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "winapi-util",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "winapi"
 | 
			
		||||
version = "0.3.9"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "winapi-i686-pc-windows-gnu",
 | 
			
		||||
 "winapi-x86_64-pc-windows-gnu",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "winapi-i686-pc-windows-gnu"
 | 
			
		||||
version = "0.4.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "winapi-util"
 | 
			
		||||
version = "0.1.5"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "winapi",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "winapi-x86_64-pc-windows-gnu"
 | 
			
		||||
version = "0.4.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "windows-sys"
 | 
			
		||||
version = "0.42.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "windows_aarch64_gnullvm",
 | 
			
		||||
 "windows_aarch64_msvc",
 | 
			
		||||
 "windows_i686_gnu",
 | 
			
		||||
 "windows_i686_msvc",
 | 
			
		||||
 "windows_x86_64_gnu",
 | 
			
		||||
 "windows_x86_64_gnullvm",
 | 
			
		||||
 "windows_x86_64_msvc",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "windows_aarch64_gnullvm"
 | 
			
		||||
version = "0.42.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "windows_aarch64_msvc"
 | 
			
		||||
version = "0.42.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "windows_i686_gnu"
 | 
			
		||||
version = "0.42.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "windows_i686_msvc"
 | 
			
		||||
version = "0.42.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "windows_x86_64_gnu"
 | 
			
		||||
version = "0.42.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "windows_x86_64_gnullvm"
 | 
			
		||||
version = "0.42.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "windows_x86_64_msvc"
 | 
			
		||||
version = "0.42.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
 | 
			
		||||
 | 
			
		||||
@ -7,6 +7,9 @@ edition = "2021"
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
regex = "1"
 | 
			
		||||
itertools = "^0.10"
 | 
			
		||||
log = "^0.4"
 | 
			
		||||
env_logger = "^0.10"
 | 
			
		||||
 | 
			
		||||
[[bin]]
 | 
			
		||||
name = "d1p1"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										22
									
								
								src/bin/d11p1.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/bin/d11p1.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
			
		||||
use aoc22::{
 | 
			
		||||
    day11::{self, monkey_business},
 | 
			
		||||
    util,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const N_ROUNDS: usize = 20;
 | 
			
		||||
 | 
			
		||||
pub fn main() {
 | 
			
		||||
    env_logger::init();
 | 
			
		||||
 | 
			
		||||
    let monkeys = day11::parse_monkeys(&util::parse_input());
 | 
			
		||||
 | 
			
		||||
    for _ in 0..20 {
 | 
			
		||||
        day11::do_round(&monkeys);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    println!(
 | 
			
		||||
        "Monkey business after {} rounds is {}",
 | 
			
		||||
        N_ROUNDS,
 | 
			
		||||
        monkey_business(&monkeys)
 | 
			
		||||
    );
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										199
									
								
								src/day11.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										199
									
								
								src/day11.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,199 @@
 | 
			
		||||
use std::{borrow::BorrowMut, cell::RefCell, collections::VecDeque};
 | 
			
		||||
 | 
			
		||||
use itertools::Itertools;
 | 
			
		||||
use regex::Regex;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub enum OpKind {
 | 
			
		||||
    Add,
 | 
			
		||||
    Mul,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub enum OpParam {
 | 
			
		||||
    Old,
 | 
			
		||||
    Num(usize),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct Operation {
 | 
			
		||||
    pub kind: OpKind,
 | 
			
		||||
    pub a: OpParam,
 | 
			
		||||
    pub b: OpParam,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct Test {
 | 
			
		||||
    pub div: usize,
 | 
			
		||||
    pub target_true: usize,
 | 
			
		||||
    pub target_false: usize,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
pub struct Monkey {
 | 
			
		||||
    pub activity: usize,
 | 
			
		||||
    pub items: VecDeque<usize>,
 | 
			
		||||
    pub op: Operation,
 | 
			
		||||
    pub test: Test,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn parse_monkeys(input: &String) -> Vec<RefCell<Monkey>> {
 | 
			
		||||
    let mut result = Vec::new();
 | 
			
		||||
 | 
			
		||||
    for (n, monkey) in input.lines().chunks(7).into_iter().enumerate() {
 | 
			
		||||
        let (n_actual, monkey) = parse_monkey(&monkey.collect());
 | 
			
		||||
        assert_eq!(n, n_actual);
 | 
			
		||||
        result.push(RefCell::new(monkey));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    result
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn parse_monkey(lines: &Vec<&str>) -> (usize, Monkey) {
 | 
			
		||||
    let re0 = Regex::new(r"^Monkey (\d)+:$").unwrap();
 | 
			
		||||
    let re1 = Regex::new(r"^  Starting items: ((\d+(?:, )?)+)$").unwrap();
 | 
			
		||||
    let re2 = Regex::new(r"^  Operation: new = (old|\d+) ([*+]) (old|\d+)$").unwrap();
 | 
			
		||||
    let re3 = Regex::new(r"^  Test: divisible by (\d+)$").unwrap();
 | 
			
		||||
    let re45 = Regex::new(r"^    If (true|false): throw to monkey (\d+)$").unwrap();
 | 
			
		||||
 | 
			
		||||
    let n: usize = re0
 | 
			
		||||
        .captures(lines.get(0).unwrap())
 | 
			
		||||
        .unwrap()
 | 
			
		||||
        .get(1)
 | 
			
		||||
        .unwrap()
 | 
			
		||||
        .as_str()
 | 
			
		||||
        .parse()
 | 
			
		||||
        .unwrap();
 | 
			
		||||
 | 
			
		||||
    let items = re1
 | 
			
		||||
        .captures(lines.get(1).unwrap())
 | 
			
		||||
        .unwrap()
 | 
			
		||||
        .get(1)
 | 
			
		||||
        .unwrap()
 | 
			
		||||
        .as_str()
 | 
			
		||||
        .split(", ")
 | 
			
		||||
        .map(|i| i.parse().expect(&format!("Invalid item: {}", i)))
 | 
			
		||||
        .collect();
 | 
			
		||||
 | 
			
		||||
    let op = re2.captures(lines.get(2).unwrap()).unwrap();
 | 
			
		||||
    let op_a = parse_op_param(op.get(1).unwrap().as_str());
 | 
			
		||||
    let op_kind = parse_op_kind(op.get(2).unwrap().as_str());
 | 
			
		||||
    let op_b = parse_op_param(op.get(3).unwrap().as_str());
 | 
			
		||||
    let op = Operation {
 | 
			
		||||
        kind: op_kind,
 | 
			
		||||
        a: op_a,
 | 
			
		||||
        b: op_b,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let test_div = re3
 | 
			
		||||
        .captures(lines.get(3).unwrap())
 | 
			
		||||
        .unwrap()
 | 
			
		||||
        .get(1)
 | 
			
		||||
        .unwrap()
 | 
			
		||||
        .as_str()
 | 
			
		||||
        .parse()
 | 
			
		||||
        .unwrap();
 | 
			
		||||
 | 
			
		||||
    let test_true = re45.captures(lines.get(4).unwrap()).unwrap();
 | 
			
		||||
    assert_eq!(test_true.get(1).unwrap().as_str(), "true");
 | 
			
		||||
    let test_true = test_true.get(2).unwrap().as_str().parse().unwrap();
 | 
			
		||||
 | 
			
		||||
    let test_false = re45.captures(lines.get(5).unwrap()).unwrap();
 | 
			
		||||
    assert_eq!(test_false.get(1).unwrap().as_str(), "false");
 | 
			
		||||
    let test_false = test_false.get(2).unwrap().as_str().parse().unwrap();
 | 
			
		||||
 | 
			
		||||
    let test = Test {
 | 
			
		||||
        div: test_div,
 | 
			
		||||
        target_true: test_true,
 | 
			
		||||
        target_false: test_false,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    assert!(lines.len() == 6 || lines.get(6).unwrap().is_empty());
 | 
			
		||||
 | 
			
		||||
    (
 | 
			
		||||
        n,
 | 
			
		||||
        Monkey {
 | 
			
		||||
            activity: 0,
 | 
			
		||||
            items,
 | 
			
		||||
            op,
 | 
			
		||||
            test,
 | 
			
		||||
        },
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn parse_op_param(s: &str) -> OpParam {
 | 
			
		||||
    if s == "old" {
 | 
			
		||||
        OpParam::Old
 | 
			
		||||
    } else {
 | 
			
		||||
        OpParam::Num(s.parse().unwrap())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn parse_op_kind(s: &str) -> OpKind {
 | 
			
		||||
    match s {
 | 
			
		||||
        "+" => OpKind::Add,
 | 
			
		||||
        "*" => OpKind::Mul,
 | 
			
		||||
        _ => panic!("Unknown OpKind {}", s),
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn do_round(monkeys: &Vec<RefCell<Monkey>>) {
 | 
			
		||||
    for n in 0..monkeys.len() {
 | 
			
		||||
        let mut monkey = monkeys.get(n).unwrap().borrow_mut();
 | 
			
		||||
        log::debug!("Monkey {}", n);
 | 
			
		||||
        while !monkey.items.is_empty() {
 | 
			
		||||
            monkey.activity += 1;
 | 
			
		||||
            let mut item = monkey.items.pop_front().unwrap();
 | 
			
		||||
            log::debug!("  Monkey inspects an item with a worry level of {}.", item);
 | 
			
		||||
            item = run_op(&monkey.op, item);
 | 
			
		||||
            log::debug!("    Worry level increases to {}.", item);
 | 
			
		||||
            item /= 3;
 | 
			
		||||
            log::debug!(
 | 
			
		||||
                "    Monkey gets bored with item. Worry level is divided by 3 to {}.",
 | 
			
		||||
                item
 | 
			
		||||
            );
 | 
			
		||||
            let target = if item % monkey.test.div == 0 {
 | 
			
		||||
                log::debug!(
 | 
			
		||||
                    "Current worry level is not divisible by {}",
 | 
			
		||||
                    monkey.test.div
 | 
			
		||||
                );
 | 
			
		||||
                monkey.test.target_true
 | 
			
		||||
            } else {
 | 
			
		||||
                log::debug!("Current worry level is divisible by {}", monkey.test.div);
 | 
			
		||||
                monkey.test.target_false
 | 
			
		||||
            };
 | 
			
		||||
            log::debug!(
 | 
			
		||||
                "Item with worry level {} is thrown to monkey {}.",
 | 
			
		||||
                item,
 | 
			
		||||
                target
 | 
			
		||||
            );
 | 
			
		||||
            let mut target = &mut monkeys.get(target).unwrap().borrow_mut().items;
 | 
			
		||||
            target.push_back(item);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn run_op(op: &Operation, item: usize) -> usize {
 | 
			
		||||
    let a = match op.a {
 | 
			
		||||
        OpParam::Old => item,
 | 
			
		||||
        OpParam::Num(n) => n,
 | 
			
		||||
    };
 | 
			
		||||
    let b = match op.b {
 | 
			
		||||
        OpParam::Old => item,
 | 
			
		||||
        OpParam::Num(n) => n,
 | 
			
		||||
    };
 | 
			
		||||
    match op.kind {
 | 
			
		||||
        OpKind::Add => a + b,
 | 
			
		||||
        OpKind::Mul => a * b,
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn monkey_business(monkeys: &Vec<RefCell<Monkey>>) -> usize {
 | 
			
		||||
    monkeys
 | 
			
		||||
        .iter()
 | 
			
		||||
        .map(|m| m.borrow().activity)
 | 
			
		||||
        .sorted()
 | 
			
		||||
        .rev()
 | 
			
		||||
        .take(2)
 | 
			
		||||
        .product()
 | 
			
		||||
}
 | 
			
		||||
@ -1,5 +1,6 @@
 | 
			
		||||
pub mod day1;
 | 
			
		||||
pub mod day10;
 | 
			
		||||
pub mod day11;
 | 
			
		||||
pub mod day2;
 | 
			
		||||
pub mod day3;
 | 
			
		||||
pub mod day4;
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user