From 5248092252ecc178bc3b7fa62fecd30da57b0e11 Mon Sep 17 00:00:00 2001 From: Bizcochito Date: Mon, 12 Dec 2022 20:04:27 +0100 Subject: [PATCH] Day11 --- Cargo.lock | 4 ++ Cargo.toml | 2 +- day11/Cargo.toml | 8 ++++ day11/input | 55 ++++++++++++++++++++++ day11/src/main.rs | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 day11/Cargo.toml create mode 100644 day11/input create mode 100644 day11/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index bc77fc7..3c3d10c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -42,6 +42,10 @@ version = "0.1.0" name = "day10" version = "0.1.0" +[[package]] +name = "day11" +version = "0.1.0" + [[package]] name = "tiesto" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 65600da..6d71df6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ members = [ "day08", "day09", "day10", -# "day11", + "day11", # "day12", # "day13", # "day14", diff --git a/day11/Cargo.toml b/day11/Cargo.toml new file mode 100644 index 0000000..8f5b9a5 --- /dev/null +++ b/day11/Cargo.toml @@ -0,0 +1,8 @@ +[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] diff --git a/day11/input b/day11/input new file mode 100644 index 0000000..bdbd568 --- /dev/null +++ b/day11/input @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 64 + Operation: new = old * 7 + Test: divisible by 13 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 60, 84, 84, 65 + Operation: new = old + 7 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 7 + +Monkey 2: + Starting items: 52, 67, 74, 88, 51, 61 + Operation: new = old * 3 + Test: divisible by 5 + If true: throw to monkey 5 + If false: throw to monkey 7 + +Monkey 3: + Starting items: 67, 72 + Operation: new = old + 3 + Test: divisible by 2 + If true: throw to monkey 1 + If false: throw to monkey 2 + +Monkey 4: + Starting items: 80, 79, 58, 77, 68, 74, 98, 64 + Operation: new = old * old + Test: divisible by 17 + If true: throw to monkey 6 + If false: throw to monkey 0 + +Monkey 5: + Starting items: 62, 53, 61, 89, 86 + Operation: new = old + 8 + Test: divisible by 11 + If true: throw to monkey 4 + If false: throw to monkey 6 + +Monkey 6: + Starting items: 86, 89, 82 + Operation: new = old + 2 + Test: divisible by 7 + If true: throw to monkey 3 + If false: throw to monkey 0 + +Monkey 7: + Starting items: 92, 81, 70, 96, 69, 84, 83 + Operation: new = old + 4 + Test: divisible by 3 + If true: throw to monkey 4 + If false: throw to monkey 5 diff --git a/day11/src/main.rs b/day11/src/main.rs new file mode 100644 index 0000000..f275044 --- /dev/null +++ b/day11/src/main.rs @@ -0,0 +1,114 @@ +use std::fs; +use std::str::FromStr; +use std::collections::BinaryHeap; + +fn main() { + const FILE_PATH: &str = "input"; + println!("Hi this is the nineth day of AOC2022, first we will read the file {}", FILE_PATH); + let contents = fs::read_to_string(FILE_PATH) + .expect("Should have been able to read the file"); + + println!("The level of monkey business in this situation is: {}", BinaryHeap::from(play_monke(parse(&contents), 20, Some(3))).iter().take(2).fold(1, |x,y| x*y )); + println!("The level of monkey business after a lot is: {}", BinaryHeap::from(play_monke(parse(&contents), 10000, None)).iter().take(2).fold(1, |x,y| x*y )); +} +#[derive(Debug)] +enum Operation{ + Sum(u128), + Mul(u128), + Pow(u128) +} +#[derive(Debug)] +struct Monke{ + items: Vec, + op: Operation, + test: (u128, usize, usize) +} + +fn parse(s: &str) -> Vec{ + s.split("\n\n").map(|m|{ + let m: Vec<_> = m.lines().collect(); + let items = m[1].split(": ").skip(1).next().unwrap().split(", ") + .map(|n| u128::from_str(n).expect("NaN")).collect(); + let op: Vec<_> = m[2].split("old ").skip(1).next().unwrap().split(" ").collect(); + let op = match op[0] { + "+" => Operation::Sum(u128::from_str(op[1]).expect("NaN")), + "*" => if op[1] == "old"{ + Operation::Pow(2) + }else{ + Operation::Mul(u128::from_str(op[1]).expect("NaN")) + }, + _ => unreachable!() + }; + let test = (u128::from_str(m[3].split("by ").skip(1).next().unwrap()).expect("NaN"), + usize::from_str(m[4].split("monkey ").skip(1).next().unwrap()).expect("NaN"), + usize::from_str(m[5].split("monkey ").skip(1).next().unwrap()).expect("NaN")); + + Monke{items, op, test} + }).collect() +} + +fn play_monke(mut monkes: Vec, r: u32, relief: Option) -> Vec{ + let mut inspetions = vec![0; monkes.len()]; + let mcm = monkes.iter().map(|m| m.test.0).fold(1, |x,y| x*y); + for _ in 0..r{ + for i in 0..monkes.len() { + inspetions[i] += monkes[i].items.len(); + while monkes[i].items.len() > 0{ + let mut item = monkes[i].items.pop().expect("Must have an item"); + item = match monkes[i].op { + Operation::Sum(n) => item+n, + Operation::Mul(n) => item*n, + Operation::Pow(n) => item.pow(n as u32), + }; + item /= relief.unwrap_or(1); + item %= mcm; + let (tes, si, no) = monkes[i].test; + match item % tes { + 0 => monkes[si].items.push(item), + _ => monkes[no].items.push(item), + }; + } + } + } + inspetions +} + +#[cfg(test)] +mod test { + const INPUT: &str = r#"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"#; + use super::*; + #[test] + fn monke1() { + assert_eq!(BinaryHeap::from(play_monke(parse(INPUT), 20, Some(3))).iter().take(2).fold(1, |x,y| x*y ), 10605); + } + #[test] + fn monke2() { + assert_eq!(BinaryHeap::from(play_monke(parse(INPUT), 10000, None)).iter().take(2).fold(1, |x,y| x*y), 2713310158); + } +} \ No newline at end of file