diff --git a/Cargo.lock b/Cargo.lock index 71c57a0..6e8578a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ name = "advent_of_code_2025" version = "0.1.0" dependencies = [ "grid", + "sanitise-file-name", ] [[package]] @@ -14,3 +15,9 @@ name = "grid" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9e2d4c0a8296178d8802098410ca05d86b17a10bb5ab559b3fb404c1f948220" + +[[package]] +name = "sanitise-file-name" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19d36299972b96b8ae7e8f04ecbf75fb41a27bf3781af00abcf57609774cb911" diff --git a/Cargo.toml b/Cargo.toml index 3c13b18..b379acd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2024" [dependencies] grid = "1.0.0" +sanitise-file-name = "1.0.0" diff --git a/src/solvers.rs b/src/solvers.rs index 176f11e..15877df 100644 --- a/src/solvers.rs +++ b/src/solvers.rs @@ -1,6 +1,8 @@ +use sanitise_file_name; use std::fs::File; use std::io; use std::io::BufRead; +use std::path; use std::path::Path; pub mod cafeteria; @@ -10,12 +12,13 @@ pub mod trash_compactor; pub trait Solver { const PUZZLE_INDEX: u8; const PUZZLE_NAME: &'static str; - // TODO: Replace constant input filename by a conversion from the puzzle name in the runner. - const INPUT_FILENAME: &'static str; - fn puzzle_index(&self) -> u8 { Self::PUZZLE_INDEX } - fn puzzle_name(&self) -> &'static str { Self::PUZZLE_NAME } - fn input_filename(&self) -> &'static str { Self::INPUT_FILENAME } + fn puzzle_index(&self) -> u8 { + Self::PUZZLE_INDEX + } + fn puzzle_name(&self) -> &'static str { + Self::PUZZLE_NAME + } fn process_data(&self, reader: R) -> (u64, u64); } @@ -32,7 +35,11 @@ pub fn run(solver: S) { // "target/release/" directory with "data/" directory in package root or parent directory. let paths = vec!["./data", "../data", "../../data", "../../../data"]; - match read_data_file(solver.input_filename(), &paths) { + let filename = sanitise_file_name::sanitise(solver.puzzle_name()) + .to_lowercase() + .replace(" ", "_") + + ".txt"; + match read_data_file(filename, &paths) { Ok(reader) => { let result = solver.process_data(reader); print_result(result); @@ -45,10 +52,7 @@ fn print_result(result: (u64, u64)) { println!("Part 1: {:?}\nPart 2: {:?}\n", result.0, result.1); } -fn read_data_file( - filename: T, - search_paths: &Vec, -) -> Result, String> +fn read_data_file(filename: String, search_paths: &Vec) -> Result, String> where T: AsRef, { @@ -60,7 +64,11 @@ where let message = search_paths .iter() - .map(|path| path.as_ref().join(&filename).display().to_string()) + .filter_map(|p| { + path::absolute(p.as_ref().join(&filename)) + .ok() + .and_then(|p| Some(p.display().to_string())) + }) .collect::>() .join("\n"); Err(format!( diff --git a/src/solvers/cafeteria.rs b/src/solvers/cafeteria.rs index dbc9fb4..89b919f 100644 --- a/src/solvers/cafeteria.rs +++ b/src/solvers/cafeteria.rs @@ -8,7 +8,6 @@ pub struct Cafeteria {} impl Solver for Cafeteria { const PUZZLE_INDEX: u8 = 5; const PUZZLE_NAME: &'static str = "Cafeteria"; - const INPUT_FILENAME: &'static str = "cafeteria.txt"; fn process_data(&self, mut reader: R) -> (u64, u64) { // Builds intervals collection. diff --git a/src/solvers/printing_department.rs b/src/solvers/printing_department.rs index ed001e9..1c865b8 100644 --- a/src/solvers/printing_department.rs +++ b/src/solvers/printing_department.rs @@ -7,7 +7,6 @@ pub struct PrintingDepartment {} impl Solver for PrintingDepartment { const PUZZLE_INDEX: u8 = 4; const PUZZLE_NAME: &'static str = "Printing Department"; - const INPUT_FILENAME: &'static str = "printing_department.txt"; fn process_data(&self, reader: R) -> (u64, u64) { let mut grid = Grid::new(0, 0); diff --git a/src/solvers/trash_compactor.rs b/src/solvers/trash_compactor.rs index ca38b11..f4eca2a 100644 --- a/src/solvers/trash_compactor.rs +++ b/src/solvers/trash_compactor.rs @@ -7,7 +7,6 @@ pub struct TrashCompactor {} impl Solver for TrashCompactor { const PUZZLE_INDEX: u8 = 6; const PUZZLE_NAME: &'static str = "Trash Compactor"; - const INPUT_FILENAME: &'static str = "trash_compactor.txt"; fn process_data(&self, reader: R) -> (u64, u64) { let mut part1 = 0;