From c692f1f510f67d7b3026073e59ce174e94fb0319 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 2 Dec 2023 19:04:53 +0100 Subject: [PATCH] feat: allow different samples within one day Signed-off-by: Matej Focko --- src/solution.rs | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/solution.rs b/src/solution.rs index 9e0ce2c..1d46c5b 100644 --- a/src/solution.rs +++ b/src/solution.rs @@ -9,7 +9,7 @@ pub use regex::Regex; pub use tracing::{debug, error, info, trace, warn}; use tracing_subscriber::EnvFilter; -pub trait Solution { +pub trait Solution { fn day() -> String { let mut day = String::from(type_name::().split("::").next().unwrap()); day.make_ascii_lowercase(); @@ -17,10 +17,22 @@ pub trait Solution { day.to_string() } - fn parse_input>(pathname: P) -> Input; + fn new>(pathname: P) -> Self; - fn part_1(input: &Input) -> Output; - fn part_2(input: &Input) -> Output; + fn part_1(&mut self) -> Output1; + fn part_2(&mut self) -> Output2; + + fn get_sample(part: i32) -> String { + let possible_paths = [ + format!("samples/{}.txt", Self::day()), + format!("samples/{}_{}.txt", Self::day(), part), + ]; + + possible_paths + .into_iter() + .find(|p| Path::new(p).exists()) + .expect("at least one sample should exist") + } fn run(type_of_input: &str) -> Result<()> where @@ -36,10 +48,10 @@ pub trait Solution { .init(); color_eyre::install()?; - let input = Self::parse_input(format!("{}s/{}.txt", type_of_input, Self::day())); + let mut day = Self::new(format!("{}s/{}.txt", type_of_input, Self::day())); - info!("Part 1: {}", Self::part_1(&input)); - info!("Part 2: {}", Self::part_2(&input)); + info!("Part 1: {}", day.part_1()); + info!("Part 2: {}", day.part_2()); Ok(()) } @@ -61,16 +73,16 @@ macro_rules! test_sample { #[test] fn test_part_1() { - let sample = - $day_struct::parse_input(&format!("samples/{}.txt", $day_struct::day())); - assert_eq!($day_struct::part_1(&sample), $part_1); + let path = $day_struct::get_sample(1); + let mut day = $day_struct::new(path); + assert_eq!(day.part_1(), $part_1); } #[test] fn test_part_2() { - let sample = - $day_struct::parse_input(&format!("samples/{}.txt", $day_struct::day())); - assert_eq!($day_struct::part_2(&sample), $part_2); + let path = $day_struct::get_sample(2); + let mut day = $day_struct::new(path); + assert_eq!(day.part_2(), $part_2); } } };