diff options
| author | 2024-12-03 10:01:25 +0100 | |
|---|---|---|
| committer | 2024-12-03 10:01:25 +0100 | |
| commit | 120fe4c9e696ec3289736843f54657fdf2b8247d (patch) | |
| tree | b3b5ae08112ce9f597de00504eb87bba2e669379 /aoc/src | |
| parent | f40672059393040e9403ba3c1ec442d0823d1622 (diff) | |
| download | 2024-120fe4c9e696ec3289736843f54657fdf2b8247d.tar.gz 2024-120fe4c9e696ec3289736843f54657fdf2b8247d.tar.bz2 2024-120fe4c9e696ec3289736843f54657fdf2b8247d.zip | |
Day3
Diffstat (limited to 'aoc/src')
| -rw-r--r-- | aoc/src/lib.rs | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/aoc/src/lib.rs b/aoc/src/lib.rs index c4318e1..dcca651 100644 --- a/aoc/src/lib.rs +++ b/aoc/src/lib.rs | |||
| @@ -2,6 +2,7 @@ use clap::Parser; | |||
| 2 | use std::fs::File; | 2 | use std::fs::File; |
| 3 | use std::io; | 3 | use std::io; |
| 4 | use std::io::{BufRead, BufReader, Read}; | 4 | use std::io::{BufRead, BufReader, Read}; |
| 5 | use std::panic::catch_unwind; | ||
| 5 | use std::time::Instant; | 6 | use std::time::Instant; |
| 6 | 7 | ||
| 7 | #[derive(Parser, Debug)] | 8 | #[derive(Parser, Debug)] |
| @@ -16,6 +17,14 @@ struct Args { | |||
| 16 | 17 | ||
| 17 | pub trait Day { | 18 | pub trait Day { |
| 18 | fn example_input(&self) -> &'static str; | 19 | fn example_input(&self) -> &'static str; |
| 20 | fn example_input_part_1(&self) -> &'static str { | ||
| 21 | self.example_input() | ||
| 22 | } | ||
| 23 | |||
| 24 | fn example_input_part_2(&self) -> &'static str { | ||
| 25 | self.example_input() | ||
| 26 | } | ||
| 27 | |||
| 19 | fn example_result_part_1(&self) -> &'static str; | 28 | fn example_result_part_1(&self) -> &'static str; |
| 20 | fn example_result_part_2(&self) -> &'static str; | 29 | fn example_result_part_2(&self) -> &'static str; |
| 21 | 30 | ||
| @@ -61,19 +70,26 @@ pub fn main(day: &dyn Day) { | |||
| 61 | println!("Time to run: {}s", now.elapsed().as_secs_f64()); | 70 | println!("Time to run: {}s", now.elapsed().as_secs_f64()); |
| 62 | } | 71 | } |
| 63 | 72 | ||
| 64 | pub fn test_day<T: Day>(day: &T) { | 73 | pub fn test_day<T: Day + std::panic::RefUnwindSafe>(day: &T) { |
| 65 | use std::io::Cursor; | 74 | use std::io::Cursor; |
| 66 | use std::io::{BufReader, Read}; | 75 | use std::io::{BufReader, Read}; |
| 67 | 76 | ||
| 68 | let example_input = day.example_input(); | ||
| 69 | |||
| 70 | // Test Part 1 | 77 | // Test Part 1 |
| 71 | let input = BufReader::new(Box::new(Cursor::new(example_input)) as Box<dyn Read>); | 78 | let output = catch_unwind(|| { |
| 72 | let output = day.part_1(input); | 79 | let input = BufReader::new(Box::new(Cursor::new(day.example_input_part_1())) as Box<dyn Read>); |
| 73 | assert_eq!(output.trim(), day.example_result_part_1(), "Part 1 failed"); | 80 | day.part_1(input) |
| 81 | }); | ||
| 82 | if output.is_ok() { | ||
| 83 | assert_eq!(output.unwrap().trim(), day.example_result_part_1(), "Part 1 failed"); | ||
| 84 | } | ||
| 74 | 85 | ||
| 75 | // Test Part 2 | 86 | // Test Part 2 |
| 76 | let input = BufReader::new(Box::new(Cursor::new(example_input)) as Box<dyn Read>); | 87 | let output = catch_unwind(|| { |
| 77 | let output = day.part_2(input); | 88 | let input = BufReader::new(Box::new(Cursor::new(day.example_input_part_2())) as Box<dyn Read>); |
| 78 | assert_eq!(output.trim(), day.example_result_part_2(), "Part 2 failed"); | 89 | day.part_2(input) |
| 90 | }); | ||
| 91 | |||
| 92 | if output.is_ok() { | ||
| 93 | assert_eq!(output.unwrap().trim(), day.example_result_part_2(), "Part 2 failed"); | ||
| 94 | } | ||
| 79 | } \ No newline at end of file | 95 | } \ No newline at end of file |
