From 7aa2bae3999b1339cb39423a55468deaa16d308b Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 11 Dec 2022 11:09:16 +0100 Subject: [PATCH] [rs] Solve 2022_11 part 1 --- inputs/2022/2022_11.input | 55 ++++++++++++ inputs/2022/2022_11.solution | 2 + rs/src/main.rs | 1 + rs/src/y2022.rs | 1 + rs/src/y2022/d11.rs | 126 ++++++++++++++++++++++++++++ sample_inputs/2022/2022_11.input | 27 ++++++ sample_inputs/2022/2022_11.solution | 2 + 7 files changed, 214 insertions(+) create mode 100644 inputs/2022/2022_11.input create mode 100644 inputs/2022/2022_11.solution create mode 100644 rs/src/y2022/d11.rs create mode 100644 sample_inputs/2022/2022_11.input create mode 100644 sample_inputs/2022/2022_11.solution diff --git a/inputs/2022/2022_11.input b/inputs/2022/2022_11.input new file mode 100644 index 0000000..619c5cb --- /dev/null +++ b/inputs/2022/2022_11.input @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 75, 75, 98, 97, 79, 97, 64 + Operation: new = old * 13 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 7 + +Monkey 1: + Starting items: 50, 99, 80, 84, 65, 95 + Operation: new = old + 2 + Test: divisible by 3 + If true: throw to monkey 4 + If false: throw to monkey 5 + +Monkey 2: + Starting items: 96, 74, 68, 96, 56, 71, 75, 53 + Operation: new = old + 1 + Test: divisible by 11 + If true: throw to monkey 7 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 83, 96, 86, 58, 92 + Operation: new = old + 8 + Test: divisible by 17 + If true: throw to monkey 6 + If false: throw to monkey 1 + +Monkey 4: + Starting items: 99 + Operation: new = old * old + Test: divisible by 5 + If true: throw to monkey 0 + If false: throw to monkey 5 + +Monkey 5: + Starting items: 60, 54, 83 + Operation: new = old + 4 + Test: divisible by 2 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 6: + Starting items: 77, 67 + Operation: new = old * 17 + Test: divisible by 13 + If true: throw to monkey 4 + If false: throw to monkey 1 + +Monkey 7: + Starting items: 95, 65, 58, 76 + Operation: new = old + 5 + Test: divisible by 7 + If true: throw to monkey 3 + If false: throw to monkey 6 diff --git a/inputs/2022/2022_11.solution b/inputs/2022/2022_11.solution new file mode 100644 index 0000000..d3dd0a9 --- /dev/null +++ b/inputs/2022/2022_11.solution @@ -0,0 +1,2 @@ +Part 1: 66124 +Part 2: ??? diff --git a/rs/src/main.rs b/rs/src/main.rs index 7513956..308e879 100644 --- a/rs/src/main.rs +++ b/rs/src/main.rs @@ -61,6 +61,7 @@ days! { Y2022D08: "2022_08" => y2022::d08::solve, Y2022D09: "2022_09" => y2022::d09::solve, Y2022D10: "2022_10" => y2022::d10::solve, + Y2022D11: "2022_11" => y2022::d11::solve, } #[derive(Parser)] diff --git a/rs/src/y2022.rs b/rs/src/y2022.rs index 8859e04..a68a352 100644 --- a/rs/src/y2022.rs +++ b/rs/src/y2022.rs @@ -8,3 +8,4 @@ pub mod d07; pub mod d08; pub mod d09; pub mod d10; +pub mod d11; diff --git a/rs/src/y2022/d11.rs b/rs/src/y2022/d11.rs new file mode 100644 index 0000000..47080a0 --- /dev/null +++ b/rs/src/y2022/d11.rs @@ -0,0 +1,126 @@ +use std::mem; +use std::str::Lines; + +#[derive(Clone, Copy)] +enum Operation { + Add, + Mul, +} + +impl Operation { + fn eval(self, lhs: u32, rhs: u32) -> u32 { + match self { + Self::Add => lhs + rhs, + Self::Mul => lhs * rhs, + } + } +} + +struct Monkey { + holds: Vec, + + // Left hand side is always "old" + op: Operation, + rhs: Option, + + div_by: u32, + if_true: usize, + if_false: usize, + + inspections: usize, +} + +impl Monkey { + fn parse(s: &str) -> Self { + let mut lines = s.lines(); + lines.next(); + + fn prefixed<'a>(lines: &'a mut Lines, prefix: &str) -> &'a str { + lines.next().unwrap().strip_prefix(prefix).unwrap() + } + + let holds = prefixed(&mut lines, " Starting items: ") + .split(", ") + .map(|i| i.parse::().unwrap()) + .collect::>(); + + let (op, rhs) = prefixed(&mut lines, " Operation: new = old ") + .split_once(' ') + .unwrap(); + let op = match op { + "+" => Operation::Add, + "*" => Operation::Mul, + _ => panic!(), + }; + let rhs = match rhs { + "old" => None, + _ => Some(rhs.parse().unwrap()), + }; + + let div_by = prefixed(&mut lines, " Test: divisible by ") + .parse() + .unwrap(); + + let if_true = prefixed(&mut lines, " If true: throw to monkey ") + .parse() + .unwrap(); + + let if_false = prefixed(&mut lines, " If false: throw to monkey ") + .parse() + .unwrap(); + + Self { + holds, + op, + rhs, + div_by, + if_true, + if_false, + inspections: 0, + } + } + + fn take(&mut self) -> Self { + Self { + holds: mem::take(&mut self.holds), + op: self.op, + rhs: self.rhs, + div_by: self.div_by, + if_true: self.if_true, + if_false: self.if_false, + inspections: self.inspections, + } + } +} + +fn round(monkeys: &mut Vec) { + for i in 0..monkeys.len() { + monkeys[i].inspections += monkeys[i].holds.len(); + let monkey = monkeys[i].take(); + for item in monkey.holds { + let item = monkey.op.eval(item, monkey.rhs.unwrap_or(item)) / 3; + let target = if item % monkey.div_by == 0 { + monkey.if_true + } else { + monkey.if_false + }; + monkeys[target].holds.push(item); + } + } +} + +pub fn solve(input: String) { + let mut monkeys = vec![]; + for monkey in input.trim().split("\n\n") { + monkeys.push(Monkey::parse(monkey)); + } + + for _ in 0..20 { + round(&mut monkeys); + } + + let mut inspections = monkeys.iter().map(|m| m.inspections).collect::>(); + inspections.sort_unstable(); + let part1 = inspections.into_iter().rev().take(2).product::(); + println!("Part 1: {part1}"); +} diff --git a/sample_inputs/2022/2022_11.input b/sample_inputs/2022/2022_11.input new file mode 100644 index 0000000..30e09e5 --- /dev/null +++ b/sample_inputs/2022/2022_11.input @@ -0,0 +1,27 @@ +Monkey 0: + Starting items: 79, 98 + Operation: new = old * 19 + Test: divisible by 23 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 54, 65, 75, 74 + Operation: new = old + 6 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 79, 60, 97 + Operation: new = old * old + Test: divisible by 13 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 74 + Operation: new = old + 3 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 1 diff --git a/sample_inputs/2022/2022_11.solution b/sample_inputs/2022/2022_11.solution new file mode 100644 index 0000000..d940eba --- /dev/null +++ b/sample_inputs/2022/2022_11.solution @@ -0,0 +1,2 @@ +Part 1: 10605 +Part 2: ???