1
0
This commit is contained in:
Jack Bond-Preston 2022-12-26 23:52:21 +00:00
parent 00a67d7d3d
commit c7087d0921
5 changed files with 207 additions and 0 deletions

View File

@ -11,4 +11,5 @@ members = [
"day8",
"day9",
"day10",
"day11",
]

9
day11/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "day11"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
num = { version = "0.4.0" }

55
day11/input.txt Normal file
View File

@ -0,0 +1,55 @@
Monkey 0:
Starting items: 89, 74
Operation: new = old * 5
Test: divisible by 17
If true: throw to monkey 4
If false: throw to monkey 7
Monkey 1:
Starting items: 75, 69, 87, 57, 84, 90, 66, 50
Operation: new = old + 3
Test: divisible by 7
If true: throw to monkey 3
If false: throw to monkey 2
Monkey 2:
Starting items: 55
Operation: new = old + 7
Test: divisible by 13
If true: throw to monkey 0
If false: throw to monkey 7
Monkey 3:
Starting items: 69, 82, 69, 56, 68
Operation: new = old + 5
Test: divisible by 2
If true: throw to monkey 0
If false: throw to monkey 2
Monkey 4:
Starting items: 72, 97, 50
Operation: new = old + 2
Test: divisible by 19
If true: throw to monkey 6
If false: throw to monkey 5
Monkey 5:
Starting items: 90, 84, 56, 92, 91, 91
Operation: new = old * 19
Test: divisible by 3
If true: throw to monkey 6
If false: throw to monkey 1
Monkey 6:
Starting items: 63, 93, 55, 53
Operation: new = old * old
Test: divisible by 5
If true: throw to monkey 3
If false: throw to monkey 1
Monkey 7:
Starting items: 50, 61, 52, 58, 86, 68, 97
Operation: new = old + 4
Test: divisible by 11
If true: throw to monkey 5
If false: throw to monkey 4

27
day11/sample_input.txt Normal file
View File

@ -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

115
day11/src/main.rs Normal file
View File

@ -0,0 +1,115 @@
struct Monkey {
items: Vec<i64>,
operation: Box<dyn Fn(i64) -> i64>,
test: Box<dyn Fn(i64) -> i64>,
inspections: u64,
}
fn empty_op(_: i64) -> i64 {
-1
}
fn empty_test(_: i64) -> i64 {
-1
}
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),
};
if operand == "old" {
Box::new(move |x| {
op(x, x)
})
} else {
let operand: i64 = operand.parse().unwrap();
Box::new(move |x| {
op(x, operand)
})
}
}
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;
let mut lcm: i64 = 1;
for line in INPUT.lines() {
let line = line.trim_start();
if line == "" {
continue;
}
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
});
continue;
}
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());
}
}
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: ") {
cur_test_divisor = line[19..].parse().unwrap();
lcm = num::integer::lcm(lcm, cur_test_divisor);
}
else if line.starts_with("If true: ") {
cur_test_true_ret = line[25..].parse().unwrap();
}
else if line.starts_with("If false: ") {
let target: i64 = line[26..].parse().unwrap();
monkeys[cur_monkey as usize].test = Box::new(move |x| {
if x % cur_test_divisor == 0 {
cur_test_true_ret
} else {
target
}
});
}
}
println!("lcm: {}", lcm);
for _ in 1..=10000 {
for i in 0..monkeys.len() {
for j in 0..monkeys[i].items.len() {
let mut new_worry = (monkeys[i].operation)(monkeys[i].items[j]);
new_worry = new_worry % lcm;
let dest: usize = (monkeys[i].test)(new_worry).try_into().unwrap();
monkeys[dest].items.push(new_worry);
monkeys[i].inspections += 1;
}
monkeys[i].items.clear();
}
}
let mut active_monkeys = monkeys.iter().map(|m| { m.inspections }).collect::<Vec<u64>>();
active_monkeys.sort();
active_monkeys.reverse();
println!("{}", active_monkeys[0] * active_monkeys[1]);
}