From 3ad8924d6c744c4193f251dc962a3bd7c421b22e Mon Sep 17 00:00:00 2001 From: Noah Over Date: Tue, 13 Dec 2022 09:39:51 -0500 Subject: [PATCH] Day 11 2022 --- noahover/rust_2022/input/2022/day11.txt | 55 +++++ .../rust_2022/input/2022/day11.txt.example | 27 +++ noahover/rust_2022/src/day11.rs | 224 ++++++++++++++++++ noahover/rust_2022/src/lib.rs | 1 + 4 files changed, 307 insertions(+) create mode 100644 noahover/rust_2022/input/2022/day11.txt create mode 100644 noahover/rust_2022/input/2022/day11.txt.example create mode 100644 noahover/rust_2022/src/day11.rs diff --git a/noahover/rust_2022/input/2022/day11.txt b/noahover/rust_2022/input/2022/day11.txt new file mode 100644 index 0000000..41ef4ec --- /dev/null +++ b/noahover/rust_2022/input/2022/day11.txt @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 98, 70, 75, 80, 84, 89, 55, 98 + Operation: new = old * 2 + Test: divisible by 11 + If true: throw to monkey 1 + If false: throw to monkey 4 + +Monkey 1: + Starting items: 59 + Operation: new = old * old + Test: divisible by 19 + If true: throw to monkey 7 + If false: throw to monkey 3 + +Monkey 2: + Starting items: 77, 95, 54, 65, 89 + Operation: new = old + 6 + Test: divisible by 7 + If true: throw to monkey 0 + If false: throw to monkey 5 + +Monkey 3: + Starting items: 71, 64, 75 + Operation: new = old + 2 + Test: divisible by 17 + If true: throw to monkey 6 + If false: throw to monkey 2 + +Monkey 4: + Starting items: 74, 55, 87, 98 + Operation: new = old * 11 + Test: divisible by 3 + If true: throw to monkey 1 + If false: throw to monkey 7 + +Monkey 5: + Starting items: 90, 98, 85, 52, 91, 60 + Operation: new = old + 7 + Test: divisible by 5 + If true: throw to monkey 0 + If false: throw to monkey 4 + +Monkey 6: + Starting items: 99, 51 + Operation: new = old + 1 + Test: divisible by 13 + If true: throw to monkey 5 + If false: throw to monkey 2 + +Monkey 7: + Starting items: 98, 94, 59, 76, 51, 65, 75 + Operation: new = old + 5 + Test: divisible by 2 + If true: throw to monkey 3 + If false: throw to monkey 6 diff --git a/noahover/rust_2022/input/2022/day11.txt.example b/noahover/rust_2022/input/2022/day11.txt.example new file mode 100644 index 0000000..30e09e5 --- /dev/null +++ b/noahover/rust_2022/input/2022/day11.txt.example @@ -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 diff --git a/noahover/rust_2022/src/day11.rs b/noahover/rust_2022/src/day11.rs new file mode 100644 index 0000000..cefc62e --- /dev/null +++ b/noahover/rust_2022/src/day11.rs @@ -0,0 +1,224 @@ +#[aoc_generator(day11)] +pub fn input_generator(input: &str) -> (Vec>, Vec, Vec, Vec, Vec, Vec) { + let items = input + .split("\n\n") + .map(|monkey| { + let lines: Vec<&str> = monkey.lines().collect(); + + lines[1] + .trim() + .strip_prefix("Starting items: ") + .unwrap() + .split(", ") + .map(|item| item.parse().unwrap()) + .collect() + }) + .collect(); + + let operation_types = input + .split("\n\n") + .map(|monkey| { + let lines: Vec<&str> = monkey.lines().collect(); + + let mut operation_info = lines[2] + .trim() + .strip_prefix("Operation: new = old ") + .unwrap() + .split_whitespace(); + + let operation_info = ( + operation_info.next().unwrap(), + operation_info.next().unwrap(), + ); + + let operation_info = match operation_info { + ("*", "old") => ("^", 2), + (t, v) => (t, v.parse().unwrap()), + }; + + operation_info.0.to_string() + }) + .collect(); + + let operation_values = input + .split("\n\n") + .map(|monkey| { + let lines: Vec<&str> = monkey.lines().collect(); + + let mut operation_info = lines[2] + .trim() + .strip_prefix("Operation: new = old ") + .unwrap() + .split_whitespace(); + + let operation_info = ( + operation_info.next().unwrap(), + operation_info.next().unwrap(), + ); + + let operation_info = match operation_info { + ("*", "old") => ("^", 2), + (t, v) => (t, v.parse().unwrap()), + }; + + operation_info.1 + }) + .collect(); + + let tests = input + .split("\n\n") + .map(|monkey| { + let lines: Vec<&str> = monkey.lines().collect(); + + lines[3] + .trim() + .strip_prefix("Test: divisible by ") + .unwrap() + .parse() + .unwrap() + }) + .collect(); + + let true_monkeys = input + .split("\n\n") + .map(|monkey| { + let lines: Vec<&str> = monkey.lines().collect(); + + lines[4] + .trim() + .strip_prefix("If true: throw to monkey ") + .unwrap() + .parse() + .unwrap() + }) + .collect(); + + let false_monkeys = input + .split("\n\n") + .map(|monkey| { + let lines: Vec<&str> = monkey.lines().collect(); + + lines[5] + .trim() + .strip_prefix("If false: throw to monkey ") + .unwrap() + .parse() + .unwrap() + }) + .collect(); + + ( + items, + operation_types, + operation_values, + tests, + true_monkeys, + false_monkeys, + ) +} + +#[aoc(day11, part1)] +pub fn solve_part1(input: &(Vec>, Vec, Vec, Vec, Vec, Vec)) -> usize { + let mut items: Vec> = vec![Vec::new(); input.0.len()]; + + for (i, items_vec) in input.0.iter().enumerate() { + for item in items_vec { + items[i].push(*item); + } + } + + let operation_types = &input.1; + let operation_values = &input.2; + let tests = &input.3; + let true_monkeys = &input.4; + let false_monkeys = &input.5; + + let mut inspections = vec![0; items.len()]; + + for _ in 0..20 { + for i in 0..items.len() { + inspections[i] += items[i].len(); + + for j in 0..items[i].len() { + let item = items[i][j]; + + let new_item = match (operation_types[i].as_str(), operation_values[i]) { + ("+", v) => item + v, + ("*", v) => item * v, + ("^", _) => item * item, + _ => item, + }; + + let new_item = new_item / 3; + + if new_item % tests[i] == 0 { + items[true_monkeys[i]].push(new_item); + } else { + items[false_monkeys[i]].push(new_item); + } + } + + items[i].clear(); + } + } + + inspections.sort(); + inspections.reverse(); + + inspections[0] * inspections[1] +} + +#[aoc(day11, part2)] +pub fn solve_part2(input: &(Vec>, Vec, Vec, Vec, Vec, Vec)) -> usize { + let mut items: Vec> = vec![Vec::new(); input.0.len()]; + + for (i, items_vec) in input.0.iter().enumerate() { + for item in items_vec { + items[i].push(*item); + } + } + + let operation_types = &input.1; + let operation_values = &input.2; + let tests = &input.3; + let true_monkeys = &input.4; + let false_monkeys = &input.5; + + let divisor: usize = tests + .iter() + .product(); + + let mut inspections = vec![0; items.len()]; + + for _ in 0..10_000 { + for i in 0..items.len() { + inspections[i] += items[i].len(); + + for j in 0..items[i].len() { + let item = items[i][j]; + + let new_item = match (operation_types[i].as_str(), operation_values[i]) { + ("+", v) => item + v, + ("*", v) => item * v, + ("^", _) => item * item, + _ => item, + }; + + let new_item = new_item % divisor; + + if new_item % tests[i] == 0 { + items[true_monkeys[i]].push(new_item); + } else { + items[false_monkeys[i]].push(new_item); + } + } + + items[i].clear(); + } + } + + inspections.sort(); + inspections.reverse(); + + inspections[0] * inspections[1] +} diff --git a/noahover/rust_2022/src/lib.rs b/noahover/rust_2022/src/lib.rs index f84a9f7..71871b8 100644 --- a/noahover/rust_2022/src/lib.rs +++ b/noahover/rust_2022/src/lib.rs @@ -13,5 +13,6 @@ pub mod day7; pub mod day8; pub mod day9; pub mod day10; +pub mod day11; aoc_lib!{ year = 2022 }