diff --git a/inputs/2022/2022_11.solution b/inputs/2022/2022_11.solution index d3dd0a9..22e3063 100644 --- a/inputs/2022/2022_11.solution +++ b/inputs/2022/2022_11.solution @@ -1,2 +1,2 @@ Part 1: 66124 -Part 2: ??? +Part 2: 19309892877 diff --git a/rs/src/y2022/d11.rs b/rs/src/y2022/d11.rs index 47080a0..c75ccfd 100644 --- a/rs/src/y2022/d11.rs +++ b/rs/src/y2022/d11.rs @@ -1,14 +1,14 @@ use std::mem; use std::str::Lines; -#[derive(Clone, Copy)] +#[derive(Debug, Clone, Copy)] enum Operation { Add, Mul, } impl Operation { - fn eval(self, lhs: u32, rhs: u32) -> u32 { + fn eval(self, lhs: u64, rhs: u64) -> u64 { match self { Self::Add => lhs + rhs, Self::Mul => lhs * rhs, @@ -16,14 +16,15 @@ impl Operation { } } +#[derive(Clone)] struct Monkey { - holds: Vec, + holds: Vec, // Left hand side is always "old" op: Operation, - rhs: Option, + rhs: Option, - div_by: u32, + div_by: u64, if_true: usize, if_false: usize, @@ -41,7 +42,7 @@ impl Monkey { let holds = prefixed(&mut lines, " Starting items: ") .split(", ") - .map(|i| i.parse::().unwrap()) + .map(|i| i.parse::().unwrap()) .collect::>(); let (op, rhs) = prefixed(&mut lines, " Operation: new = old ") @@ -93,12 +94,14 @@ impl Monkey { } } -fn round(monkeys: &mut Vec) { +fn round(monkeys: &mut Vec, reduce_worry: bool, modulo: u64) { 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 item = monkey.op.eval(item, monkey.rhs.unwrap_or(item)); + let item = if reduce_worry { item / 3 } else { item }; + let item = item % modulo; let target = if item % monkey.div_by == 0 { monkey.if_true } else { @@ -109,18 +112,30 @@ fn round(monkeys: &mut Vec) { } } +fn monkey_business(monkeys: &[Monkey]) -> usize { + let mut inspections = monkeys.iter().map(|m| m.inspections).collect::>(); + inspections.sort_unstable(); + inspections.into_iter().rev().take(2).product::() +} + 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 common_multiple = monkeys.iter().map(|m| m.div_by).product::(); + eprintln!("Common multiple: {common_multiple}"); - 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}"); + let mut part1 = monkeys.clone(); + for _ in 0..20 { + round(&mut part1, true, common_multiple); + } + println!("Part 1: {}", monkey_business(&part1)); + + let mut part2 = monkeys.clone(); + for _ in 0..10000 { + round(&mut part2, false, common_multiple); + } + println!("Part 2: {}", monkey_business(&part2)); } diff --git a/sample_inputs/2022/2022_11.solution b/sample_inputs/2022/2022_11.solution index d940eba..bdeb089 100644 --- a/sample_inputs/2022/2022_11.solution +++ b/sample_inputs/2022/2022_11.solution @@ -1,2 +1,2 @@ Part 1: 10605 -Part 2: ??? +Part 2: 2713310158