1
0
Fork 0

solution: adjust after a refactor

• publicly export reused imports
• deduce string with the day from the typename
• parse sample separately in the tests
  · reason: thread safety when running tests

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-12-08 22:36:58 +01:00
parent 10ecf19590
commit f53be47327
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -1,12 +1,18 @@
use std::any::type_name;
use std::fmt::Display;
use std::path::Path;
pub use std::path::Path;
use color_eyre::eyre::Result;
pub use color_eyre::eyre::Result;
use tracing::info;
use tracing_subscriber::EnvFilter;
pub trait Solution<Input, Output: Display> {
fn day() -> &'static str;
fn day() -> String {
let mut day = String::from(type_name::<Self>().split("::").next().unwrap());
day.make_ascii_lowercase();
day.to_string()
}
fn parse_input<P: AsRef<Path>>(pathname: P) -> Input;
@ -27,7 +33,7 @@ pub trait Solution<Input, Output: Display> {
.init();
color_eyre::install()?;
let input = Self::parse_input(format!("inputs/day{}.txt", Self::day()));
let input = Self::parse_input(format!("inputs/{}.txt", Self::day()));
info!("Part 1: {}", Self::part_1(&input));
info!("Part 2: {}", Self::part_2(&input));
@ -41,23 +47,20 @@ macro_rules! test_sample {
($mod_name:ident, $day_struct:tt, $part_1:expr, $part_2:expr) => {
#[cfg(test)]
mod $mod_name {
use lazy_static::lazy_static;
use super::*;
lazy_static! {
static ref SAMPLE: Input =
$day_struct::parse_input(&format!("samples/day{}.txt", $day_struct::day()));
}
#[test]
fn test_part_1() {
assert_eq!($day_struct::part_1(&SAMPLE), $part_1);
let sample =
$day_struct::parse_input(&format!("samples/{}.txt", $day_struct::day()));
assert_eq!($day_struct::part_1(&sample), $part_1);
}
#[test]
fn test_part_2() {
assert_eq!($day_struct::part_2(&SAMPLE), $part_2);
let sample =
$day_struct::parse_input(&format!("samples/{}.txt", $day_struct::day()));
assert_eq!($day_struct::part_2(&sample), $part_2);
}
}
};