[rs] Solve 2022_11 part 2
This commit is contained in:
parent
7aa2bae399
commit
c467daaa62
3 changed files with 32 additions and 17 deletions
|
|
@ -1,2 +1,2 @@
|
||||||
Part 1: 66124
|
Part 1: 66124
|
||||||
Part 2: ???
|
Part 2: 19309892877
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::str::Lines;
|
use std::str::Lines;
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
enum Operation {
|
enum Operation {
|
||||||
Add,
|
Add,
|
||||||
Mul,
|
Mul,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Operation {
|
impl Operation {
|
||||||
fn eval(self, lhs: u32, rhs: u32) -> u32 {
|
fn eval(self, lhs: u64, rhs: u64) -> u64 {
|
||||||
match self {
|
match self {
|
||||||
Self::Add => lhs + rhs,
|
Self::Add => lhs + rhs,
|
||||||
Self::Mul => lhs * rhs,
|
Self::Mul => lhs * rhs,
|
||||||
|
|
@ -16,14 +16,15 @@ impl Operation {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
struct Monkey {
|
struct Monkey {
|
||||||
holds: Vec<u32>,
|
holds: Vec<u64>,
|
||||||
|
|
||||||
// Left hand side is always "old"
|
// Left hand side is always "old"
|
||||||
op: Operation,
|
op: Operation,
|
||||||
rhs: Option<u32>,
|
rhs: Option<u64>,
|
||||||
|
|
||||||
div_by: u32,
|
div_by: u64,
|
||||||
if_true: usize,
|
if_true: usize,
|
||||||
if_false: usize,
|
if_false: usize,
|
||||||
|
|
||||||
|
|
@ -41,7 +42,7 @@ impl Monkey {
|
||||||
|
|
||||||
let holds = prefixed(&mut lines, " Starting items: ")
|
let holds = prefixed(&mut lines, " Starting items: ")
|
||||||
.split(", ")
|
.split(", ")
|
||||||
.map(|i| i.parse::<u32>().unwrap())
|
.map(|i| i.parse::<u64>().unwrap())
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let (op, rhs) = prefixed(&mut lines, " Operation: new = old ")
|
let (op, rhs) = prefixed(&mut lines, " Operation: new = old ")
|
||||||
|
|
@ -93,12 +94,14 @@ impl Monkey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn round(monkeys: &mut Vec<Monkey>) {
|
fn round(monkeys: &mut Vec<Monkey>, reduce_worry: bool, modulo: u64) {
|
||||||
for i in 0..monkeys.len() {
|
for i in 0..monkeys.len() {
|
||||||
monkeys[i].inspections += monkeys[i].holds.len();
|
monkeys[i].inspections += monkeys[i].holds.len();
|
||||||
let monkey = monkeys[i].take();
|
let monkey = monkeys[i].take();
|
||||||
for item in monkey.holds {
|
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 {
|
let target = if item % monkey.div_by == 0 {
|
||||||
monkey.if_true
|
monkey.if_true
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -109,18 +112,30 @@ fn round(monkeys: &mut Vec<Monkey>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn monkey_business(monkeys: &[Monkey]) -> usize {
|
||||||
|
let mut inspections = monkeys.iter().map(|m| m.inspections).collect::<Vec<_>>();
|
||||||
|
inspections.sort_unstable();
|
||||||
|
inspections.into_iter().rev().take(2).product::<usize>()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn solve(input: String) {
|
pub fn solve(input: String) {
|
||||||
let mut monkeys = vec![];
|
let mut monkeys = vec![];
|
||||||
for monkey in input.trim().split("\n\n") {
|
for monkey in input.trim().split("\n\n") {
|
||||||
monkeys.push(Monkey::parse(monkey));
|
monkeys.push(Monkey::parse(monkey));
|
||||||
}
|
}
|
||||||
|
|
||||||
for _ in 0..20 {
|
let common_multiple = monkeys.iter().map(|m| m.div_by).product::<u64>();
|
||||||
round(&mut monkeys);
|
eprintln!("Common multiple: {common_multiple}");
|
||||||
}
|
|
||||||
|
|
||||||
let mut inspections = monkeys.iter().map(|m| m.inspections).collect::<Vec<_>>();
|
let mut part1 = monkeys.clone();
|
||||||
inspections.sort_unstable();
|
for _ in 0..20 {
|
||||||
let part1 = inspections.into_iter().rev().take(2).product::<usize>();
|
round(&mut part1, true, common_multiple);
|
||||||
println!("Part 1: {part1}");
|
}
|
||||||
|
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));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
Part 1: 10605
|
Part 1: 10605
|
||||||
Part 2: ???
|
Part 2: 2713310158
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue