advent-of-code-rust icon indicating copy to clipboard operation
advent-of-code-rust copied to clipboard

RFC: Separate parsing from solutions?

Open fspoettel opened this issue 1 year ago โ€ข 1 comments

Implementation

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)

fspoettel avatar Dec 03 '22 11:12 fspoettel

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?

aranasaurus avatar Dec 04 '22 06:12 aranasaurus