From c36d703042a5851a5568c077cf9fe8a90ea84fb9 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Wed, 8 Feb 2023 00:06:41 +0100 Subject: [PATCH] day 11 --- aoc_11a/.gitignore | 12 +++++ aoc_11a/Cargo.toml | 8 +++ aoc_11a/input | 55 +++++++++++++++++++ aoc_11a/input2 | 27 ++++++++++ aoc_11a/src/main.rs | 127 ++++++++++++++++++++++++++++++++++++++++++++ aoc_11b/.gitignore | 12 +++++ aoc_11b/Cargo.toml | 8 +++ aoc_11b/src/main.rs | 126 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 375 insertions(+) create mode 100644 aoc_11a/.gitignore create mode 100644 aoc_11a/Cargo.toml create mode 100644 aoc_11a/input create mode 100644 aoc_11a/input2 create mode 100644 aoc_11a/src/main.rs create mode 100644 aoc_11b/.gitignore create mode 100644 aoc_11b/Cargo.toml create mode 100644 aoc_11b/src/main.rs diff --git a/aoc_11a/.gitignore b/aoc_11a/.gitignore new file mode 100644 index 0000000..62bd1a4 --- /dev/null +++ b/aoc_11a/.gitignore @@ -0,0 +1,12 @@ +# ---> Rust +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + diff --git a/aoc_11a/Cargo.toml b/aoc_11a/Cargo.toml new file mode 100644 index 0000000..ff345d9 --- /dev/null +++ b/aoc_11a/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc_11a" +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/aoc_11a/input b/aoc_11a/input new file mode 100644 index 0000000..a0fdfb2 --- /dev/null +++ b/aoc_11a/input @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 72, 97 + Operation: new = old * 13 + Test: divisible by 19 + If true: throw to monkey 5 + If false: throw to monkey 6 + +Monkey 1: + Starting items: 55, 70, 90, 74, 95 + Operation: new = old * old + Test: divisible by 7 + If true: throw to monkey 5 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 74, 97, 66, 57 + Operation: new = old + 6 + Test: divisible by 17 + If true: throw to monkey 1 + If false: throw to monkey 0 + +Monkey 3: + Starting items: 86, 54, 53 + Operation: new = old + 2 + Test: divisible by 13 + If true: throw to monkey 1 + If false: throw to monkey 2 + +Monkey 4: + Starting items: 50, 65, 78, 50, 62, 99 + Operation: new = old + 3 + Test: divisible by 11 + If true: throw to monkey 3 + If false: throw to monkey 7 + +Monkey 5: + Starting items: 90 + Operation: new = old + 4 + Test: divisible by 2 + If true: throw to monkey 4 + If false: throw to monkey 6 + +Monkey 6: + Starting items: 88, 92, 63, 94, 96, 82, 53, 53 + Operation: new = old + 8 + Test: divisible by 5 + If true: throw to monkey 4 + If false: throw to monkey 7 + +Monkey 7: + Starting items: 70, 60, 71, 69, 77, 70, 98 + Operation: new = old * 7 + Test: divisible by 3 + If true: throw to monkey 2 + If false: throw to monkey 3 diff --git a/aoc_11a/input2 b/aoc_11a/input2 new file mode 100644 index 0000000..30e09e5 --- /dev/null +++ b/aoc_11a/input2 @@ -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/aoc_11a/src/main.rs b/aoc_11a/src/main.rs new file mode 100644 index 0000000..357b319 --- /dev/null +++ b/aoc_11a/src/main.rs @@ -0,0 +1,127 @@ +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; +use std::iter::Zip; + +#[derive(Debug)] +enum Operand { + Add, + Multiply, +} + +#[derive(Debug)] +struct Test { + value: u32, + if_true: usize, + if_false: usize, +} + +impl Test { + fn new(value: u32, if_true: usize, if_false: usize) -> Self { + Self { + value, + if_true, + if_false, + } + } + + fn compute(&self, item: u32) -> usize { + match item % self.value { + 0 => self.if_true, + _ => self.if_false, + } + } +} + +#[derive(Debug)] +struct Operation { + lhs: Option, + rhs: Option, + operand: Operand, +} + +impl Operation { + fn new(lhs: Option, rhs: Option, operand: Operand) -> Self { + Self { + lhs, + rhs, + operand, + } + } + + fn compute(&self, item: u32) -> u32 { + let lhs = self.lhs.unwrap_or(item); + let rhs = self.rhs.unwrap_or(item); + match self.operand { + Operand::Multiply => lhs * rhs, + Operand::Add => lhs + rhs, + } + } +} + +#[derive(Debug)] +struct Monkey { + items: Vec, + operation: Operation, + test: Test, + business: usize, +} + +impl Monkey { + fn new(items: Vec, operation: Operation, test: Test) -> Self { + Self { + items, + operation, + test, + business: 0, + } + } + + fn compute(&mut self) -> Vec { + self.business += self.items.len(); + self.items = self.items.iter().map(|x| self.operation.compute(*x)).collect(); + self.items = self.items.iter().map(|x| x / 3).collect(); + self.items.iter().map(|x| self.test.compute(*x)).collect() + } +} + + + +fn main() { + let file_path = String::from("input"); + let mut monkeys = Vec::::new(); + + monkeys.push(Monkey::new(vec![72, 97], Operation::new(None, Some(13), Operand::Multiply), Test::new(19, 5, 6))); + monkeys.push(Monkey::new(vec![55, 70, 90, 74, 95], Operation::new(None, None, Operand::Multiply), Test::new(7, 5, 0))); + monkeys.push(Monkey::new(vec![74, 97, 66, 57], Operation::new(None, Some(6), Operand::Add), Test::new(17, 1, 0))); + monkeys.push(Monkey::new(vec![86, 54, 53], Operation::new(None, Some(2), Operand::Add), Test::new(13, 1, 2))); + monkeys.push(Monkey::new(vec![50, 65, 78, 50, 62, 99], Operation::new(None, Some(3), Operand::Add), Test::new(11, 3, 7))); + monkeys.push(Monkey::new(vec![90], Operation::new(None, Some(4), Operand::Add), Test::new(2, 4, 6))); + monkeys.push(Monkey::new(vec![88, 92, 63, 94, 96, 82, 53, 53], Operation::new(None, Some(8), Operand::Add), Test::new(5, 4, 7))); + monkeys.push(Monkey::new(vec![70, 60, 71, 69, 77, 70, 98], Operation::new(None, Some(7), Operand::Multiply), Test::new(3, 2, 3))); + + for _ in 0..20 { + for i in 0..8 { + let pass_to_i_vec = monkeys[i].compute(); + let test: Vec = monkeys[i].items.drain(..).collect(); + let my_zip = test.iter().zip(pass_to_i_vec.iter()); + for (v, i) in my_zip { + monkeys[*i].items.push(*v); + } + } + } + + println!("{:#?}", monkeys); + let mut monkey_business = Vec::::new(); + for i in 0..8 { + monkey_business.push(monkeys[i].business); + } + monkey_business.sort(); + println!("{}", monkey_business[6] * monkey_business[7]); +} + +fn read_lines

