Skip to content

Commit 38dc536

Browse files
committed
feat: add read_file_part() helper
closes #37
1 parent b7b5a58 commit 38dc536

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Individual solutions live in the `./src/bin/` directory as separate binaries. _I
5151

5252
Every [solution](https://github.com/fspoettel/advent-of-code-rust/blob/main/src/template/commands/scaffold.rs#L9-L35) has _tests_ referencing its _example_ file in `./data/examples`. Use these tests to develop and debug your solutions against the example input.
5353

54+
> [!TIP]
55+
> If a day has different example inputs for both parts, you can use the `read_file_part()` helper in your tests instead of `read_file()`. For example, if this applies to day 1, you can create a second example file `01-2.txt` and invoke the helper like `let result = part_two(&advent_of_code::template::read_file_part("examples", DAY, 2));` to read it in `test_part_two`.
56+
5457
> [!TIP]
5558
> when editing a solution, `rust-analyzer` will display buttons for running / debugging unit tests above the unit test blocks.
5659

src/template/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ pub fn read_file(folder: &str, day: Day) -> String {
1919
f.expect("could not open input file")
2020
}
2121

22+
/// Helper function that reads a text file to string, appending a part suffix. E.g. like `01-2.txt`.
23+
#[must_use]
24+
pub fn read_file_part(folder: &str, day: Day, part: u8) -> String {
25+
let cwd = env::current_dir().unwrap();
26+
let filepath = cwd.join("data").join(folder).join(format!("{day}-{part}.txt"));
27+
let f = fs::read_to_string(filepath);
28+
f.expect("could not open input file")
29+
}
30+
2231
/// Creates the constant `DAY` and sets up the input and runner for each part.
2332
#[macro_export]
2433
macro_rules! solution {

0 commit comments

Comments
 (0)