From 2b7b34eb1fde3761f5f894e470d791e13ad02c2c Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Mon, 12 Dec 2022 02:31:36 +0100 Subject: [PATCH] add day 11, part 2 --- day11/common.rs | 5 ++++- day11/part2.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 day11/part2.rs diff --git a/day11/common.rs b/day11/common.rs index 35f2cdc..a2509ba 100644 --- a/day11/common.rs +++ b/day11/common.rs @@ -34,6 +34,7 @@ pub struct Monkey { pub struct Game { monkeys: Vec, + modulus_lcm: u64, divisor: u64, } @@ -158,8 +159,10 @@ impl Game { while let Some(monkey) = Monkey::from_stdin()? { monkeys.push(monkey); } + let modulus_lcm = monkeys.iter().map(|m| m.modulus).product(); Ok(Self { monkeys: monkeys, + modulus_lcm: modulus_lcm, divisor: divisor, }) } @@ -178,7 +181,7 @@ impl Iterator for Game { match self.monkeys[i].throw_item(self.divisor) { Ok(None) => break, Ok(Some((item, target))) => { - self.monkeys[target].add_item(item); + self.monkeys[target].add_item(item % self.modulus_lcm); } Err(e) => return Some(Err(e)), } diff --git a/day11/part2.rs b/day11/part2.rs new file mode 100644 index 0000000..e49b5c5 --- /dev/null +++ b/day11/part2.rs @@ -0,0 +1,36 @@ +// Copyright 2022 Christian Ulrich +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// usage: ./part1 < input + +pub mod common; + +use common::{Game, Monkey}; + +fn main() -> Result<(), &'static str> { + let game = Game::from_stdin(1)?; + let last_round = game.take(10000).last().unwrap(); + let mut item_counts: Vec<_> = last_round? + .monkeys() + .iter() + .map(Monkey::item_count) + .collect(); + item_counts.sort(); + println!( + "{}", + item_counts[item_counts.len() - 1] * item_counts[item_counts.len() - 2] + ); + Ok(()) +}