Day 9 part 2

Switch to using a `VecDeque` instead and rotate it on each turn.
This commit is contained in:
Tyler Hallada 2019-02-16 15:49:30 -05:00
parent d0fe734212
commit 21a0815fd7
2 changed files with 35 additions and 27 deletions

View File

@ -5,6 +5,7 @@ use std::fmt;
use std::fs; use std::fs;
use std::result; use std::result;
use std::str::FromStr; use std::str::FromStr;
use std::collections::VecDeque;
use regex::Regex; use regex::Regex;
@ -47,8 +48,7 @@ impl FromStr for GameParameters {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
struct GameState { struct GameState {
turn: Option<usize>, turn: Option<usize>,
circle: Vec<usize>, circle: VecDeque<usize>,
current_marble_index: usize,
current_marble: usize, current_marble: usize,
player_scores: Vec<usize>, player_scores: Vec<usize>,
} }
@ -57,13 +57,28 @@ impl GameState {
fn new(parameters: GameParameters) -> GameState { fn new(parameters: GameParameters) -> GameState {
GameState { GameState {
turn: None, turn: None,
circle: vec![0], circle: VecDeque::from(vec![0]),
current_marble_index: 0,
current_marble: 0, current_marble: 0,
player_scores: vec![0; parameters.players as usize], player_scores: vec![0; parameters.players as usize],
} }
} }
fn rotate_clockwise(&mut self, steps: usize) {
for _ in 0..steps {
if let Some(marble) = self.circle.pop_back() {
self.circle.push_front(marble);
}
}
}
fn rotate_counter_clockwise(&mut self, steps: usize) {
for _ in 0..steps {
if let Some(marble) = self.circle.pop_front() {
self.circle.push_back(marble);
}
}
}
fn play_until_marble(&mut self, last_marble: usize) { fn play_until_marble(&mut self, last_marble: usize) {
for _ in 0..last_marble { for _ in 0..last_marble {
self.turn = match self.turn { self.turn = match self.turn {
@ -81,33 +96,19 @@ impl GameState {
fn place_next_marble(&mut self) { fn place_next_marble(&mut self) {
self.current_marble += 1; self.current_marble += 1;
if self.current_marble_index == self.circle.len() - 1 { self.rotate_counter_clockwise(1);
self.current_marble_index = 1; self.circle.push_back(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);
}
} }
fn place_23rd_marble(&mut self) { fn place_23rd_marble(&mut self) {
self.current_marble += 1; self.current_marble += 1;
let mut remove_marble_index: i32 = self.current_marble_index as i32 - 7; self.player_scores[self.turn.unwrap()] += self.current_marble;
if remove_marble_index < 0 { self.rotate_clockwise(7);
remove_marble_index += self.circle.len() as i32; if let Some(marble) = self.circle.pop_back() {
self.player_scores[self.turn.unwrap()] += marble;
} }
let removed_marble = self.circle.remove(remove_marble_index as usize); self.rotate_counter_clockwise(1);
self.player_scores[self.turn.unwrap()] += removed_marble + self.current_marble;
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 { fn highest_score(&mut self) -> usize {
@ -122,7 +123,7 @@ impl fmt::Display for GameState {
Some(turn) => write!(f, "[{}] ", turn + 1)?, Some(turn) => write!(f, "[{}] ", turn + 1)?,
} }
for (index, marble) in self.circle.iter().enumerate() { for (index, marble) in self.circle.iter().enumerate() {
if index == self.current_marble_index { if index == self.circle.len() - 1 {
write!(f, "({}) ", marble)?; write!(f, "({}) ", marble)?;
} else { } else {
write!(f, "{} ", marble)?; write!(f, "{} ", marble)?;
@ -137,6 +138,12 @@ pub fn solve_part1() -> Result<usize> {
Ok(get_highest_score_for_game(game_params)) Ok(get_highest_score_for_game(game_params))
} }
pub fn solve_part2() -> Result<usize> {
let mut game_params = read_game_parameters(INPUT)?;
game_params.last_marble *= 100;
Ok(get_highest_score_for_game(game_params))
}
fn read_game_parameters(filename: &str) -> Result<GameParameters> { fn read_game_parameters(filename: &str) -> Result<GameParameters> {
let game_params = fs::read_to_string(filename)?; let game_params = fs::read_to_string(filename)?;
Ok(game_params.parse()?) Ok(game_params.parse()?)

View File

@ -39,5 +39,6 @@ fn main() {
// println!("{}", day8::solve_part1().unwrap()); // println!("{}", day8::solve_part1().unwrap());
// println!("{}", day8::solve_part2().unwrap()); // println!("{}", day8::solve_part2().unwrap());
println!("Day 9:"); println!("Day 9:");
println!("{}", day9::solve_part1().unwrap()); // println!("{}", day9::solve_part1().unwrap());
println!("{}", day9::solve_part2().unwrap());
} }