@@ -37,15 +37,46 @@ impl<'a> Monkey<'a> {
37
37
commands
38
38
}
39
39
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
+
40
70
fn parse_operation_object ( & self , current_item : & usize ) -> usize {
41
71
match self . operetion_obj . parse :: < usize > ( ) {
42
72
Ok ( x) => x,
43
73
_ => * current_item,
44
74
}
45
75
}
46
-
47
76
}
48
77
78
+
79
+
49
80
fn read_input ( ) -> Vec < Vec < & ' static str > > {
50
81
let input = include_str ! ( r"D:\rust\exercises\AOC_rust\src\days\inputs\day_11.txt" ) ;
51
82
let lines_info = input. lines ( ) . collect :: < Vec < _ > > ( ) ;
@@ -57,7 +88,7 @@ fn read_input() -> Vec<Vec<&'static str>> {
57
88
chunked_lines_info
58
89
}
59
90
60
- pub fn solve_v1 ( n_rounds : u8 ) {
91
+ pub fn solve_v1 ( n_rounds_part_1 : u8 , n_rounds_part_2 : u16 ) {
61
92
let monkeys_info = read_input ( ) ;
62
93
let mut monkeys = Vec :: with_capacity ( 8 ) ;
63
94
//Parsing to objects
@@ -109,14 +140,12 @@ pub fn solve_v1(n_rounds: u8) {
109
140
// println!("{:?}", monkeys);
110
141
111
142
// Rounds started
112
- for n_round in 0 ..20 {
143
+ for n_round in 0 ..n_rounds_part_1 {
113
144
for N in 0 ..monkeys. len ( ) {
114
145
let monkey_commands = monkeys[ N ] . run_monkey ( ) ; //command to trow item and to which monkey
115
146
monkey_commands. iter ( ) . for_each ( |monkey_command| monkeys[ monkey_command. 1 as usize ] . items . push ( monkey_command. 0 ) ) ;
116
147
}
117
148
}
118
- println ! ( "-----------" ) ;
119
- println ! ( "{:?}" , monkeys) ;
120
149
// Rounds ended
121
150
122
151
// Take 2 max number of inspected numbers
@@ -132,4 +161,76 @@ pub fn solve_v1(n_rounds: u8) {
132
161
monkeys_interest. into_iter( ) . rev( ) . take( 2 ) . reduce( |accum, x| accum * x) . unwrap( )
133
162
) ;
134
163
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
+
135
236
}
0 commit comments