From 355f718208976c5db13e91e48b8ca97a07bf5a94 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Sat, 23 Dec 2023 13:17:53 +0200 Subject: Day 23 parsing --- 2023/src/bin/day_23.rs | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) (limited to '2023/src/bin/day_23.rs') 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> { - 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>); -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) } } -- cgit v1.2.3