Skip to content

Commit

Permalink
day_11 Second part
Browse files Browse the repository at this point in the history
  • Loading branch information
Groni3000 committed Jan 4, 2023
1 parent 891b675 commit 203bf92
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 9 deletions.
111 changes: 106 additions & 5 deletions src/days/day_11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,46 @@ impl<'a> Monkey<'a> {
commands
}

fn run_monkey_2(&mut self, general_mult:&usize) -> 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(&current_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 % general_mult;

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<_>>();
Expand All @@ -57,7 +88,7 @@ fn read_input() -> Vec<Vec<&'static str>> {
chunked_lines_info
}

pub fn solve_v1(n_rounds: u8) {
pub fn solve_v1(n_rounds_part_1: u8, n_rounds_part_2: u16) {
let monkeys_info = read_input();
let mut monkeys = Vec::with_capacity(8);
//Parsing to objects
Expand Down Expand Up @@ -109,14 +140,12 @@ pub fn solve_v1(n_rounds: u8) {
// println!("{:?}", monkeys);

// Rounds started
for n_round in 0..20 {
for n_round in 0..n_rounds_part_1 {
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
Expand All @@ -132,4 +161,76 @@ pub fn solve_v1(n_rounds: u8) {
monkeys_interest.into_iter().rev().take(2).reduce(|accum, x| accum * x).unwrap()
);

// ----------------------------
// --------Second part---------
// ----------------------------

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,
})
}

let multipliers = monkeys.clone().into_iter().map(|x| x.divisible_by).collect::<Vec<_>>();
let general_mult = multipliers.clone().into_iter().reduce(|SAD , x| SAD * x).unwrap();
for n_round in 0..n_rounds_part_2 {
for N in 0..monkeys.len() {
let monkey_commands = monkeys[N].run_monkey_2(&general_mult); //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));
}
}

// 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!(
"Second part solution: {:?}",
monkeys_interest.into_iter().rev().take(2).reduce(|accum, x| accum * x).unwrap()
);

}
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
// #[allow(dead_code)]
#[allow(non_snake_case)]
#[allow(unused_variables)]
pub mod days;
pub mod days;

#[allow(dead_code)]
fn print_objects<T>(x: &Vec<T>)
where T:std::fmt::Debug{
for el in x{
println!("{:?}", el);
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn main() {
// day_10::solve_v1(); // Ummm... I, personally, think that is awful puzzle.

// DAY 11
day_11::solve_v1(20); //
// println!("{}", (3 as u32)/(3 as u32))
day_11::solve_v1(20, 10_000); //
// println!("{}", 68/17);

}

0 comments on commit 203bf92

Please sign in to comment.