1
0
This commit is contained in:
2022-12-27 01:46:31 +00:00
parent 8fbaaa4bbc
commit e871837dc6
11 changed files with 110 additions and 86 deletions

View File

@ -17,17 +17,13 @@ fn op_func(op: char, operand: &str) -> Box<dyn Fn(i64) -> i64> {
let op = match op {
'*' => std::ops::Mul::mul,
'+' => std::ops::Add::add,
_ => panic!("unsupported op {}", op),
_ => panic!("unsupported op {}", op),
};
if operand == "old" {
Box::new(move |x| {
op(x, x)
})
Box::new(move |x| op(x, x))
} else {
let operand: i64 = operand.parse().unwrap();
Box::new(move |x| {
op(x, operand)
})
Box::new(move |x| op(x, operand))
}
}
@ -35,7 +31,7 @@ fn main() {
const INPUT: &str = include_str!("../input.txt");
let mut monkeys: Vec<Monkey> = Vec::new();
let mut cur_monkey = -1i64;
let mut cur_test_divisor = -1i64;
let mut cur_test_true_ret = -1i64;
@ -47,38 +43,34 @@ fn main() {
if line == "" {
continue;
}
else if line.starts_with("Monkey") {
} else if line.starts_with("Monkey") {
cur_monkey += 1;
monkeys.push(Monkey {
items: Vec::new(),
operation: Box::new(empty_op),
test: Box::new(empty_test),
inspections: 0
inspections: 0,
});
continue;
}
else if line.starts_with("Starting items: ") {
} else if line.starts_with("Starting items: ") {
let items = line[16..].split(", ");
for item in items {
monkeys[cur_monkey as usize].items.push(item.parse().unwrap());
monkeys[cur_monkey as usize]
.items
.push(item.parse().unwrap());
}
}
else if line.starts_with("Operation: ") {
} else if line.starts_with("Operation: ") {
let mut items = line[21..].split(' ');
let op = items.next().unwrap().chars().next().unwrap();
let operand = items.next().unwrap();
monkeys[cur_monkey as usize].operation = op_func(op, operand);
}
else if line.starts_with("Test: ") {
} else if line.starts_with("Test: ") {
cur_test_divisor = line[19..].parse().unwrap();
lcm = num::integer::lcm(lcm, cur_test_divisor);
}
else if line.starts_with("If true: ") {
} else if line.starts_with("If true: ") {
cur_test_true_ret = line[25..].parse().unwrap();
}
else if line.starts_with("If false: ") {
} else if line.starts_with("If false: ") {
let target: i64 = line[26..].parse().unwrap();
monkeys[cur_monkey as usize].test = Box::new(move |x| {
@ -107,7 +99,7 @@ fn main() {
}
}
let mut active_monkeys = monkeys.iter().map(|m| { m.inspections }).collect::<Vec<u64>>();
let mut active_monkeys = monkeys.iter().map(|m| m.inspections).collect::<Vec<u64>>();
active_monkeys.sort();
active_monkeys.reverse();