diff --git a/2021/day4/src/bingo.rs b/2021/day4/src/bingo.rs index dd63140..d8ab36d 100644 --- a/2021/day4/src/bingo.rs +++ b/2021/day4/src/bingo.rs @@ -3,7 +3,40 @@ pub struct BingoCard { grid: Vec>, } +impl BingoCard { + pub fn from_input(input: &[&str]) -> Self { + let grid = input + .iter() + .map(|line| { + line.replace(" ", " ") + .trim() + .split(' ') + .map(|n| n.parse::().unwrap()) + .map(|n| (n, false)) + .collect::>() + }) + .collect::>>(); + + BingoCard { grid } + } +} + pub fn parse_bingo_input(input: &str) -> (&str, Vec) { + 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::>(); + + println!("x.len = {:?}", x.len()); + println!("{:?}", x); + ("", vec![]) } @@ -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 = "";