diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2023-12-23 13:17:53 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2023-12-23 13:17:53 +0200 |
commit | 355f718208976c5db13e91e48b8ca97a07bf5a94 (patch) | |
tree | 8e5aac78ed8c5118c3a895941bebbcd5cb4ad8b9 /2023/src/bin | |
parent | a00b89b6358646f6fc48aa285b500697840a5979 (diff) |
Day 23 parsing
Diffstat (limited to '2023/src/bin')
-rw-r--r-- | 2023/src/bin/day_23.rs | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/2023/src/bin/day_23.rs b/2023/src/bin/day_23.rs index b3a610b..3a2bd48 100644 --- a/2023/src/bin/day_23.rs +++ b/2023/src/bin/day_23.rs @@ -1,19 +1,51 @@ -use nom::IResult; +use nom::{ + branch::alt, + character::complete::{char, line_ending}, + combinator::{map, value}, + multi::{many1, separated_list1}, + IResult, +}; use std::fs; fn main() -> Result<(), Box<dyn std::error::Error>> { - let input = fs::read_to_string("inputs/day_2.txt")?; - let parsed = Example::parser(&input).unwrap().1; + let input = fs::read_to_string("inputs/day_23.txt")?; + let parsed = ForestMap::parser(&input).unwrap().1; dbg!(&parsed); Ok(()) } #[derive(Debug)] -struct Example; +struct ForestMap(Vec<Vec<ForestTile>>); -impl Example { - fn parser(_input: &str) -> IResult<&str, Self> { - todo!() +#[derive(Debug, Clone)] +enum ForestTile { + Wall, + Open, + SlopeUp, + SlopeDown, + SlopeLeft, + SlopeRight, +} + +impl ForestMap { + fn parser(input: &str) -> IResult<&str, Self> { + map( + separated_list1(line_ending, many1(ForestTile::parser)), + ForestMap, + )(input) + } +} + +impl ForestTile { + fn parser(input: &str) -> IResult<&str, Self> { + alt(( + value(ForestTile::Wall, char('#')), + value(ForestTile::Open, char('.')), + value(ForestTile::SlopeUp, char('^')), + value(ForestTile::SlopeDown, char('v')), + value(ForestTile::SlopeLeft, char('<')), + value(ForestTile::SlopeRight, char('>')), + ))(input) } } |