-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,6 @@ members = [ | |
"day20", | ||
"day21", | ||
"day22", | ||
"day23", | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |