diff options
Diffstat (limited to '2023/src')
-rw-r--r-- | 2023/src/bin/day_22.rs | 44 |
1 files changed, 37 insertions, 7 deletions
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<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_22.txt")?; + let parsed = BrickPile::parser(&input).unwrap().1; dbg!(&parsed); Ok(()) } #[derive(Debug)] -struct Example; +struct BrickPile(Vec<Brick>); -impl Example { - fn parser(_input: &str) -> IResult<&str, Self> { - todo!() +#[derive(Debug)] +struct Brick { + a: Point3<u32>, + b: Point3<u32>, +} + +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<u32>> { + map( + tuple((u32, tag(","), u32, tag(","), u32)), + |(x, _, y, _, z)| Point3::new(x, y, z), + )(input) +} |