Setup for 2023
This commit is contained in:
46
2023/src/lib.rs
Normal file
46
2023/src/lib.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
#![feature(test)]
|
||||
extern crate test;
|
||||
|
||||
use core::fmt;
|
||||
use std::{fmt::Debug, fs};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
pub trait Solver {
|
||||
type Output1: fmt::Display + Debug + PartialEq;
|
||||
type Output2: fmt::Display + Debug + PartialEq;
|
||||
|
||||
fn day() -> u8;
|
||||
fn part1(input: &str) -> Self::Output1;
|
||||
fn part2(input: &str) -> Self::Output2;
|
||||
|
||||
fn test<T: Fn(&str) -> U, U: Debug + PartialEq>(f: T, name: &str, result: U) -> Result<()> {
|
||||
// Select the right function
|
||||
|
||||
// Read the test input
|
||||
let input = fs::read_to_string(format!("input/{:02}/{name}", Self::day()))
|
||||
.with_context(|| format!("Failed to read '{}' for day {:02}", name, Self::day()))?;
|
||||
|
||||
// Assert that the result matches the expected value
|
||||
assert_eq!(f(&input), result);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn solve() -> Result<()> {
|
||||
let input = fs::read_to_string(format!("input/{:02}/input", Self::day()))
|
||||
.with_context(|| format!("Failed to read 'input' for day {:02}", Self::day()))?;
|
||||
println!("Part 1:\n{}", Self::part1(&input));
|
||||
println!("Part 2:\n{}", Self::part2(&input));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn benchmark<T: Fn(&str) -> U, U: Debug + PartialEq>(f: T, b: &mut test::Bencher) {
|
||||
let input = fs::read_to_string(format!("input/{:02}/input", Self::day()))
|
||||
.with_context(|| format!("Failed to read 'input' for day {:02}", Self::day()))
|
||||
.unwrap();
|
||||
|
||||
b.iter(|| f(&input));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user