aoc-rs-2021/src/day_06/parsing.rs
Jan Christian Grünhage b02d56254d implement day 6
2021-12-07 10:43:19 +01:00

36 lines
816 B
Rust

use super::model::{Fish, School};
use crate::parsing::parse_number;
use yap::Tokens;
pub fn parse_fish(tokens: &mut impl Tokens<Item = char>) -> Option<Fish> {
parse_number(tokens).map(|num| Fish { timer: num })
}
pub fn parse_school_of_fish(tokens: &mut impl Tokens<Item = char>) -> School {
School {
fishes: tokens.sep_by(|t| parse_fish(t), |t| t.token(',')).collect(),
}
}
#[cfg(test)]
mod test {
use super::super::model::Fish;
use yap::IntoTokens;
#[test]
fn parse_fish() {
assert_eq!(
Some(Fish { timer: 1 }),
super::parse_fish(&mut "1".into_tokens())
);
}
#[test]
fn parse_school() {
assert_eq!(
5,
super::parse_school_of_fish(&mut "3,4,3,1,2".into_tokens()).size()
);
}
}