-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#[derive(Debug, Clone)] | ||
struct Monkey<'a> { | ||
// n: u8, | ||
items: Vec<usize>, | ||
operetion_obj: &'a str, | ||
operation_type: &'a str, | ||
divisible_by: usize, | ||
to_wich_monkey_trow: Vec<usize>, | ||
inspected_items: usize, | ||
} | ||
|
||
impl<'a> Monkey<'a> { | ||
fn run_monkey(&mut self) -> Vec<(usize, usize)> { | ||
let mut commands = vec![]; | ||
|
||
for i in 0..self.items.len() { | ||
self.inspected_items += 1; | ||
let mut current_item = self.items[i]; | ||
let operation_object = self.parse_operation_object(¤t_item); | ||
current_item = match self.operation_type { | ||
"+" => current_item + operation_object, | ||
"-" => current_item - operation_object, | ||
"*" => current_item * operation_object, | ||
"/" => current_item / operation_object, | ||
_ => unreachable!("Nice operation type maaaan!"), | ||
}; | ||
current_item = current_item / 3; | ||
|
||
if current_item % self.divisible_by == 0 { | ||
commands.push((current_item, self.to_wich_monkey_trow[0])); | ||
} else { | ||
commands.push((current_item, self.to_wich_monkey_trow[1])); | ||
} | ||
} | ||
|
||
self.items.clear(); | ||
commands | ||
} | ||
|
||
fn parse_operation_object(&self, current_item: &usize) -> usize { | ||
match self.operetion_obj.parse::<usize>() { | ||
Ok(x) => x, | ||
_ => *current_item, | ||
} | ||
} | ||
|
||
} | ||
|
||
fn read_input() -> Vec<Vec<&'static str>> { | ||
let input = include_str!(r"D:\rust\exercises\AOC_rust\src\days\inputs\day_11.txt"); | ||
let lines_info = input.lines().collect::<Vec<_>>(); | ||
let chunked_lines_info = lines_info | ||
.chunks(7) | ||
.map(|v| v.iter().copied().collect()) | ||
.collect(); | ||
|
||
chunked_lines_info | ||
} | ||
|
||
pub fn solve_v1(n_rounds: u8) { | ||
let monkeys_info = read_input(); | ||
let mut monkeys = Vec::with_capacity(8); | ||
//Parsing to objects | ||
for i in 0..monkeys_info.len() { | ||
let starting_items = monkeys_info[i][1] | ||
.split(": ") | ||
.last() | ||
.unwrap() | ||
.split(", ") | ||
.map(|x| x.parse::<usize>().unwrap()) | ||
.collect::<Vec<usize>>(); | ||
|
||
let mut splitted_operetion = monkeys_info[i][2].trim_start().split(' ').rev(); | ||
let operetion_obj = splitted_operetion.next().unwrap(); | ||
let operation_type = splitted_operetion.next().unwrap(); | ||
let divisible_by = monkeys_info[i][3] | ||
.split(' ') | ||
.last() | ||
.unwrap() | ||
.parse::<usize>() | ||
.unwrap(); | ||
let to_wich_monkey = vec![ | ||
monkeys_info[i][4] | ||
.split(' ') | ||
.last() | ||
.unwrap() | ||
.parse::<usize>() | ||
.unwrap(), | ||
monkeys_info[i][5] | ||
.split(' ') | ||
.last() | ||
.unwrap() | ||
.parse::<usize>() | ||
.unwrap(), | ||
]; | ||
|
||
monkeys.push(Monkey { | ||
// n: i as u8, | ||
items: starting_items, | ||
operetion_obj: operetion_obj, | ||
operation_type: operation_type, | ||
divisible_by: divisible_by, | ||
to_wich_monkey_trow: to_wich_monkey, | ||
inspected_items: 0, | ||
}) | ||
} | ||
|
||
//Take a look at them | ||
// println!("{:?}", monkeys); | ||
|
||
// Rounds started | ||
for n_round in 0..20 { | ||
for N in 0..monkeys.len() { | ||
let monkey_commands = monkeys[N].run_monkey(); //command to trow item and to which monkey | ||
monkey_commands.iter().for_each(|monkey_command| monkeys[monkey_command.1 as usize].items.push(monkey_command.0)); | ||
} | ||
} | ||
println!("-----------"); | ||
println!("{:?}", monkeys); | ||
// Rounds ended | ||
|
||
// Take 2 max number of inspected numbers | ||
let mut monkeys_interest = monkeys | ||
.iter() | ||
.map(|x| x.inspected_items) | ||
.collect::<Vec<usize>>(); | ||
monkeys_interest.sort(); | ||
|
||
//Print result | ||
println!( | ||
"First part solution: {:?}", | ||
monkeys_interest.into_iter().rev().take(2).reduce(|accum, x| accum * x).unwrap() | ||
); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
Monkey 0: | ||
Starting items: 80 | ||
Operation: new = old * 5 | ||
Test: divisible by 2 | ||
If true: throw to monkey 4 | ||
If false: throw to monkey 3 | ||
|
||
Monkey 1: | ||
Starting items: 75, 83, 74 | ||
Operation: new = old + 7 | ||
Test: divisible by 7 | ||
If true: throw to monkey 5 | ||
If false: throw to monkey 6 | ||
|
||
Monkey 2: | ||
Starting items: 86, 67, 61, 96, 52, 63, 73 | ||
Operation: new = old + 5 | ||
Test: divisible by 3 | ||
If true: throw to monkey 7 | ||
If false: throw to monkey 0 | ||
|
||
Monkey 3: | ||
Starting items: 85, 83, 55, 85, 57, 70, 85, 52 | ||
Operation: new = old + 8 | ||
Test: divisible by 17 | ||
If true: throw to monkey 1 | ||
If false: throw to monkey 5 | ||
|
||
Monkey 4: | ||
Starting items: 67, 75, 91, 72, 89 | ||
Operation: new = old + 4 | ||
Test: divisible by 11 | ||
If true: throw to monkey 3 | ||
If false: throw to monkey 1 | ||
|
||
Monkey 5: | ||
Starting items: 66, 64, 68, 92, 68, 77 | ||
Operation: new = old * 2 | ||
Test: divisible by 19 | ||
If true: throw to monkey 6 | ||
If false: throw to monkey 2 | ||
|
||
Monkey 6: | ||
Starting items: 97, 94, 79, 88 | ||
Operation: new = old * old | ||
Test: divisible by 5 | ||
If true: throw to monkey 2 | ||
If false: throw to monkey 7 | ||
|
||
Monkey 7: | ||
Starting items: 77, 85 | ||
Operation: new = old + 6 | ||
Test: divisible by 13 | ||
If true: throw to monkey 4 | ||
If false: throw to monkey 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ pub mod day_6; | |
pub mod day_7; | ||
pub mod day_8; | ||
pub mod day_9; | ||
pub mod day_10; | ||
pub mod day_10; | ||
pub mod day_11; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// #[allow(dead_code)] | ||
#[allow(non_snake_case)] | ||
#[allow(unused_variables)] | ||
pub mod days; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters