From d0fe734212093155ce4e0d25f01cfba3a248b7da Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Sat, 16 Feb 2019 15:09:13 -0500 Subject: [PATCH] Day 9 part 1 --- src/day9.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 71 insertions(+), 11 deletions(-) diff --git a/src/day9.rs b/src/day9.rs index 7b24f6c..179aa90 100644 --- a/src/day9.rs +++ b/src/day9.rs @@ -10,7 +10,7 @@ use regex::Regex; type Result = result::Result>; -const INPUT: &str = "inputs/9_test.txt"; +const INPUT: &str = "inputs/9.txt"; #[derive(Clone, Copy, Debug, PartialEq)] struct GameParameters { @@ -65,7 +65,6 @@ impl GameState { } fn play_until_marble(&mut self, last_marble: usize) { - println!("{}", &self); for _ in 0..last_marble { self.turn = match self.turn { None => Some(0), @@ -77,32 +76,38 @@ impl GameState { } else { self.place_next_marble(); } - println!("{}", &self); } - dbg!(&self.player_scores); } fn place_next_marble(&mut self) { self.current_marble += 1; if self.current_marble_index == self.circle.len() - 1 { self.current_marble_index = 1; - self.circle.insert(self.current_marble_index, self.current_marble); + self.circle + .insert(self.current_marble_index, self.current_marble); } else { self.current_marble_index += 2; - self.circle.insert(self.current_marble_index, self.current_marble); + self.circle + .insert(self.current_marble_index, self.current_marble); } } fn place_23rd_marble(&mut self) { - println!("23rd marble placed"); self.current_marble += 1; - // TODO: handle case where this over-extends over the beginning of the vec - let removed_marble = self.circle.remove(self.current_marble_index - 7); + let mut remove_marble_index: i32 = self.current_marble_index as i32 - 7; + if remove_marble_index < 0 { + remove_marble_index += self.circle.len() as i32; + } + let removed_marble = self.circle.remove(remove_marble_index as usize); self.player_scores[self.turn.unwrap()] += removed_marble + self.current_marble; - self.current_marble_index -= 7; + let mut new_current_mable_index: i32 = self.current_marble_index as i32 - 7; + if new_current_mable_index < 0 { + new_current_mable_index += self.circle.len() as i32 + 1; + } + self.current_marble_index = new_current_mable_index as usize; } fn highest_score(&mut self) -> usize { @@ -159,7 +164,62 @@ mod tests { } #[test] - fn gets_highest_score_for_game() { + fn gets_highest_score_for_game1() { assert_eq!(get_highest_score_for_game(TEST_GAME_PARAMS), 32); } + + #[test] + fn gets_highest_score_for_game2() { + assert_eq!( + get_highest_score_for_game(GameParameters { + players: 10, + last_marble: 1618, + }), + 8317 + ); + } + + #[test] + fn gets_highest_score_for_game3() { + assert_eq!( + get_highest_score_for_game(GameParameters { + players: 13, + last_marble: 7999, + }), + 146_373 + ); + } + + #[test] + fn gets_highest_score_for_game4() { + assert_eq!( + get_highest_score_for_game(GameParameters { + players: 17, + last_marble: 1104, + }), + 2764 + ); + } + + #[test] + fn gets_highest_score_for_game5() { + assert_eq!( + get_highest_score_for_game(GameParameters { + players: 21, + last_marble: 6111, + }), + 54718 + ); + } + + #[test] + fn gets_highest_score_for_game6() { + assert_eq!( + get_highest_score_for_game(GameParameters { + players: 30, + last_marble: 5807, + }), + 37305 + ); + } }