Skip to content

Commit

Permalink
Implement parseing bingo card
Browse files Browse the repository at this point in the history
  • Loading branch information
tjheslin1 committed Dec 4, 2021
1 parent 9c1131d commit 70b1a52
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions 2021/day4/src/bingo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,40 @@ pub struct BingoCard {
grid: Vec<Vec<(u32, bool)>>,
}

impl BingoCard {
pub fn from_input(input: &[&str]) -> Self {
let grid = input
.iter()
.map(|line| {
line.replace(" ", " ")
.trim()
.split(' ')
.map(|n| n.parse::<u32>().unwrap())
.map(|n| (n, false))
.collect::<Vec<(u32, bool)>>()
})
.collect::<Vec<Vec<(u32, bool)>>>();

BingoCard { grid }
}
}

pub fn parse_bingo_input(input: &str) -> (&str, Vec<BingoCard>) {
let lines: Vec<&str> = input.split('\n').collect();

let (bingo_numbers, cards) = match lines.split_first() {
Some((first, rest)) => (first, rest),
_ => (&"", &[] as &[&str]),
};

let x = cards
.split(|line| line.is_empty())
.filter(|line| line.is_empty() == false)
.collect::<Vec<&[&str]>>();

println!("x.len = {:?}", x.len());
println!("{:?}", x);

("", vec![])
}

Expand All @@ -12,6 +45,43 @@ mod tests {

use crate::bingo::*;

#[test]
fn construct_empty_bingo_card_from_input() {
let input = vec![];

let actual_bingo_card = BingoCard::from_input(&input);

let expected_bingo_card = BingoCard { grid: vec![] };

assert_eq!(actual_bingo_card, expected_bingo_card);
}

#[test]
fn construct_bingo_card_from_input() {
let input = vec![
"22 13 17 11 0",
" 8 2 23 4 24",
"21 9 14 16 7",
" 6 10 3 18 5",
" 1 12 20 15 19",
];

let actual_bingo_card = BingoCard::from_input(&input);

#[rustfmt::skip]
let expected_bingo_card = BingoCard {
grid: vec![
vec![(22, false),(13, false),(17, false),(11, false),(0, false),],
vec![(8, false),(2, false),(23, false),(4, false),(24, false),],
vec![(21, false),(9, false),(14, false),(16, false),(7, false),],
vec![(6, false),(10, false),(3, false),(18, false),(5, false),],
vec![(1, false),(12, false),(20, false),(15, false),(19, false),],
],
};

assert_eq!(actual_bingo_card, expected_bingo_card);
}

#[test]
fn parse_empty_bingo_input() {
let input = "";
Expand Down

0 comments on commit 70b1a52

Please sign in to comment.