1
0
Fork 0

feat: allow different samples within one day

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-02 19:04:53 +01:00
parent 3afb870cf1
commit c692f1f510
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -9,7 +9,7 @@ pub use regex::Regex;
pub use tracing::{debug, error, info, trace, warn}; pub use tracing::{debug, error, info, trace, warn};
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
pub trait Solution<Input, Output: Display> { pub trait Solution<Output1: Display, Output2: Display> {
fn day() -> String { fn day() -> String {
let mut day = String::from(type_name::<Self>().split("::").next().unwrap()); let mut day = String::from(type_name::<Self>().split("::").next().unwrap());
day.make_ascii_lowercase(); day.make_ascii_lowercase();
@ -17,10 +17,22 @@ pub trait Solution<Input, Output: Display> {
day.to_string() day.to_string()
} }
fn parse_input<P: AsRef<Path>>(pathname: P) -> Input; fn new<P: AsRef<Path>>(pathname: P) -> Self;
fn part_1(input: &Input) -> Output; fn part_1(&mut self) -> Output1;
fn part_2(input: &Input) -> Output; 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<()> fn run(type_of_input: &str) -> Result<()>
where where
@ -36,10 +48,10 @@ pub trait Solution<Input, Output: Display> {
.init(); .init();
color_eyre::install()?; 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 1: {}", day.part_1());
info!("Part 2: {}", Self::part_2(&input)); info!("Part 2: {}", day.part_2());
Ok(()) Ok(())
} }
@ -61,16 +73,16 @@ macro_rules! test_sample {
#[test] #[test]
fn test_part_1() { fn test_part_1() {
let sample = let path = $day_struct::get_sample(1);
$day_struct::parse_input(&format!("samples/{}.txt", $day_struct::day())); let mut day = $day_struct::new(path);
assert_eq!($day_struct::part_1(&sample), $part_1); assert_eq!(day.part_1(), $part_1);
} }
#[test] #[test]
fn test_part_2() { fn test_part_2() {
let sample = let path = $day_struct::get_sample(2);
$day_struct::parse_input(&format!("samples/{}.txt", $day_struct::day())); let mut day = $day_struct::new(path);
assert_eq!($day_struct::part_2(&sample), $part_2); assert_eq!(day.part_2(), $part_2);
} }
} }
}; };