From a3c39206a35e4ecf867a368108afe7002d8b4d17 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Fri, 22 Dec 2023 22:23:01 +0200 Subject: Day 22 parsing --- 2023/src/bin/day_22.rs | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to '2023/src/bin') diff --git a/2023/src/bin/day_22.rs b/2023/src/bin/day_22.rs index b3a610b..3619b83 100644 --- a/2023/src/bin/day_22.rs +++ b/2023/src/bin/day_22.rs @@ -1,19 +1,49 @@ -use nom::IResult; +use nalgebra::Point3; +use nom::{ + bytes::complete::tag, + character::complete::{line_ending, u32}, + combinator::map, + multi::separated_list1, + sequence::{separated_pair, tuple}, + 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_22.txt")?; + let parsed = BrickPile::parser(&input).unwrap().1; dbg!(&parsed); Ok(()) } #[derive(Debug)] -struct Example; +struct BrickPile(Vec); -impl Example { - fn parser(_input: &str) -> IResult<&str, Self> { - todo!() +#[derive(Debug)] +struct Brick { + a: Point3, + b: Point3, +} + +impl BrickPile { + fn parser(input: &str) -> IResult<&str, Self> { + map(separated_list1(line_ending, Brick::parser), BrickPile)(input) + } +} + +impl Brick { + fn parser(input: &str) -> IResult<&str, Self> { + map( + separated_pair(point_parser, tag("~"), point_parser), + |(a, b)| Brick { a, b }, + )(input) } } + +fn point_parser(input: &str) -> IResult<&str, Point3> { + map( + tuple((u32, tag(","), u32, tag(","), u32)), + |(x, _, y, _, z)| Point3::new(x, y, z), + )(input) +} -- cgit v1.2.3