Add parsing capabilities; change to read CSV files instead of standard input
This commit is contained in:
parent
bcec7a8b4b
commit
1010ac0e20
4 changed files with 53 additions and 11 deletions
|
@ -7,4 +7,5 @@ edition = "2018"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
csv = "1.1.3"
|
csv = "1.1.3"
|
||||||
|
argparse = "0.2.2"
|
|
@ -2,7 +2,7 @@ use std::rc::{Rc, Weak};
|
||||||
use std::cell::{RefCell};
|
use std::cell::{RefCell};
|
||||||
use std::fmt::Formatter;
|
use std::fmt::Formatter;
|
||||||
|
|
||||||
static mut DEBUG: bool = false;
|
pub static mut DEBUG: bool = false;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub enum CellValue {
|
pub enum CellValue {
|
||||||
|
|
57
src/main.rs
57
src/main.rs
|
@ -5,24 +5,65 @@ use sudoku_solver::solver::solve_grid;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
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");
|
println!("Solving grid");
|
||||||
solve_grid(&mut grid);
|
solve_grid(&mut grid);
|
||||||
println!("{}", grid);
|
|
||||||
|
println!("Solved grid:\n{}", grid);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_grid() -> Result<Grid, &'static str> {
|
fn read_grid(filename: &str) -> Result<Grid, String> {
|
||||||
let mut reader = csv::ReaderBuilder::new()
|
let reader_result = csv::ReaderBuilder::new()
|
||||||
.has_headers(false)
|
.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 grid = Grid::new();
|
||||||
let mut row = 0;
|
let mut row = 0;
|
||||||
for result in reader.records() {
|
for result in reader.records() {
|
||||||
if row > 8 {
|
if row > 8 {
|
||||||
return Err("Hit row limit");
|
return Err("Hit row limit".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
let record = result.unwrap();
|
let record = result.unwrap();
|
||||||
|
@ -39,7 +80,7 @@ fn read_grid() -> Result<Grid, &'static str> {
|
||||||
}
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
Err(_error) => {return Err("Invalid cell value")}
|
Err(_error) => {return Err("Invalid cell value".to_string())}
|
||||||
};
|
};
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,7 +5,7 @@ use std::collections::HashSet;
|
||||||
|
|
||||||
use crate::grid::{Cell, Line, Grid, CellValue, LineType};
|
use crate::grid::{Cell, Line, Grid, CellValue, LineType};
|
||||||
|
|
||||||
static mut DEBUG: bool = false;
|
pub static mut DEBUG: bool = false;
|
||||||
|
|
||||||
struct FauxCell{
|
struct FauxCell{
|
||||||
index: usize,
|
index: usize,
|
||||||
|
|
Loading…
Reference in a new issue