Skip to content

Commit

Permalink
Solve part two of day04.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oberacda committed Dec 4, 2023
1 parent 494c9ec commit 68002db
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
4 changes: 4 additions & 0 deletions day04/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ publish.workspace = true
[[bin]]
name = "day04_part1"

[[bin]]
name = "day04_part2"

[dependencies]
regex = "1.10.2"
69 changes: 69 additions & 0 deletions day04/src/bin/day04_part2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::collections::HashSet;
use std::str::FromStr;

struct Card {
winning_numbers: HashSet<u64>,
present_numbers: HashSet<u64>
}

fn parse_input(input: &str) -> Vec<Card> {

let mut cards: Vec<Card> = 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<u64> = (splits[1]).split_whitespace().map(|x| u64::from_str(x).unwrap()).collect();
let present_numbers: HashSet<u64> = (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<Card>) -> u64 {
let no_of_cards: usize = cards.len();
let card_wins: Vec<u64> = cards.iter().map(|card| calculate_card_value(card) as u64).collect();
let mut card_count: Vec<u64> = 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);
}
}

0 comments on commit 68002db

Please sign in to comment.