2024-01-14 21:04:10 +00:00
|
|
|
//! Functionality for [`Day`]s and [`Solution`]s.
|
|
|
|
|
2022-10-02 20:20:09 +00:00
|
|
|
use {color_eyre::Result, derivative::Derivative};
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The information for an advent day of a given year.
|
2022-10-02 20:20:09 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Day {
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The advent day.
|
2022-10-02 20:20:09 +00:00
|
|
|
pub day: i32,
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The year for the advent.
|
2022-10-02 20:20:09 +00:00
|
|
|
pub year: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Day {
|
2024-01-14 21:04:10 +00:00
|
|
|
/// Create a new [`Day`] from a given day and year.
|
2022-10-02 20:20:09 +00:00
|
|
|
pub fn new(day: i32, year: i32) -> Self {
|
|
|
|
Self { day, year }
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// Return the filter string as `YEAR::DAY` for this day.
|
2022-10-02 20:20:09 +00:00
|
|
|
pub fn filter_string(&self) -> String {
|
|
|
|
format!("{}::{:02}", self.year, self.day)
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// Return the link for this day on the Advent of Code website.
|
2022-10-02 20:20:09 +00:00
|
|
|
pub fn day_link(&self) -> String {
|
|
|
|
format!("{}/day/{}", self.year_link(), self.day)
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// Return the link for this year on the Advent of Code website.
|
2022-10-02 20:20:09 +00:00
|
|
|
pub fn year_link(&self) -> String {
|
|
|
|
format!("https://adventofcode.com/{}", self.year)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// Function type alias for the expected input and outputs of a day.
|
2022-10-02 20:20:09 +00:00
|
|
|
pub type DayFunction = fn(input: &str) -> Result<String>;
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The solution to one of the advent calendar days.
|
2022-10-02 20:20:09 +00:00
|
|
|
#[derive(Derivative)]
|
|
|
|
#[derivative(Debug)]
|
|
|
|
pub struct Solution {
|
2024-01-14 21:04:10 +00:00
|
|
|
/// Which day the solution applies to.
|
2022-10-02 20:20:09 +00:00
|
|
|
pub day: Day,
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The calculated result for part one.
|
2022-10-02 20:20:09 +00:00
|
|
|
pub part_1: String,
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The calculated result for part two.
|
2022-10-02 20:20:09 +00:00
|
|
|
pub part_2: String,
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The expected result for part one (for validation).
|
2022-10-02 20:20:09 +00:00
|
|
|
pub part_1_expected: String,
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The expected result for part two (for validation).
|
2022-10-02 20:20:09 +00:00
|
|
|
pub part_2_expected: String,
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The function for part one.
|
2022-10-02 20:20:09 +00:00
|
|
|
#[derivative(Debug = "ignore")]
|
|
|
|
pub part_1_fn: DayFunction,
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// The function for part two.
|
2022-10-02 20:20:09 +00:00
|
|
|
#[derivative(Debug = "ignore")]
|
|
|
|
pub part_2_fn: DayFunction,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Solution {
|
2024-01-14 21:04:10 +00:00
|
|
|
/// Create a new [`Solution`] with a given [`Day`] and two [`DayFunction`]s.
|
2022-10-02 20:20:09 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// Helper function to add the expected results for both parts.
|
2022-10-02 20:20:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:04:10 +00:00
|
|
|
/// Run the [`DayFunction`]s for this [`Solution`] and put the results in
|
|
|
|
/// their respective fields.
|
2022-10-02 20:20:09 +00:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|