diff --git a/day04/Cargo.toml b/day04/Cargo.toml index 1c53ba2..73438c5 100644 --- a/day04/Cargo.toml +++ b/day04/Cargo.toml @@ -11,4 +11,8 @@ publish.workspace = true [[bin]] name = "day04_part1" +[[bin]] +name = "day04_part2" + [dependencies] +regex = "1.10.2" diff --git a/day04/src/bin/day04_part2.rs b/day04/src/bin/day04_part2.rs new file mode 100644 index 0000000..5eda267 --- /dev/null +++ b/day04/src/bin/day04_part2.rs @@ -0,0 +1,69 @@ +use std::collections::HashSet; +use std::str::FromStr; + +struct Card { + winning_numbers: HashSet, + present_numbers: HashSet +} + +fn parse_input(input: &str) -> Vec { + + let mut cards: Vec = Vec::new(); + for line in input.lines() { + let splits: Vec<&str> = line.split_terminator(&[':', '|'][..]).collect(); + if splits.len() != 3 { + eprintln!("Line: {} is not formatted correctly!", line); + continue + } + + let winning_numbers: HashSet = (splits[1]).split_whitespace().map(|x| u64::from_str(x).unwrap()).collect(); + let present_numbers: HashSet = (splits[2]).split_whitespace().map(|x| u64::from_str(x).unwrap()).collect(); + cards.push(Card { winning_numbers, present_numbers}); + } + cards +} + + +fn calculate_card_value(card: &Card) -> usize { + card.present_numbers.intersection(&card.winning_numbers).count() +} + +fn calculate_result(cards: Vec) -> u64 { + let no_of_cards: usize = cards.len(); + let card_wins: Vec = cards.iter().map(|card| calculate_card_value(card) as u64).collect(); + let mut card_count: Vec = vec![1; no_of_cards]; + + let mut result: u64 = 0; + + for idx in 0..no_of_cards { + let wins = card_wins[idx]; + let count = card_count[idx]; + + for idy in & mut card_count [idx+1..idx+ (wins as usize) + 1] { + *idy += count; + } + result += count; + } + result +} + +fn main() { + + let input = include_str!("../../resources/input_01.txt"); + let cards = parse_input(input); + let res = calculate_result(cards); + println!("{}", res); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_input01() { + let input = include_str!("../../resources/test_input_01.txt"); + let cards = parse_input(input); + let res = calculate_result(cards); + assert_eq!(res, 30); + } +}