Skip to content

Commit f27e48c

Browse files
committed
some tweaks
1 parent 5183989 commit f27e48c

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/day2/chumsky.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,17 @@ fn parse_game(s: &str) -> eyre::Result<Game> {
4646
fn parser() -> impl Parser<char, Game, Error = Simple<char>> {
4747
let head = just("Game").padded();
4848

49-
let id = text::int(10)
50-
.map(|s: String| s.parse::<usize>().unwrap())
51-
.then_ignore(just(':'));
49+
let integer = text::int(10).from_str().unwrapped().padded();
5250

53-
let color = just("red")
54-
.to(Color::Red)
55-
.or(just("green").to(Color::Green))
56-
.or(just("blue").to(Color::Blue));
51+
let id = integer.then_ignore(just(':')).padded();
5752

58-
let color_count = text::int(10)
59-
.map(|s: String| s.parse::<usize>().unwrap())
60-
.padded()
61-
.then(color);
53+
let color = choice((
54+
just("red").to(Color::Red),
55+
just("green").to(Color::Green),
56+
just("blue").to(Color::Blue),
57+
));
58+
59+
let color_count = integer.then(color);
6260

6361
let color_set = color_count.separated_by(just(',')).map(|color_counts| {
6462
let mut set = ColorSet::default();
@@ -76,6 +74,7 @@ fn parser() -> impl Parser<char, Game, Error = Simple<char>> {
7674

7775
head.ignore_then(id)
7876
.then(color_set)
77+
.then_ignore(end())
7978
.map(|(id, draws)| Game { id, draws })
8079
}
8180

0 commit comments

Comments
 (0)