Browse Source

Day 9 part 1

Tyler Hallada 5 years ago
parent
commit
d0fe734212
1 changed files with 71 additions and 11 deletions
  1. 71 11
      src/day9.rs

+ 71 - 11
src/day9.rs

@@ -10,7 +10,7 @@ use regex::Regex;
10 10
 
11 11
 type Result<T> = result::Result<T, Box<Error>>;
12 12
 
13
-const INPUT: &str = "inputs/9_test.txt";
13
+const INPUT: &str = "inputs/9.txt";
14 14
 
15 15
 #[derive(Clone, Copy, Debug, PartialEq)]
16 16
 struct GameParameters {
@@ -65,7 +65,6 @@ impl GameState {
65 65
     }
66 66
 
67 67
     fn play_until_marble(&mut self, last_marble: usize) {
68
-        println!("{}", &self);
69 68
         for _ in 0..last_marble {
70 69
             self.turn = match self.turn {
71 70
                 None => Some(0),
@@ -77,32 +76,38 @@ impl GameState {
77 76
             } else {
78 77
                 self.place_next_marble();
79 78
             }
80
-            println!("{}", &self);
81 79
         }
82
-        dbg!(&self.player_scores);
83 80
     }
84 81
 
85 82
     fn place_next_marble(&mut self) {
86 83
         self.current_marble += 1;
87 84
         if self.current_marble_index == self.circle.len() - 1 {
88 85
             self.current_marble_index = 1;
89
-            self.circle.insert(self.current_marble_index, self.current_marble);
86
+            self.circle
87
+                .insert(self.current_marble_index, self.current_marble);
90 88
         } else {
91 89
             self.current_marble_index += 2;
92
-            self.circle.insert(self.current_marble_index, self.current_marble);
90
+            self.circle
91
+                .insert(self.current_marble_index, self.current_marble);
93 92
         }
94 93
     }
95 94
 
96 95
     fn place_23rd_marble(&mut self) {
97
-        println!("23rd marble placed");
98 96
         self.current_marble += 1;
99 97
 
100
-        // TODO: handle case where this over-extends over the beginning of the vec
101
-        let removed_marble = self.circle.remove(self.current_marble_index - 7);
98
+        let mut remove_marble_index: i32 = self.current_marble_index as i32 - 7;
99
+        if remove_marble_index < 0 {
100
+            remove_marble_index += self.circle.len() as i32;
101
+        }
102
+        let removed_marble = self.circle.remove(remove_marble_index as usize);
102 103
 
103 104
         self.player_scores[self.turn.unwrap()] += removed_marble + self.current_marble;
104 105
 
105
-        self.current_marble_index -= 7;
106
+        let mut new_current_mable_index: i32 = self.current_marble_index as i32 - 7;
107
+        if new_current_mable_index < 0 {
108
+            new_current_mable_index += self.circle.len() as i32 + 1;
109
+        }
110
+        self.current_marble_index = new_current_mable_index as usize;
106 111
     }
107 112
 
108 113
     fn highest_score(&mut self) -> usize {
@@ -159,7 +164,62 @@ mod tests {
159 164
     }
160 165
 
161 166
     #[test]
162
-    fn gets_highest_score_for_game() {
167
+    fn gets_highest_score_for_game1() {
163 168
         assert_eq!(get_highest_score_for_game(TEST_GAME_PARAMS), 32);
164 169
     }
170
+
171
+    #[test]
172
+    fn gets_highest_score_for_game2() {
173
+        assert_eq!(
174
+            get_highest_score_for_game(GameParameters {
175
+                players: 10,
176
+                last_marble: 1618,
177
+            }),
178
+            8317
179
+        );
180
+    }
181
+
182
+    #[test]
183
+    fn gets_highest_score_for_game3() {
184
+        assert_eq!(
185
+            get_highest_score_for_game(GameParameters {
186
+                players: 13,
187
+                last_marble: 7999,
188
+            }),
189
+            146_373
190
+        );
191
+    }
192
+
193
+    #[test]
194
+    fn gets_highest_score_for_game4() {
195
+        assert_eq!(
196
+            get_highest_score_for_game(GameParameters {
197
+                players: 17,
198
+                last_marble: 1104,
199
+            }),
200
+            2764
201
+        );
202
+    }
203
+
204
+    #[test]
205
+    fn gets_highest_score_for_game5() {
206
+        assert_eq!(
207
+            get_highest_score_for_game(GameParameters {
208
+                players: 21,
209
+                last_marble: 6111,
210
+            }),
211
+            54718
212
+        );
213
+    }
214
+
215
+    #[test]
216
+    fn gets_highest_score_for_game6() {
217
+        assert_eq!(
218
+            get_highest_score_for_game(GameParameters {
219
+                players: 30,
220
+                last_marble: 5807,
221
+            }),
222
+            37305
223
+        );
224
+    }
165 225
 }