Add run_solver() function for testing
This commit is contained in:
+18
-14
@@ -23,6 +23,10 @@ pub trait Solver {
|
|||||||
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64);
|
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The "../../data" and "../../../data" paths are useful for running the binary from
|
||||||
|
// "target/release/" directory with "data/" directory in package root or parent directory.
|
||||||
|
const DATA_PATHS: &'static [&'static str] = &["./data", "../data", "../../data", "../../../data"];
|
||||||
|
|
||||||
pub fn run<S: Solver>(solver: S) {
|
pub fn run<S: Solver>(solver: S) {
|
||||||
println!(
|
println!(
|
||||||
"--- Day {}: {} ---",
|
"--- Day {}: {} ---",
|
||||||
@@ -30,29 +34,29 @@ pub fn run<S: Solver>(solver: S) {
|
|||||||
solver.puzzle_name()
|
solver.puzzle_name()
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Convert to constant and add "example" paths.
|
match run_solver(solver, &DATA_PATHS) {
|
||||||
// The "../../data" and "../../../data" paths are useful for running the binary from
|
Ok(result) => print_result(result),
|
||||||
// "target/release/" directory with "data/" directory in package root or parent directory.
|
Err(error) => eprintln!("{}", error),
|
||||||
let paths = vec!["./data", "../data", "../../data", "../../../data"];
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let filename = sanitise_file_name::sanitise(solver.puzzle_name())
|
pub fn run_solver<S: Solver>(solver: S, paths: &[&str]) -> Result<(u64, u64), String> {
|
||||||
|
let reader = read_data_file(input_filename(solver.puzzle_name()), paths)?;
|
||||||
|
Ok(solver.process_data(reader))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn input_filename(puzzle_name: &str) -> String {
|
||||||
|
sanitise_file_name::sanitise(puzzle_name)
|
||||||
.to_lowercase()
|
.to_lowercase()
|
||||||
.replace(" ", "_")
|
.replace(" ", "_")
|
||||||
+ ".txt";
|
+ ".txt"
|
||||||
match read_data_file(filename, &paths) {
|
|
||||||
Ok(reader) => {
|
|
||||||
let result = solver.process_data(reader);
|
|
||||||
print_result(result);
|
|
||||||
}
|
|
||||||
Err(error) => eprintln!("{}", error),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_result(result: (u64, u64)) {
|
fn print_result(result: (u64, u64)) {
|
||||||
println!("Part 1: {:?}\nPart 2: {:?}\n", result.0, result.1);
|
println!("Part 1: {:?}\nPart 2: {:?}\n", result.0, result.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_data_file<T>(filename: String, search_paths: &Vec<T>) -> Result<io::BufReader<File>, String>
|
fn read_data_file<T>(filename: String, search_paths: &[T]) -> Result<io::BufReader<File>, String>
|
||||||
where
|
where
|
||||||
T: AsRef<Path>,
|
T: AsRef<Path>,
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user