Skip to content

Commit

Permalink
Day 22 parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Dec 22, 2023
1 parent 3a01ea0 commit 2bc41f1
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
9 changes: 9 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 @@ -24,5 +24,6 @@ members = [
"day19",
"day20",
"day21",
"day22",
]

9 changes: 9 additions & 0 deletions day22/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 20 additions & 0 deletions day22/src/bin/day22a.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 day22::*;

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())?, 7);
Ok(())
}

fn main() -> Result<()> {
do_main(|| process(stdin().lock()))
}
50 changes: 50 additions & 0 deletions day22/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 = "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<Vec<(P, P)>> {
aoc::parse_with!(multi::many1(line), bufin)
}
}

#[test]
fn test() -> Result<()> {
let input = parser::parse(EXAMPLE.as_bytes())?;
assert_eq!(input.len(), 7);
Ok(())
}

0 comments on commit 2bc41f1

Please sign in to comment.