Day 11, puzzle 2
This commit is contained in:
@ -10,8 +10,8 @@ pub fn main() {
|
||||
|
||||
let monkeys = day11::parse_monkeys(&util::parse_input());
|
||||
|
||||
for _ in 0..20 {
|
||||
day11::do_round(&monkeys);
|
||||
for _ in 0..N_ROUNDS {
|
||||
day11::do_round(&monkeys, true);
|
||||
}
|
||||
|
||||
println!(
|
||||
|
||||
22
src/bin/d11p2.rs
Normal file
22
src/bin/d11p2.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use aoc22::{
|
||||
day11::{self, monkey_business},
|
||||
util,
|
||||
};
|
||||
|
||||
const N_ROUNDS: usize = 10000;
|
||||
|
||||
pub fn main() {
|
||||
env_logger::init();
|
||||
|
||||
let monkeys = day11::parse_monkeys(&util::parse_input());
|
||||
|
||||
for _ in 0..N_ROUNDS {
|
||||
day11::do_round(&monkeys, false);
|
||||
}
|
||||
|
||||
println!(
|
||||
"Monkey business after {} rounds is {}",
|
||||
N_ROUNDS,
|
||||
monkey_business(&monkeys)
|
||||
);
|
||||
}
|
||||
30
src/day11.rs
30
src/day11.rs
@ -1,6 +1,7 @@
|
||||
use std::{borrow::BorrowMut, cell::RefCell, collections::VecDeque};
|
||||
use std::{cell::RefCell, collections::VecDeque};
|
||||
|
||||
use itertools::Itertools;
|
||||
use num::Integer;
|
||||
use regex::Regex;
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -137,7 +138,12 @@ fn parse_op_kind(s: &str) -> OpKind {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn do_round(monkeys: &Vec<RefCell<Monkey>>) {
|
||||
pub fn do_round(monkeys: &Vec<RefCell<Monkey>>, do_worry: bool) {
|
||||
let overall_mod = monkeys
|
||||
.iter()
|
||||
.map(|m| m.borrow().test.div)
|
||||
.reduce(|acc, div| acc.lcm(&div))
|
||||
.unwrap();
|
||||
for n in 0..monkeys.len() {
|
||||
let mut monkey = monkeys.get(n).unwrap().borrow_mut();
|
||||
log::debug!("Monkey {}", n);
|
||||
@ -145,21 +151,23 @@ pub fn do_round(monkeys: &Vec<RefCell<Monkey>>) {
|
||||
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);
|
||||
item = run_op(&monkey.op, item) % overall_mod;
|
||||
log::debug!(" Worry level increases to {}.", item);
|
||||
item /= 3;
|
||||
log::debug!(
|
||||
" Monkey gets bored with item. Worry level is divided by 3 to {}.",
|
||||
item
|
||||
);
|
||||
if do_worry {
|
||||
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 divisible by {}", monkey.test.div);
|
||||
monkey.test.target_true
|
||||
} else {
|
||||
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!(
|
||||
|
||||
Reference in New Issue
Block a user