Solution for day 11 part 2

main
Inga 🏳‍🌈 4 years ago
parent 07e6072465
commit 6daf2f69ff
  1. 17
      day11/src/main.rs
  2. 47
      day11/src/rules_hard.rs

@ -7,15 +7,16 @@ mod board_metadata;
mod game;
mod rules;
mod rules_easy;
mod rules_hard;
use binary::State;
use game::Game;
use rules::Rules;
use rules_easy::RulesEasy;
use rules_hard::RulesHard;
fn main() {
let stdin = io::stdin();
let lines: Vec<_> = stdin.lock().lines().map(|line| line.unwrap()).collect();
let mut game = Game::from_input::<RulesEasy>(&lines);
fn solve<T: Rules>(lines: &[String]) {
let mut game = Game::from_input::<T>(&lines);
//game.print_board();
@ -31,3 +32,11 @@ fn main() {
game.print_board();
println!("Board stabilized at {} occupied seats", game.get_count_of_cells_for_state(State::SeatOccupied));
}
fn main() {
let stdin = io::stdin();
let lines: Vec<_> = stdin.lock().lines().map(|line| line.unwrap()).collect();
solve::<RulesEasy>(&lines);
solve::<RulesHard>(&lines);
}

@ -0,0 +1,47 @@
use enum_map::EnumMap;
use ndarray::Array2;
use strum::IntoEnumIterator;
use crate::binary::{Direction, State};
use crate::board_metadata::{BoardMetadata,CellLocation};
use crate::rules::Rules;
fn find_neighbour(cell_location: CellLocation, direction: Direction, board_metadata: &BoardMetadata, original_states: &Array2<State>) -> Option<CellLocation> {
let mut location = cell_location;
loop {
match board_metadata.get_neighbour_location(location, direction) {
Some(new_location) => match original_states[new_location] {
State::SeatEmpty | State::SeatOccupied => {
return Some(new_location);
},
_ => {
location = new_location;
},
},
None => {
return None;
},
}
}
}
pub struct RulesHard {}
impl Rules for RulesHard {
fn get_next_state(current_state: State, neighbour_counts: EnumMap<State, usize>) -> State {
match current_state {
State::SeatEmpty => if neighbour_counts[State::SeatOccupied] == 0 { State::SeatOccupied } else { State::SeatEmpty },
State::SeatOccupied => if neighbour_counts[State::SeatOccupied] >= 5 { State::SeatEmpty } else { State::SeatOccupied },
other => other
}
}
fn get_neighbours(cell_location: CellLocation, board_metadata: &BoardMetadata, original_states: &Array2<State>) -> EnumMap<Direction, Option<CellLocation>> {
let mut neighbours = EnumMap::new();
for direction in Direction::iter() {
neighbours[direction] = find_neighbour(cell_location, direction, &board_metadata, &original_states)
}
neighbours
}
}
Loading…
Cancel
Save