Skip to content

Instantly share code, notes, and snippets.

Created June 21, 2015 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/ea4b440c9bf77f49592e to your computer and use it in GitHub Desktop.
Save anonymous/ea4b440c9bf77f49592e to your computer and use it in GitHub Desktop.
use std::collections::HashMap;
use std::fs::File;
use std::path::Path;
use std::io::Read;
// Given a Cell:
// if 0-2 neighbors are on, the cell goes OFF
// if 3-5 neighbors are on, the cell goes ON
// if 6-8 neighbors are on, the cell goes OFF
#[derive(Debug, PartialEq)]
enum Cell {
ON,
OFF,
}
type Board = Vec<Vec<Cell>>;
fn generate_board(config_path: &str) -> Board {
let board_path = Path::new(config_path);
let mut file: File = File::open(&board_path).ok().expect("Could not open file");
let mut content: String = String::new();
file.read_to_string(&mut content).ok().expect("Could not read file");
let lines = content.trim().split("\n");
let mut board = Board::new();
for line in lines {
let mut row = Vec::new();
for character in line.chars() {
let cell = match character {
'1' => Cell::ON,
'0' => Cell::OFF,
_ => panic!("Invalid character"),
};
row.push(cell);
}
board.push(row);
}
board
}
// fn neighbors(coordinate: (usize, usize), board: &Board) -> Vec<Cell> {
// let row_count = &board.len();
// let column_count = &board[0].len();
// }
fn next_gen(coordinate: (usize, usize), board: &Board) -> Cell {
// let (row, column) = coordinate;
// let cell : &Cell = &board[row][column];
Cell::OFF
}
fn step(board: &mut Board) {
let mut cells_to_update: HashMap<(usize, usize), Cell> = HashMap::new();
for (row_index, row) in board.iter().enumerate() {
for (column_index, cell) in row.iter().enumerate() {
let cell_next = next_gen((row_index, column_index), &board);
if *cell != cell_next {
cells_to_update.insert((row_index, column_index), cell_next);
}
}
}
println!("To Update: {:?}", cells_to_update);
for ((row_index, column_index), cell) in cells_to_update {
board[row_index][column_index] = cell;
}
}
fn main() {
let mut board: Board = generate_board("board.txt");
step(&mut board);
println!("{:?}", board);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment