day 11
This commit is contained in:
parent
9ecb37b48d
commit
c36d703042
|
@ -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
|
||||
|
|
@ -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]
|
|
@ -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
|
|
@ -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
|
|
@ -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<u32>,
|
||||
rhs: Option<u32>,
|
||||
operand: Operand,
|
||||
}
|
||||
|
||||
impl Operation {
|
||||
fn new(lhs: Option<u32>, rhs: Option<u32>, 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<u32>,
|
||||
operation: Operation,
|
||||
test: Test,
|
||||
business: usize,
|
||||
}
|
||||
|
||||
impl Monkey {
|
||||
fn new(items: Vec<u32>, operation: Operation, test: Test) -> Self {
|
||||
Self {
|
||||
items,
|
||||
operation,
|
||||
test,
|
||||
business: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn compute(&mut self) -> Vec<usize> {
|
||||
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::<Monkey>::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<u32> = 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::<usize>::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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where P: AsRef<Path>, {
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
|
@ -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
|
||||
|
|
@ -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]
|
|
@ -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<u64>,
|
||||
rhs: Option<u64>,
|
||||
operand: Operand,
|
||||
}
|
||||
|
||||
impl Operation {
|
||||
fn new(lhs: Option<u64>, rhs: Option<u64>, 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<u64>,
|
||||
operation: Operation,
|
||||
test: Test,
|
||||
business: usize,
|
||||
}
|
||||
|
||||
impl Monkey {
|
||||
fn new(items: Vec<u64>, operation: Operation, test: Test) -> Self {
|
||||
Self {
|
||||
items,
|
||||
operation,
|
||||
test,
|
||||
business: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn compute(&mut self) -> Vec<usize> {
|
||||
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::<Monkey>::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<u64> = 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::<usize>::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<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where P: AsRef<Path>, {
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
Loading…
Reference in New Issue