diff options
author | Justin Wernick <justin@worthe-it.co.za> | 2023-12-22 22:23:01 +0200 |
---|---|---|
committer | Justin Wernick <justin@worthe-it.co.za> | 2023-12-22 22:23:01 +0200 |
commit | a3c39206a35e4ecf867a368108afe7002d8b4d17 (patch) | |
tree | e41d35ed31d6a9d76100ddc43a05ed73755628b1 /2023/src | |
parent | f92617828df7fe9974ba3afc94b6ff338d25bd9b (diff) |
Day 22 parsing
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) +} |