From 1010ac0e20c1c8b98f3ee72c62b2906201829b83 Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Mon, 21 Sep 2020 17:12:30 -0700 Subject: [PATCH] Add parsing capabilities; change to read CSV files instead of standard input --- Cargo.toml | 3 ++- src/grid.rs | 2 +- src/main.rs | 57 +++++++++++++++++++++++++++++++++++++++++++-------- src/solver.rs | 2 +- 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9b9df07..ca88ad2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -csv = "1.1.3" \ No newline at end of file +csv = "1.1.3" +argparse = "0.2.2" \ No newline at end of file diff --git a/src/grid.rs b/src/grid.rs index 60b2abd..04837d9 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -2,7 +2,7 @@ use std::rc::{Rc, Weak}; use std::cell::{RefCell}; use std::fmt::Formatter; -static mut DEBUG: bool = false; +pub static mut DEBUG: bool = false; #[derive(Clone, Debug, Eq, PartialEq)] pub enum CellValue { diff --git a/src/main.rs b/src/main.rs index 6c36cb5..f3e3032 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,24 +5,65 @@ use sudoku_solver::solver::solve_grid; fn main() { - let mut grid = read_grid().unwrap(); + let mut debug = false; + let mut filename = String::new(); + { // this block limits scope of borrows by ap.refer() method + let mut ap = argparse::ArgumentParser::new(); + ap.set_description("Solve Sudoku puzzles"); + ap.refer(&mut debug) + .add_option(&["--debug"], argparse::StoreTrue, "Run in debug mode"); - println!("{}", grid); + ap.refer(&mut filename) + .required() + .add_argument("filename", argparse::Store, "Path to puzzle CSV file"); + + ap.parse_args_or_exit(); + } + + if debug { + unsafe { + sudoku_solver::grid::DEBUG = true; + sudoku_solver::solver::DEBUG = true; + } + } + + + let mut grid = match read_grid(&filename) { + Ok(grid) => grid, + Err(e) => { + eprintln!("Error while reading grid: \"{}\"", e); + std::process::exit(1); + } + }; + + println!("Grid to be solved:\n{}", grid); println!("Solving grid"); solve_grid(&mut grid); - println!("{}", grid); + + println!("Solved grid:\n{}", grid); + + } -fn read_grid() -> Result { - let mut reader = csv::ReaderBuilder::new() +fn read_grid(filename: &str) -> Result { + let reader_result = csv::ReaderBuilder::new() .has_headers(false) - .from_reader(std::io::stdin()); + .from_path(filename); + + let mut reader = match reader_result { + Ok(reader) => reader, + Err(e) => { + let e = e.to_string(); + return Err(e); + } + }; + let grid = Grid::new(); let mut row = 0; for result in reader.records() { if row > 8 { - return Err("Hit row limit"); + return Err("Hit row limit".to_string()); } let record = result.unwrap(); @@ -39,7 +80,7 @@ fn read_grid() -> Result { } }, - Err(_error) => {return Err("Invalid cell value")} + Err(_error) => {return Err("Invalid cell value".to_string())} }; }, diff --git a/src/solver.rs b/src/solver.rs index d03d785..40c4add 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -5,7 +5,7 @@ use std::collections::HashSet; use crate::grid::{Cell, Line, Grid, CellValue, LineType}; -static mut DEBUG: bool = false; +pub static mut DEBUG: bool = false; struct FauxCell{ index: usize,