Use proper Nom number parsing functions

This was previously a documentation/discoverability issue, rather than a conscious choice to avoid these.
This commit is contained in:
George Thomas 2026-01-07 13:41:03 +00:00
parent b80743c840
commit bf1cac4e94
2 changed files with 8 additions and 19 deletions

View File

@ -2,8 +2,8 @@ use crate::puzzle::Puzzle;
use nom::{
Parser,
branch::alt,
character::complete::{char, digit1, newline},
combinator::{eof, map_res, value},
character::complete::{char, i32, newline},
combinator::{eof, value},
error::Error,
multi::separated_list1,
sequence::{pair, terminated},
@ -12,7 +12,7 @@ use nom::{
pub const PUZZLE: Puzzle<Vec<(Direction, i32)>, 2> = Puzzle {
number: 1,
parser: |input| {
terminated(
terminated::<_, _, Error<&str>, _, _>(
terminated(
separated_list1(
newline,
@ -21,7 +21,7 @@ pub const PUZZLE: Puzzle<Vec<(Direction, i32)>, 2> = Puzzle {
value(Direction::L, char('L')),
value(Direction::R, char('R')),
)),
parse_int(),
i32,
),
),
newline,
@ -83,7 +83,3 @@ fn step(i: i32, d: Direction, p: i32) -> (i32, i32) {
};
(p1.div_euclid(100), p1.rem_euclid(100))
}
fn parse_int<'a>() -> impl Parser<&'a str, Output = i32, Error = Error<&'a str>> {
map_res(digit1, |s: &str| s.parse::<i32>())
}

View File

@ -1,8 +1,8 @@
use crate::puzzle::Puzzle;
use nom::{
Parser,
character::complete::{char, digit1, newline},
combinator::{eof, map_res},
character::complete::{char, newline, usize},
combinator::eof,
error::Error,
multi::separated_list1,
sequence::{separated_pair, terminated},
@ -11,12 +11,9 @@ use nom::{
pub const PUZZLE: Puzzle<Vec<(usize, usize)>, 2> = Puzzle {
number: 2,
parser: |input| {
terminated(
terminated::<_, _, Error<&str>, _, _>(
terminated(
separated_list1(
char(','),
separated_pair(parse_int(), char('-'), parse_int()),
),
separated_list1(char(','), separated_pair(usize, char('-'), usize)),
newline,
),
eof,
@ -70,7 +67,3 @@ fn equal_chunks(n: &String, i: usize) -> bool {
Some(x) => chunks.all(|y| y == x),
}
}
fn parse_int<'a>() -> impl Parser<&'a str, Output = usize, Error = Error<&'a str>> {
map_res(digit1, |s: &str| s.parse::<usize>())
}