1
Fork 0
advent-of-code/source/solution.rs

84 lines
1.6 KiB
Rust

use {color_eyre::Result, derivative::Derivative};
#[derive(Debug)]
pub struct Day {
pub day: i32,
pub year: i32,
}
impl Day {
pub fn new(day: i32, year: i32) -> Self {
Self { day, year }
}
pub fn filter_string(&self) -> String {
format!("{}::{:02}", self.year, self.day)
}
pub fn day_link(&self) -> String {
format!("{}/day/{}", self.year_link(), self.day)
}
pub fn year_link(&self) -> String {
format!("https://adventofcode.com/{}", self.year)
}
}
pub type DayFunction = fn(input: &str) -> Result<String>;
#[derive(Derivative)]
#[derivative(Debug)]
pub struct Solution {
pub day: Day,
pub part_1: String,
pub part_2: String,
pub part_1_expected: String,
pub part_2_expected: String,
#[derivative(Debug = "ignore")]
pub part_1_fn: DayFunction,
#[derivative(Debug = "ignore")]
pub part_2_fn: DayFunction,
}
impl Solution {
pub fn new(day: Day, part_1_fn: DayFunction, part_2_fn: DayFunction) -> Self {
Self {
day,
part_1: String::new(),
part_2: String::new(),
part_1_expected: String::new(),
part_2_expected: String::new(),
part_1_fn,
part_2_fn,
}
}
pub fn with_expected<A: std::fmt::Display, B: std::fmt::Display>(
self,
part_1_expected: A,
part_2_expected: B,
) -> Self {
Self {
part_1_expected: format!("{part_1_expected}"),
part_2_expected: format!("{part_2_expected}"),
..self
}
}
pub fn solve(self, data: &str) -> Result<Self> {
let (part_1, part_2) = ((self.part_1_fn)(data)?, (self.part_2_fn)(data)?);
Ok(Self {
part_1,
part_2,
..self
})
}
}