@@ -6,6 +6,86 @@ enum Operator {
66 Mul
77}
88
9+ #[ derive( Clone , Copy ) ]
10+ struct Item {
11+ base_2 : i32 ,
12+ base_3 : i32 ,
13+ base_5 : i32 ,
14+ base_7 : i32 ,
15+ base_11 : i32 ,
16+ base_13 : i32 ,
17+ base_17 : i32 ,
18+ base_19 : i32 ,
19+ }
20+
21+ impl Item {
22+ fn init ( val : i32 ) -> Item {
23+ return Item {
24+ base_2 : val % 2 ,
25+ base_3 : val % 3 ,
26+ base_5 : val % 5 ,
27+ base_7 : val % 7 ,
28+ base_11 : val % 11 ,
29+ base_13 : val % 13 ,
30+ base_17 : val % 17 ,
31+ base_19 : val % 19 ,
32+ }
33+ }
34+
35+ fn is_divisible ( self , val : i32 ) -> bool {
36+ match val {
37+ 2 => self . base_2 == 0 ,
38+ 3 => self . base_3 == 0 ,
39+ 5 => self . base_5 == 0 ,
40+ 7 => self . base_7 == 0 ,
41+ 11 => self . base_11 == 0 ,
42+ 13 => self . base_13 == 0 ,
43+ 17 => self . base_17 == 0 ,
44+ 19 => self . base_19 == 0 ,
45+ _ => false
46+ }
47+ }
48+
49+ fn add ( self , val : i32 ) -> Item {
50+ return Item {
51+ base_2 : ( self . base_2 + val) % 2 ,
52+ base_3 : ( self . base_3 + val) % 3 ,
53+ base_5 : ( self . base_5 + val) % 5 ,
54+ base_7 : ( self . base_7 + val) % 7 ,
55+ base_11 : ( self . base_11 + val) % 11 ,
56+ base_13 : ( self . base_13 + val) % 13 ,
57+ base_17 : ( self . base_17 + val) % 17 ,
58+ base_19 : ( self . base_19 + val) % 19 ,
59+ }
60+ }
61+
62+ fn mul ( self , val : i32 ) -> Item {
63+ return Item {
64+ base_2 : ( self . base_2 * val) % 2 ,
65+ base_3 : ( self . base_3 * val) % 3 ,
66+ base_5 : ( self . base_5 * val) % 5 ,
67+ base_7 : ( self . base_7 * val) % 7 ,
68+ base_11 : ( self . base_11 * val) % 11 ,
69+ base_13 : ( self . base_13 * val) % 13 ,
70+ base_17 : ( self . base_17 * val) % 17 ,
71+ base_19 : ( self . base_19 * val) % 19 ,
72+ }
73+ }
74+
75+ fn sq ( self ) -> Item {
76+ return Item {
77+ base_2 : ( self . base_2 * self . base_2 ) % 2 ,
78+ base_3 : ( self . base_3 * self . base_3 ) % 3 ,
79+ base_5 : ( self . base_5 * self . base_5 ) % 5 ,
80+ base_7 : ( self . base_7 * self . base_7 ) % 7 ,
81+ base_11 : ( self . base_11 * self . base_11 ) % 11 ,
82+ base_13 : ( self . base_13 * self . base_13 ) % 13 ,
83+ base_17 : ( self . base_17 * self . base_17 ) % 17 ,
84+ base_19 : ( self . base_19 * self . base_19 ) % 19 ,
85+ }
86+ }
87+ }
88+
989struct Monkey {
1090 starting_items : Vec < i32 > ,
1191 operator : Operator ,
@@ -58,6 +138,58 @@ fn part1(monkeys: &Vec<Monkey>) {
58138 println ! ( "Part 1: {}" , monkey_inspections[ 0 ] * monkey_inspections[ 1 ] ) ;
59139}
60140
141+ fn part2 ( monkeys : & Vec < Monkey > ) {
142+ let mut monkey_states: Vec < Vec < Item > > = vec ! [ vec![ ] ; monkeys. len( ) ] ;
143+ let mut monkey_inspections: Vec < i32 > = vec ! [ 0 ; monkeys. len( ) ] ;
144+
145+ for m in 0 ..monkeys. len ( ) {
146+ for i in 0 ..monkeys[ m] . starting_items . len ( ) {
147+ monkey_states[ m] . push ( Item :: init ( monkeys[ m] . starting_items [ i] ) )
148+ }
149+ }
150+
151+ for _ in 0 ..10000 {
152+ for m in 0 ..monkeys. len ( ) {
153+ let monkey = & monkeys[ m] ;
154+ let monkey_items = monkey_states[ m] . clone ( ) ;
155+ for item in monkey_items {
156+ // Increase monkey inspection.
157+ monkey_inspections[ m] += 1 ;
158+
159+ // Calculate new worry.
160+ let mut item = item. clone ( ) ;
161+ if monkey. operand == None {
162+ if monkey. operator == Operator :: Add {
163+ item = item. mul ( 2 ) ;
164+ } else {
165+ item = item. sq ( )
166+ }
167+ } else {
168+ if monkey. operator == Operator :: Add {
169+ item = item. add ( monkey. operand . unwrap ( ) ) ;
170+ } else {
171+ item = item. mul ( monkey. operand . unwrap ( ) ) ;
172+ }
173+ }
174+
175+ // Throw to monkey.
176+ if item. is_divisible ( monkey. test_divisible ) {
177+ monkey_states[ monkey. throw_to_monkey_if_true ] . push ( item) ;
178+ } else {
179+ monkey_states[ monkey. throw_to_monkey_if_false ] . push ( item) ;
180+ }
181+ }
182+
183+ monkey_states[ m] = vec ! [ ]
184+ }
185+ }
186+
187+ monkey_inspections. sort ( ) ;
188+ monkey_inspections. reverse ( ) ;
189+ println ! ( "Part 2: {}" , monkey_inspections[ 0 ] ) ;
190+ println ! ( "Part 2: {}" , monkey_inspections[ 1 ] ) ;
191+ }
192+
61193pub fn run ( ) {
62194 let input = input_reader:: read_file_in_cwd ( "src/day_11.data" ) ;
63195
@@ -96,4 +228,5 @@ pub fn run() {
96228 } ) . collect ( ) ;
97229
98230 part1 ( & monkeys) ;
231+ part2 ( & monkeys) ;
99232}
0 commit comments