(filename: P) -> io::Result>> +where P: AsRef, { + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} diff --git a/aoc_11b/.gitignore b/aoc_11b/.gitignore new file mode 100644 index 0000000..62bd1a4 --- /dev/null +++ b/aoc_11b/.gitignore @@ -0,0 +1,12 @@ +# ---> Rust +# Generated by Cargo +# will have compiled files and executables +/target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + diff --git a/aoc_11b/Cargo.toml b/aoc_11b/Cargo.toml new file mode 100644 index 0000000..ac38df8 --- /dev/null +++ b/aoc_11b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc_11b" +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/aoc_11b/src/main.rs b/aoc_11b/src/main.rs new file mode 100644 index 0000000..c3a7e93 --- /dev/null +++ b/aoc_11b/src/main.rs @@ -0,0 +1,126 @@ +use std::fs::File; +use std::io::{self, BufRead}; +use std::path::Path; +use std::iter::Zip; + +#[derive(Debug)] +enum Operand { + Add, + Multiply, +} + +#[derive(Debug)] +struct Test { + value: u64, + if_true: usize, + if_false: usize, +} + +impl Test { + fn new(value: u64, if_true: usize, if_false: usize) -> Self { + Self { + value, + if_true, + if_false, + } + } + + fn compute(&self, item: u64) -> usize { + match item % self.value { + 0 => self.if_true, + _ => self.if_false, + } + } +} + +#[derive(Debug)] +struct Operation { + lhs: Option, + rhs: Option, + operand: Operand, +} + +impl Operation { + fn new(lhs: Option, rhs: Option, operand: Operand) -> Self { + Self { + lhs, + rhs, + operand, + } + } + + fn compute(&self, item: u64) -> u64 { + let lhs = self.lhs.unwrap_or(item); + let rhs = self.rhs.unwrap_or(item); + match self.operand { + Operand::Multiply => lhs * rhs, + Operand::Add => lhs + rhs, + } + } +} + +#[derive(Debug)] +struct Monkey { + items: Vec, + operation: Operation, + test: Test, + business: usize, +} + +impl Monkey { + fn new(items: Vec, operation: Operation, test: Test) -> Self { + Self { + items, + operation, + test, + business: 0, + } + } + + fn compute(&mut self) -> Vec { + self.business += self.items.len(); + self.items = self.items.iter().map(|x| self.operation.compute(*x)).collect(); + self.items = self.items.iter().map(|x| x % 9699690).collect(); + self.items.iter().map(|x| self.test.compute(*x)).collect() + } +} + + + +fn main() { + let mut monkeys = Vec::::new(); + + monkeys.push(Monkey::new(vec![72, 97], Operation::new(None, Some(13), Operand::Multiply), Test::new(19, 5, 6))); + monkeys.push(Monkey::new(vec![55, 70, 90, 74, 95], Operation::new(None, None, Operand::Multiply), Test::new(7, 5, 0))); + monkeys.push(Monkey::new(vec![74, 97, 66, 57], Operation::new(None, Some(6), Operand::Add), Test::new(17, 1, 0))); + monkeys.push(Monkey::new(vec![86, 54, 53], Operation::new(None, Some(2), Operand::Add), Test::new(13, 1, 2))); + monkeys.push(Monkey::new(vec![50, 65, 78, 50, 62, 99], Operation::new(None, Some(3), Operand::Add), Test::new(11, 3, 7))); + monkeys.push(Monkey::new(vec![90], Operation::new(None, Some(4), Operand::Add), Test::new(2, 4, 6))); + monkeys.push(Monkey::new(vec![88, 92, 63, 94, 96, 82, 53, 53], Operation::new(None, Some(8), Operand::Add), Test::new(5, 4, 7))); + monkeys.push(Monkey::new(vec![70, 60, 71, 69, 77, 70, 98], Operation::new(None, Some(7), Operand::Multiply), Test::new(3, 2, 3))); + + for _ in 0..10000 { + for i in 0..8 { + let pass_to_i_vec = monkeys[i].compute(); + let test: Vec = monkeys[i].items.drain(..).collect(); + let my_zip = test.iter().zip(pass_to_i_vec.iter()); + for (v, i) in my_zip { + monkeys[*i].items.push(*v); + } + } + } + + println!("{:#?}", monkeys); + let mut monkey_business = Vec::::new(); + for i in 0..8 { + monkey_business.push(monkeys[i].business); + } + monkey_business.sort(); + println!("{}", monkey_business[6] * monkey_business[7]); +} + +fn read_lines

(filename: P) -> io::Result>> +where P: AsRef, { + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +}