|
1 |
| -use std::{fmt::Display, fs::read_to_string}; |
| 1 | +use std::{fmt::Display, fs, path::Path, time::Instant}; |
2 | 2 |
|
3 |
| -use anyhow::{anyhow, Result}; |
| 3 | +use anyhow::{anyhow, Context, Result}; |
4 | 4 | use winnow::{PResult, Parser as _};
|
5 | 5 |
|
6 |
| -use crate::Instant; |
7 |
| - |
8 | 6 | pub mod day01;
|
9 | 7 | pub mod day02;
|
10 | 8 | pub mod day03;
|
@@ -44,26 +42,25 @@ pub trait Day {
|
44 | 42 |
|
45 | 43 | fn part_2(input: &Self::Input) -> Self::Output2;
|
46 | 44 |
|
47 |
| - fn parse_file(fp: &str) -> Result<Self::Input> { |
48 |
| - let input_string = read_to_string(fp)?; |
| 45 | + fn parse_file(path: impl AsRef<Path>) -> Result<Self::Input> { |
| 46 | + let input_string = fs::read_to_string(path).context("reading the input file")?; |
49 | 47 | let input = Self::parser
|
50 | 48 | .parse(&input_string)
|
51 |
| - .map_err(|e| anyhow!(e.to_string()))?; |
| 49 | + .map_err(|e| anyhow!(e.to_string())) |
| 50 | + .context("running the parser")?; |
52 | 51 | Ok(input)
|
53 | 52 | }
|
54 | 53 |
|
55 | 54 | #[allow(clippy::cast_precision_loss)]
|
56 |
| - fn run_day(fp: &str) { |
57 |
| - match Self::parse_file(fp) { |
58 |
| - Err(e) => println!("{e:?}"), |
59 |
| - Ok(input) => { |
60 |
| - let before1 = Instant::now(); |
61 |
| - println!("Part 1: {}", Self::part_1(&input)); |
62 |
| - println!("Part 1 took {:?}", before1.elapsed()); |
63 |
| - let before2 = Instant::now(); |
64 |
| - println!("Part 2: {}", Self::part_2(&input)); |
65 |
| - println!("Part 2 took {:?}", before2.elapsed()); |
66 |
| - } |
67 |
| - } |
| 55 | + fn run_day(path: impl AsRef<Path>) -> Result<()> { |
| 56 | + let input = Self::parse_file(path)?; |
| 57 | + |
| 58 | + let before1 = Instant::now(); |
| 59 | + println!("Part 1: {}", Self::part_1(&input)); |
| 60 | + println!("Part 1 took {:?}", before1.elapsed()); |
| 61 | + let before2 = Instant::now(); |
| 62 | + println!("Part 2: {}", Self::part_2(&input)); |
| 63 | + println!("Part 2 took {:?}", before2.elapsed()); |
| 64 | + Ok(()) |
68 | 65 | }
|
69 | 66 | }
|
0 commit comments