Skip to content

Commit

Permalink
Day 23 parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Dec 23, 2023
1 parent a1e1bb6 commit 50c8ac9
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ members = [
"day20",
"day21",
"day22",
"day23",
]

10 changes: 10 additions & 0 deletions day23/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 20 additions & 0 deletions day23/src/bin/day23a.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2023 Leandro Lisboa Penz <lpenz@lpenz.org>
// 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<usize> {
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()))
}
87 changes: 87 additions & 0 deletions day23/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright (C) 2023 Leandro Lisboa Penz <lpenz@lpenz.org>
// 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<char> for Cell {
type Error = Report;
fn try_from(c: char) -> Result<Self, Self::Error> {
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<Cell>> {
let (input, cells) = multi::many1(cell)(input)?;
let (input, _) = character::newline(input)?;
Ok((input, cells))
}

pub fn parse(mut bufin: impl BufRead) -> Result<Vec<Vec<Cell>>> {
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(())
}

0 comments on commit 50c8ac9

Please sign in to comment.