Watch
1
0
Fork
You've already forked advent-of-code
0

Default + Option is less weird

This commit is contained in:
Jeremy Kaplan 2022-12-11 18:11:30 -08:00
commit 339498cbdd

View file

@ -16,12 +16,12 @@ fn monkey_business(mut monkeys: Vec<Monkey>, rounds: usize, confidence: i64) ->
for _ in 1..=rounds { for _ in 1..=rounds {
for m in 0..monkeys.len() { for m in 0..monkeys.len() {
// Don't forget to put the monkey back in the vec when we're done!
let mut monkey = std::mem::take(&mut monkeys[m]); let mut monkey = std::mem::take(&mut monkeys[m]);
let mut items = std::mem::take(&mut monkey.items);
for mut item in items.drain(0..) { for mut item in monkey.items.drain(0..) {
monkey.inspections += 1; monkey.inspections += 1;
item.worry = monkey.operation.apply(item.worry); item.worry = monkey.operation.unwrap().apply(item.worry);
item.worry /= confidence; item.worry /= confidence;
item.worry %= yolo_factor; item.worry %= yolo_factor;
@ -44,7 +44,7 @@ fn monkey_business(mut monkeys: Vec<Monkey>, rounds: usize, confidence: i64) ->
struct Monkey { struct Monkey {
id: usize, id: usize,
items: VecDeque<Item>, items: VecDeque<Item>,
operation: Operation, operation: Option<Operation>,
test: Test, test: Test,
inspections: usize, inspections: usize,
@ -57,23 +57,21 @@ struct Item {
worry: Worry, worry: Worry,
} }
#[derive(Debug, Copy, Clone, Default)] #[derive(Debug, Copy, Clone)]
struct Operation { struct Operation {
operator: Operator, operator: Operator,
operand: Operand, operand: Operand,
} }
#[derive(Debug, Copy, Clone, Default)] #[derive(Debug, Copy, Clone)]
enum Operator { enum Operator {
#[default]
Plus, Plus,
Times, Times,
} }
#[derive(Debug, Copy, Clone, Default)] #[derive(Debug, Copy, Clone)]
enum Operand { enum Operand {
Const(i64), Const(i64),
#[default]
Old, Old,
} }
@ -105,8 +103,8 @@ impl Operation {
match (self.operand, self.operator) { match (self.operand, self.operator) {
(Const(n), Plus) => old + n, (Const(n), Plus) => old + n,
(Old, Plus) => old + old,
(Const(n), Times) => old * n, (Const(n), Times) => old * n,
(Old, Plus) => old + old,
(Old, Times) => old * old, (Old, Times) => old * old,
} }
} }
@ -184,7 +182,7 @@ impl Monkey {
.collect() .collect()
}; };
let operation = Operation::parse(lines[2]); let operation = Some(Operation::parse(lines[2]));
let test = Test::parse(&lines[3..]); let test = Test::parse(&lines[3..]);
Self { Self {