Replace filename const by sanitise() function
This commit is contained in:
Generated
+7
@@ -7,6 +7,7 @@ name = "advent_of_code_2025"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"grid",
|
"grid",
|
||||||
|
"sanitise-file-name",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -14,3 +15,9 @@ name = "grid"
|
|||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f9e2d4c0a8296178d8802098410ca05d86b17a10bb5ab559b3fb404c1f948220"
|
checksum = "f9e2d4c0a8296178d8802098410ca05d86b17a10bb5ab559b3fb404c1f948220"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sanitise-file-name"
|
||||||
|
version = "1.0.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "19d36299972b96b8ae7e8f04ecbf75fb41a27bf3781af00abcf57609774cb911"
|
||||||
|
|||||||
@@ -5,3 +5,4 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
grid = "1.0.0"
|
grid = "1.0.0"
|
||||||
|
sanitise-file-name = "1.0.0"
|
||||||
|
|||||||
+19
-11
@@ -1,6 +1,8 @@
|
|||||||
|
use sanitise_file_name;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::BufRead;
|
use std::io::BufRead;
|
||||||
|
use std::path;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
pub mod cafeteria;
|
pub mod cafeteria;
|
||||||
@@ -10,12 +12,13 @@ pub mod trash_compactor;
|
|||||||
pub trait Solver {
|
pub trait Solver {
|
||||||
const PUZZLE_INDEX: u8;
|
const PUZZLE_INDEX: u8;
|
||||||
const PUZZLE_NAME: &'static str;
|
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_index(&self) -> u8 {
|
||||||
fn puzzle_name(&self) -> &'static str { Self::PUZZLE_NAME }
|
Self::PUZZLE_INDEX
|
||||||
fn input_filename(&self) -> &'static str { Self::INPUT_FILENAME }
|
}
|
||||||
|
fn puzzle_name(&self) -> &'static str {
|
||||||
|
Self::PUZZLE_NAME
|
||||||
|
}
|
||||||
|
|
||||||
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64);
|
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.
|
// "target/release/" directory with "data/" directory in package root or parent directory.
|
||||||
let paths = vec!["./data", "../data", "../../data", "../../../data"];
|
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) => {
|
Ok(reader) => {
|
||||||
let result = solver.process_data(reader);
|
let result = solver.process_data(reader);
|
||||||
print_result(result);
|
print_result(result);
|
||||||
@@ -45,10 +52,7 @@ 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>(
|
fn read_data_file<T>(filename: String, search_paths: &Vec<T>) -> Result<io::BufReader<File>, String>
|
||||||
filename: T,
|
|
||||||
search_paths: &Vec<T>,
|
|
||||||
) -> Result<io::BufReader<File>, String>
|
|
||||||
where
|
where
|
||||||
T: AsRef<Path>,
|
T: AsRef<Path>,
|
||||||
{
|
{
|
||||||
@@ -60,7 +64,11 @@ where
|
|||||||
|
|
||||||
let message = search_paths
|
let message = search_paths
|
||||||
.iter()
|
.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<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\n");
|
.join("\n");
|
||||||
Err(format!(
|
Err(format!(
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ pub struct Cafeteria {}
|
|||||||
impl Solver for Cafeteria {
|
impl Solver for Cafeteria {
|
||||||
const PUZZLE_INDEX: u8 = 5;
|
const PUZZLE_INDEX: u8 = 5;
|
||||||
const PUZZLE_NAME: &'static str = "Cafeteria";
|
const PUZZLE_NAME: &'static str = "Cafeteria";
|
||||||
const INPUT_FILENAME: &'static str = "cafeteria.txt";
|
|
||||||
|
|
||||||
fn process_data<R: BufRead>(&self, mut reader: R) -> (u64, u64) {
|
fn process_data<R: BufRead>(&self, mut reader: R) -> (u64, u64) {
|
||||||
// Builds intervals collection.
|
// Builds intervals collection.
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ pub struct PrintingDepartment {}
|
|||||||
impl Solver for PrintingDepartment {
|
impl Solver for PrintingDepartment {
|
||||||
const PUZZLE_INDEX: u8 = 4;
|
const PUZZLE_INDEX: u8 = 4;
|
||||||
const PUZZLE_NAME: &'static str = "Printing Department";
|
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) {
|
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) {
|
||||||
let mut grid = Grid::new(0, 0);
|
let mut grid = Grid::new(0, 0);
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ pub struct TrashCompactor {}
|
|||||||
impl Solver for TrashCompactor {
|
impl Solver for TrashCompactor {
|
||||||
const PUZZLE_INDEX: u8 = 6;
|
const PUZZLE_INDEX: u8 = 6;
|
||||||
const PUZZLE_NAME: &'static str = "Trash Compactor";
|
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) {
|
fn process_data<R: BufRead>(&self, reader: R) -> (u64, u64) {
|
||||||
let mut part1 = 0;
|
let mut part1 = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user