Setup for 2022

This commit is contained in:
2022-12-02 06:30:14 +01:00
parent f9463ba6c1
commit c889b75029
5 changed files with 110 additions and 0 deletions

28
2022/src/bin/template.rs Normal file
View File

@@ -0,0 +1,28 @@
use aoc::Solver;
pub struct Day {}
fn main() {
Day::solve();
}
#[test]
fn part1() {
Day::test(aoc::Part::ONE);
}
#[test]
fn part2() {
Day::test(aoc::Part::TWO);
}
impl aoc::Solver for Day {
fn day() -> u8 {
0
}
fn part1(input: &str) -> u32 {
input.len() as u32
}
fn part2(input: &str) -> u32 {
input.len() as u32
}
}

49
2022/src/lib.rs Normal file
View File

@@ -0,0 +1,49 @@
use std::fs;
pub enum Part {
ONE,
TWO
}
pub trait Solver {
fn day() -> u8;
fn part1(input: &str) -> u32;
fn part2(input: &str) -> u32;
fn test(part: Part) {
// Select the right function
let fun = match part {
Part::ONE => Self::part1,
Part::TWO => Self::part2,
};
// Read the test input
let test = fs::read_to_string(format!("input/{}/test", Self::day())).expect("Test file does not exist!");
// Get the correct test result
let result: u32 = match test.split("\n")
.skip(part as usize)
.next()
.expect("Expected second line to contain result for part1")
.parse() {
Ok(result) => result,
_ => 0, // Use zero if no value is specified yet
};
// Get the input for the test
// @TODO This creates a new string, would be nice if we could actually get a slice here
let input = test.split("\n")
.skip(2)
.collect::<Vec<&str>>()
.join("\n");
// Assert that the result matches the expected value
assert_eq!(fun(&input), result);
}
fn solve() {
let input = fs::read_to_string(format!("input/{}/test", Self::day())).expect("Input file does not exist!");
println!("Part 1: {}", Self::part1(&input));
println!("Part 2: {}", Self::part2(&input));
}
}