From 876ebfcfe3b4945c3bedb1582cf3cfbc508f4e76 Mon Sep 17 00:00:00 2001 From: jazzpi Date: Thu, 15 Dec 2022 21:34:16 +0100 Subject: [PATCH] Day 11, puzzle 2 --- Cargo.lock | 134 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bin/d11p1.rs | 4 +- src/bin/d11p2.rs | 22 ++++++++ src/day11.rs | 30 +++++++---- 5 files changed, 178 insertions(+), 13 deletions(-) create mode 100644 src/bin/d11p2.rs diff --git a/Cargo.lock b/Cargo.lock index b1aacd4..369296b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,9 +18,16 @@ dependencies = [ "env_logger", "itertools", "log", + "num", "regex", ] +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "bitflags" version = "1.3.2" @@ -79,6 +86,12 @@ dependencies = [ "libc", ] +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "hermit-abi" version = "0.2.6" @@ -152,6 +165,121 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "num" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e63899ad0da84ce718c14936262a41cee2c79c981fc0a0e7c7beb47d5a07e8c1" +dependencies = [ + "num-integer", + "num-traits", + "rand", + "rustc-serialize", +] + +[[package]] +name = "num-complex" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b288631d7878aaf59442cffd36910ea604ecd7745c36054328595114001c9656" +dependencies = [ + "num-traits", + "rustc-serialize", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee314c74bd753fc86b4780aa9475da469155f3848473a261d2d18e35245a784e" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", + "rustc-serialize", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "regex" version = "1.7.0" @@ -169,6 +297,12 @@ version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +[[package]] +name = "rustc-serialize" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" + [[package]] name = "rustix" version = "0.36.5" diff --git a/Cargo.toml b/Cargo.toml index 77908ff..418e7eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ regex = "1" itertools = "^0.10" log = "^0.4" env_logger = "^0.10" +num = "^0.1" [[bin]] name = "d1p1" diff --git a/src/bin/d11p1.rs b/src/bin/d11p1.rs index 7b01976..b488e9e 100644 --- a/src/bin/d11p1.rs +++ b/src/bin/d11p1.rs @@ -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!( diff --git a/src/bin/d11p2.rs b/src/bin/d11p2.rs new file mode 100644 index 0000000..9f87d16 --- /dev/null +++ b/src/bin/d11p2.rs @@ -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) + ); +} diff --git a/src/day11.rs b/src/day11.rs index 1653d11..38a86f1 100644 --- a/src/day11.rs +++ b/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>) { +pub fn do_round(monkeys: &Vec>, 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>) { 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!(