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:
parent
b80743c840
commit
bf1cac4e94
@ -2,8 +2,8 @@ use crate::puzzle::Puzzle;
|
|||||||
use nom::{
|
use nom::{
|
||||||
Parser,
|
Parser,
|
||||||
branch::alt,
|
branch::alt,
|
||||||
character::complete::{char, digit1, newline},
|
character::complete::{char, i32, newline},
|
||||||
combinator::{eof, map_res, value},
|
combinator::{eof, value},
|
||||||
error::Error,
|
error::Error,
|
||||||
multi::separated_list1,
|
multi::separated_list1,
|
||||||
sequence::{pair, terminated},
|
sequence::{pair, terminated},
|
||||||
@ -12,7 +12,7 @@ use nom::{
|
|||||||
pub const PUZZLE: Puzzle<Vec<(Direction, i32)>, 2> = Puzzle {
|
pub const PUZZLE: Puzzle<Vec<(Direction, i32)>, 2> = Puzzle {
|
||||||
number: 1,
|
number: 1,
|
||||||
parser: |input| {
|
parser: |input| {
|
||||||
terminated(
|
terminated::<_, _, Error<&str>, _, _>(
|
||||||
terminated(
|
terminated(
|
||||||
separated_list1(
|
separated_list1(
|
||||||
newline,
|
newline,
|
||||||
@ -21,7 +21,7 @@ pub const PUZZLE: Puzzle<Vec<(Direction, i32)>, 2> = Puzzle {
|
|||||||
value(Direction::L, char('L')),
|
value(Direction::L, char('L')),
|
||||||
value(Direction::R, char('R')),
|
value(Direction::R, char('R')),
|
||||||
)),
|
)),
|
||||||
parse_int(),
|
i32,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
newline,
|
newline,
|
||||||
@ -83,7 +83,3 @@ fn step(i: i32, d: Direction, p: i32) -> (i32, i32) {
|
|||||||
};
|
};
|
||||||
(p1.div_euclid(100), p1.rem_euclid(100))
|
(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>())
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
use crate::puzzle::Puzzle;
|
use crate::puzzle::Puzzle;
|
||||||
use nom::{
|
use nom::{
|
||||||
Parser,
|
Parser,
|
||||||
character::complete::{char, digit1, newline},
|
character::complete::{char, newline, usize},
|
||||||
combinator::{eof, map_res},
|
combinator::eof,
|
||||||
error::Error,
|
error::Error,
|
||||||
multi::separated_list1,
|
multi::separated_list1,
|
||||||
sequence::{separated_pair, terminated},
|
sequence::{separated_pair, terminated},
|
||||||
@ -11,12 +11,9 @@ use nom::{
|
|||||||
pub const PUZZLE: Puzzle<Vec<(usize, usize)>, 2> = Puzzle {
|
pub const PUZZLE: Puzzle<Vec<(usize, usize)>, 2> = Puzzle {
|
||||||
number: 2,
|
number: 2,
|
||||||
parser: |input| {
|
parser: |input| {
|
||||||
terminated(
|
terminated::<_, _, Error<&str>, _, _>(
|
||||||
terminated(
|
terminated(
|
||||||
separated_list1(
|
separated_list1(char(','), separated_pair(usize, char('-'), usize)),
|
||||||
char(','),
|
|
||||||
separated_pair(parse_int(), char('-'), parse_int()),
|
|
||||||
),
|
|
||||||
newline,
|
newline,
|
||||||
),
|
),
|
||||||
eof,
|
eof,
|
||||||
@ -70,7 +67,3 @@ fn equal_chunks(n: &String, i: usize) -> bool {
|
|||||||
Some(x) => chunks.all(|y| y == x),
|
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>())
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user