From c7087d092106e6fb1082e006244540e1483fb031 Mon Sep 17 00:00:00 2001 From: Jack Bond-Preston Date: Mon, 26 Dec 2022 23:52:21 +0000 Subject: [PATCH] day 11 --- Cargo.toml | 1 + day11/Cargo.toml | 9 ++++ day11/input.txt | 55 ++++++++++++++++++++ day11/sample_input.txt | 27 ++++++++++ day11/src/main.rs | 115 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 207 insertions(+) create mode 100644 day11/Cargo.toml create mode 100644 day11/input.txt create mode 100644 day11/sample_input.txt create mode 100644 day11/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index b5a3aeb..6727473 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ members = [ "day8", "day9", "day10", + "day11", ] \ No newline at end of file diff --git a/day11/Cargo.toml b/day11/Cargo.toml new file mode 100644 index 0000000..7808ac0 --- /dev/null +++ b/day11/Cargo.toml @@ -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" } diff --git a/day11/input.txt b/day11/input.txt new file mode 100644 index 0000000..3591715 --- /dev/null +++ b/day11/input.txt @@ -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 \ No newline at end of file diff --git a/day11/sample_input.txt b/day11/sample_input.txt new file mode 100644 index 0000000..c04eddb --- /dev/null +++ b/day11/sample_input.txt @@ -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 \ No newline at end of file diff --git a/day11/src/main.rs b/day11/src/main.rs new file mode 100644 index 0000000..6980ac3 --- /dev/null +++ b/day11/src/main.rs @@ -0,0 +1,115 @@ +struct Monkey { + items: Vec, + operation: Box i64>, + test: Box i64>, + inspections: u64, +} + +fn empty_op(_: i64) -> i64 { + -1 +} + +fn empty_test(_: i64) -> i64 { + -1 +} + +fn op_func(op: char, operand: &str) -> Box 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 = 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::>(); + active_monkeys.sort(); + active_monkeys.reverse(); + + println!("{}", active_monkeys[0] * active_monkeys[1]); +}