Skip to content

Commit 203bf92

Browse files
committed
day_11 Second part
1 parent 891b675 commit 203bf92

File tree

3 files changed

+117
-9
lines changed

3 files changed

+117
-9
lines changed

src/days/day_11.rs

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,46 @@ impl<'a> Monkey<'a> {
3737
commands
3838
}
3939

40+
fn run_monkey_2(&mut self, general_mult:&usize) -> Vec<(usize, usize)> {
41+
let mut commands = vec![];
42+
43+
for i in 0..self.items.len() {
44+
self.inspected_items += 1;
45+
let mut current_item = self.items[i];
46+
let operation_object = self.parse_operation_object(&current_item);
47+
48+
current_item = match self.operation_type {
49+
"+" => current_item + operation_object,
50+
"-" => current_item - operation_object,
51+
"*" => current_item * operation_object,
52+
"/" => current_item / operation_object,
53+
_ => unreachable!("Nice operation type maaaan!"),
54+
};
55+
56+
current_item = current_item % general_mult;
57+
58+
if current_item % self.divisible_by == 0 {
59+
commands.push((current_item, self.to_wich_monkey_trow[0]));
60+
} else {
61+
62+
commands.push((current_item, self.to_wich_monkey_trow[1]));
63+
}
64+
}
65+
66+
self.items.clear();
67+
commands
68+
}
69+
4070
fn parse_operation_object(&self, current_item: &usize) -> usize {
4171
match self.operetion_obj.parse::<usize>() {
4272
Ok(x) => x,
4373
_ => *current_item,
4474
}
4575
}
46-
4776
}
4877

78+
79+
4980
fn read_input() -> Vec<Vec<&'static str>> {
5081
let input = include_str!(r"D:\rust\exercises\AOC_rust\src\days\inputs\day_11.txt");
5182
let lines_info = input.lines().collect::<Vec<_>>();
@@ -57,7 +88,7 @@ fn read_input() -> Vec<Vec<&'static str>> {
5788
chunked_lines_info
5889
}
5990

60-
pub fn solve_v1(n_rounds: u8) {
91+
pub fn solve_v1(n_rounds_part_1: u8, n_rounds_part_2: u16) {
6192
let monkeys_info = read_input();
6293
let mut monkeys = Vec::with_capacity(8);
6394
//Parsing to objects
@@ -109,14 +140,12 @@ pub fn solve_v1(n_rounds: u8) {
109140
// println!("{:?}", monkeys);
110141

111142
// Rounds started
112-
for n_round in 0..20 {
143+
for n_round in 0..n_rounds_part_1 {
113144
for N in 0..monkeys.len() {
114145
let monkey_commands = monkeys[N].run_monkey(); //command to trow item and to which monkey
115146
monkey_commands.iter().for_each(|monkey_command| monkeys[monkey_command.1 as usize].items.push(monkey_command.0));
116147
}
117148
}
118-
println!("-----------");
119-
println!("{:?}", monkeys);
120149
// Rounds ended
121150

122151
// Take 2 max number of inspected numbers
@@ -132,4 +161,76 @@ pub fn solve_v1(n_rounds: u8) {
132161
monkeys_interest.into_iter().rev().take(2).reduce(|accum, x| accum * x).unwrap()
133162
);
134163

164+
// ----------------------------
165+
// --------Second part---------
166+
// ----------------------------
167+
168+
let mut monkeys = Vec::with_capacity(8);
169+
//Parsing to objects
170+
for i in 0..monkeys_info.len() {
171+
let starting_items = monkeys_info[i][1]
172+
.split(": ")
173+
.last()
174+
.unwrap()
175+
.split(", ")
176+
.map(|x| x.parse::<usize>().unwrap())
177+
.collect::<Vec<usize>>();
178+
179+
let mut splitted_operetion = monkeys_info[i][2].trim_start().split(' ').rev();
180+
let operetion_obj = splitted_operetion.next().unwrap();
181+
let operation_type = splitted_operetion.next().unwrap();
182+
let divisible_by = monkeys_info[i][3]
183+
.split(' ')
184+
.last()
185+
.unwrap()
186+
.parse::<usize>()
187+
.unwrap();
188+
let to_wich_monkey = vec![
189+
monkeys_info[i][4]
190+
.split(' ')
191+
.last()
192+
.unwrap()
193+
.parse::<usize>()
194+
.unwrap(),
195+
monkeys_info[i][5]
196+
.split(' ')
197+
.last()
198+
.unwrap()
199+
.parse::<usize>()
200+
.unwrap(),
201+
];
202+
203+
monkeys.push(Monkey {
204+
// n: i as u8,
205+
items: starting_items,
206+
operetion_obj: operetion_obj,
207+
operation_type: operation_type,
208+
divisible_by: divisible_by,
209+
to_wich_monkey_trow: to_wich_monkey,
210+
inspected_items: 0,
211+
})
212+
}
213+
214+
let multipliers = monkeys.clone().into_iter().map(|x| x.divisible_by).collect::<Vec<_>>();
215+
let general_mult = multipliers.clone().into_iter().reduce(|SAD , x| SAD * x).unwrap();
216+
for n_round in 0..n_rounds_part_2 {
217+
for N in 0..monkeys.len() {
218+
let monkey_commands = monkeys[N].run_monkey_2(&general_mult); //command to trow item and to which monkey
219+
monkey_commands.iter().for_each(|monkey_command| monkeys[monkey_command.1 as usize].items.push(monkey_command.0));
220+
}
221+
}
222+
223+
// Take 2 max number of inspected numbers
224+
let mut monkeys_interest = monkeys
225+
.iter()
226+
.map(|x| x.inspected_items)
227+
.collect::<Vec<usize>>();
228+
monkeys_interest.sort();
229+
230+
//Print result
231+
println!(
232+
"Second part solution: {:?}",
233+
monkeys_interest.into_iter().rev().take(2).reduce(|accum, x| accum * x).unwrap()
234+
);
235+
135236
}

src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
// #[allow(dead_code)]
21
#[allow(non_snake_case)]
32
#[allow(unused_variables)]
4-
pub mod days;
3+
pub mod days;
4+
5+
#[allow(dead_code)]
6+
fn print_objects<T>(x: &Vec<T>)
7+
where T:std::fmt::Debug{
8+
for el in x{
9+
println!("{:?}", el);
10+
}
11+
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn main() {
3737
// day_10::solve_v1(); // Ummm... I, personally, think that is awful puzzle.
3838

3939
// DAY 11
40-
day_11::solve_v1(20); //
41-
// println!("{}", (3 as u32)/(3 as u32))
40+
day_11::solve_v1(20, 10_000); //
41+
// println!("{}", 68/17);
4242

4343
}

0 commit comments

Comments
 (0)