diff --git a/Cargo.lock b/Cargo.lock index 507ab90..23993f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -361,6 +361,15 @@ dependencies = [ "sqrid 0.0.24", ] +[[package]] +name = "day22" +version = "0.1.0" +dependencies = [ + "aoc", + "color-eyre", + "nom", +] + [[package]] name = "either" version = "1.9.0" diff --git a/Cargo.toml b/Cargo.toml index 0c32862..caa20a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,5 +24,6 @@ members = [ "day19", "day20", "day21", + "day22", ] diff --git a/day22/Cargo.toml b/day22/Cargo.toml new file mode 100644 index 0000000..0bfcfb6 --- /dev/null +++ b/day22/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day22" +version = "0.1.0" +edition = "2021" + +[dependencies] +aoc = { path = "../aoc" } +color-eyre = "0.6.2" +nom = "7.1.3" diff --git a/day22/src/bin/day22a.rs b/day22/src/bin/day22a.rs new file mode 100644 index 0000000..a227681 --- /dev/null +++ b/day22/src/bin/day22a.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 day22::*; + +fn process(bufin: impl BufRead) -> Result { + let input = parser::parse(bufin)?; + Ok(input.len()) +} + +#[test] +fn test() -> Result<()> { + assert_eq!(process(EXAMPLE.as_bytes())?, 7); + Ok(()) +} + +fn main() -> Result<()> { + do_main(|| process(stdin().lock())) +} diff --git a/day22/src/lib.rs b/day22/src/lib.rs new file mode 100644 index 0000000..7bef73e --- /dev/null +++ b/day22/src/lib.rs @@ -0,0 +1,50 @@ +// 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 = "1,0,1~1,2,1 +0,0,2~2,0,2 +0,2,3~2,2,3 +0,0,4~0,2,4 +2,0,5~2,2,5 +0,1,6~2,1,6 +1,1,8~1,1,9 +"; + +pub type P = (i64, i64, i64); + +pub mod parser { + use aoc::parser::*; + + use super::*; + + fn point(input: &str) -> IResult<&str, P> { + let (input, x) = character::i64(input)?; + let (input, _) = tag(",")(input)?; + let (input, y) = character::i64(input)?; + let (input, _) = tag(",")(input)?; + let (input, z) = character::i64(input)?; + Ok((input, (x, y, z))) + } + + fn line(input: &str) -> IResult<&str, (P, P)> { + let (input, p1) = point(input)?; + let (input, _) = tag("~")(input)?; + let (input, p2) = point(input)?; + let (input, _) = character::newline(input)?; + Ok((input, (p1, p2))) + } + + 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(), 7); + Ok(()) +}