1
0

Replace filename const by sanitise() function

This commit is contained in:
Stefan Müller
2025-12-08 17:07:34 +01:00
parent 449e7f75ac
commit c03eca490e
6 changed files with 27 additions and 14 deletions
Generated
+7
View File
@@ -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"
+1
View File
@@ -5,3 +5,4 @@ edition = "2024"
[dependencies]
grid = "1.0.0"
sanitise-file-name = "1.0.0"
+19 -11
View File
@@ -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<R: BufRead>(&self, reader: R) -> (u64, u64);
}
@@ -32,7 +35,11 @@ pub fn run<S: Solver>(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<T>(
filename: T,
search_paths: &Vec<T>,
) -> Result<io::BufReader<File>, String>
fn read_data_file<T>(filename: String, search_paths: &Vec<T>) -> Result<io::BufReader<File>, String>
where
T: AsRef<Path>,
{
@@ -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::<Vec<_>>()
.join("\n");
Err(format!(
-1
View File
@@ -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<R: BufRead>(&self, mut reader: R) -> (u64, u64) {
// Builds intervals collection.
-1
View File
@@ -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<R: BufRead>(&self, reader: R) -> (u64, u64) {
let mut grid = Grid::new(0, 0);
-1
View File
@@ -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<R: BufRead>(&self, reader: R) -> (u64, u64) {
let mut part1 = 0;