From 50c8ac9859c86b4ab42b66ee8d7cf08a79a26733 Mon Sep 17 00:00:00 2001 From: Leandro Lisboa Penz Date: Sat, 23 Dec 2023 07:30:00 +0000 Subject: [PATCH] Day 23 parsing --- Cargo.lock | 10 +++++ Cargo.toml | 1 + day23/Cargo.toml | 10 +++++ day23/src/bin/day23a.rs | 20 ++++++++++ day23/src/lib.rs | 87 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 day23/Cargo.toml create mode 100644 day23/src/bin/day23a.rs create mode 100644 day23/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 58a574e..de96f0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -371,6 +371,16 @@ dependencies = [ "rayon", ] +[[package]] +name = "day23" +version = "0.1.0" +dependencies = [ + "aoc", + "color-eyre", + "nom", + "sqrid 0.0.24", +] + [[package]] name = "either" version = "1.9.0" diff --git a/Cargo.toml b/Cargo.toml index caa20a7..1a1d49d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,5 +25,6 @@ members = [ "day20", "day21", "day22", + "day23", ] diff --git a/day23/Cargo.toml b/day23/Cargo.toml new file mode 100644 index 0000000..cb14e60 --- /dev/null +++ b/day23/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "day23" +version = "0.1.0" +edition = "2021" + +[dependencies] +aoc = { path = "../aoc" } +color-eyre = "0.6.2" +nom = "7.1.3" +sqrid = "0.0.24" diff --git a/day23/src/bin/day23a.rs b/day23/src/bin/day23a.rs new file mode 100644 index 0000000..11e0486 --- /dev/null +++ b/day23/src/bin/day23a.rs @@ -0,0 +1,20 @@ +// Copyright (C) 2023 Leandro Lisboa Penz +// This file is subject to the terms and conditions defined in +// file 'LICENSE', which is part of this source code package. + +use day23::*; + +fn process(bufin: impl BufRead) -> Result { + let input = parser::parse(bufin)?; + Ok(input.len()) +} + +#[test] +fn test() -> Result<()> { + assert_eq!(process(EXAMPLE.as_bytes())?, 23); + Ok(()) +} + +fn main() -> Result<()> { + do_main(|| process(stdin().lock())) +} diff --git a/day23/src/lib.rs b/day23/src/lib.rs new file mode 100644 index 0000000..de01bf2 --- /dev/null +++ b/day23/src/lib.rs @@ -0,0 +1,87 @@ +// Copyright (C) 2023 Leandro Lisboa Penz +// This file is subject to the terms and conditions defined in +// file 'LICENSE', which is part of this source code package. + +pub use aoc::*; + +pub const EXAMPLE: &str = "#.##################### +#.......#########...### +#######.#########.#.### +###.....#.>.>.###.#.### +###v#####.#v#.###.#.### +###.>...#.#.#.....#...# +###v###.#.#.#########.# +###...#.#.#.......#...# +#####.#.#.#######.#.### +#.....#.#.#.......#...# +#.#####.#.#.#########v# +#.#...#...#...###...>.# +#.#.#v#######v###.###v# +#...#.>.#...>.>.#.###.# +#####v#.#.###v#.#.###.# +#.....#...#...#.#.#...# +#.#########.###.#.#.### +#...###...#...#...#.### +###.###.#.###v#####v### +#...#...#.#.>.>.#.>.### +#.###.###.#.###.#.#v### +#.....###...###...#...# +#####################.# +"; + +pub use sqrid::Dir; +pub type Sqrid = sqrid::sqrid_create!(23, 23, false); +pub type Po = sqrid::pos_create!(Sqrid); +pub type Grid = sqrid::grid_create!(Sqrid, Cell); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum Cell { + #[default] + Empty, + Wall, + Slope(Dir), +} + +impl TryFrom for Cell { + type Error = Report; + fn try_from(c: char) -> Result { + match c { + '.' => Ok(Cell::Empty), + '#' => Ok(Cell::Wall), + '^' => Ok(Cell::Slope(Dir::N)), + '>' => Ok(Cell::Slope(Dir::E)), + 'v' => Ok(Cell::Slope(Dir::S)), + '<' => Ok(Cell::Slope(Dir::W)), + other => Err(eyre!("invalid cell {}", other)), + } + } +} + +pub mod parser { + use aoc::parser::*; + + use super::*; + + fn cell(input: &str) -> IResult<&str, Cell> { + let (input, c) = character::one_of(".#^>v<")(input)?; + Ok((input, Cell::try_from(c).unwrap())) + } + + fn line(input: &str) -> IResult<&str, Vec> { + let (input, cells) = multi::many1(cell)(input)?; + let (input, _) = character::newline(input)?; + Ok((input, cells)) + } + + pub fn parse(mut bufin: impl BufRead) -> Result>> { + aoc::parse_with!(multi::many1(line), bufin) + } +} + +#[test] +fn test() -> Result<()> { + let input = parser::parse(EXAMPLE.as_bytes())?; + assert_eq!(input.len(), 23); + assert_eq!(input[0].len(), 23); + Ok(()) +}