advent-of-code-rust
advent-of-code-rust copied to clipboard
RFC: Separate parsing from solutions?
Currently experimenting with a separately timed parser util on my solution repo, might add to template after this year concludes.
Advantages:
- parser code is not timed twice.
- parsing set up in template.
- can use macro to generate
main()
.
Disadvantages:
- input has to be
Clone
. - replacing the macro (if needed for some reason) might not be clear for rust beginners (which this is aimed at).
type Input<'a> = Vec<&'a str>;
fn parse(input: &str) -> Input {
input.lines().collect()
}
pub fn part_one(input: Input) -> Option<u32> {
None
}
pub fn part_two(input: Input) -> Option<u32> {
None
}
advent_of_code::main!(4);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let input = advent_of_code::read_file("examples", 4);
assert_eq!(part_one(parse(&input)), None);
}
#[test]
fn test_part_two() {
let input = advent_of_code::read_file("examples", 4);
assert_eq!(part_two(parse(&input)), None);
}
}
Example output:
๐ Parser ๐
(elapsed: 24.96ยตs)
๐ Part 1 ๐
13005 (elapsed: 6.00ns)
๐ Part 2 ๐
11373 (elapsed: 831.00ns)
I like this idea. I think the disadvantage you listed might be able to be addressed, at least partially by adding something to the README. Step by step example how to change it for one, and then possibly a link to some documentation about the syntax and how it works?