Added the ability to have more then one test input, and fixed actual input

This commit is contained in:
Dreaded_X 2022-12-02 16:29:13 +01:00
parent a6a2385a85
commit a52cdafb8a
Signed by: Dreaded_X
GPG Key ID: 76BDEC4E165D8AD9
5 changed files with 2506 additions and 27 deletions

2500
2022/input/2/input Normal file

File diff suppressed because it is too large Load Diff

View File

@ -6,11 +6,11 @@ fn main() {
}
#[test]
fn part1() {
Day::test(aoc::Part::ONE);
Day::test(aoc::Part::ONE, "test-1", 24000);
}
#[test]
fn part2() {
Day::test(aoc::Part::TWO);
Day::test(aoc::Part::TWO, "test-1", 45000);
}

View File

@ -6,11 +6,7 @@ fn main() {
}
#[test]
fn part1() {
Day::test(aoc::Part::ONE);
}
#[test]
fn part2() {
Day::test(aoc::Part::TWO);
Day::test(aoc::Part::ONE, "test-1", 0);
}

View File

@ -10,7 +10,7 @@ pub trait Solver {
fn part1(input: &str) -> u32;
fn part2(input: &str) -> u32;
fn test(part: Part) {
fn test(part: Part, name: &str, result: u32) {
// Select the right function
let fun = match part {
Part::ONE => Self::part1,
@ -18,31 +18,14 @@ pub trait Solver {
};
// 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");
let input = fs::read_to_string(format!("input/{}/{name}", Self::day())).expect("Test file does not exist!");
// 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!");
let input = fs::read_to_string(format!("input/{}/input", Self::day())).expect("Input file does not exist!");
println!("Part 1: {}", Self::part1(&input));
println!("Part 2: {}", Self::part2(&input));
}