1
0
Fork 0

solution: factor out common functionality

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-12-08 21:50:50 +01:00
parent 4b97615e6a
commit 10ecf19590
Signed by: mfocko
GPG key ID: 7C47D46246790496
2 changed files with 69 additions and 2 deletions

View file

@ -1,8 +1,11 @@
mod input;
pub use input::*;
mod vector2d;
pub use vector2d::*;
mod solution;
pub use solution::*;
mod testing;
pub use testing::*;
mod vector2d;
pub use vector2d::*;

64
src/solution.rs Normal file
View file

@ -0,0 +1,64 @@
use std::fmt::Display;
use std::path::Path;
use color_eyre::eyre::Result;
use tracing::info;
use tracing_subscriber::EnvFilter;
pub trait Solution<Input, Output: Display> {
fn day() -> &'static str;
fn parse_input<P: AsRef<Path>>(pathname: P) -> Input;
fn part_1(input: &Input) -> Output;
fn part_2(input: &Input) -> Output;
fn main() -> Result<()>
where
Self: Sized,
{
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.with_target(false)
.with_file(true)
.with_line_number(true)
.without_time()
.compact()
.init();
color_eyre::install()?;
let input = Self::parse_input(format!("inputs/day{}.txt", Self::day()));
info!("Part 1: {}", Self::part_1(&input));
info!("Part 2: {}", Self::part_2(&input));
Ok(())
}
}
#[macro_export]
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);
}
#[test]
fn test_part_2() {
assert_eq!($day_struct::part_2(&SAMPLE), $part_2);
}
}
};
}