Skip Navigation

☃️ - 2023 DAY 4 SOLUTIONS -☃️

Day 4: Scratchcards


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

🔓 Unlocked after 8 mins

48

You're viewing a single thread.

48 comments
  • I enjoyed this one. It was a nice simple break after Days 1 and 3; the type of basic puzzle I expect from the first few days of Advent of Code. Pretty simple logic in this one, I don't think I would change too much. I'm sure I'll find a way to clean up how it's written a bit, but I'm happy with this one today.

    https://github.com/capitalpb/advent_of_code_2023/blob/main/src/solvers/day04.rs

    struct Scratchcard {
        winning_numbers: HashSet,
        player_numbers: HashSet,
    }
    
    impl Scratchcard {
        fn from(input: &str) -> Scratchcard {
            let (_, numbers) = input.split_once(':').unwrap();
            let (winning_numbers, player_numbers) = numbers.split_once('|').unwrap();
            let winning_numbers = winning_numbers
                .split_ascii_whitespace()
                .filter_map(|number| number.parse::().ok())
                .collect::>();
            let player_numbers = player_numbers
                .split_ascii_whitespace()
                .filter_map(|number| number.parse::().ok())
                .collect::>();
    
            Scratchcard {
                winning_numbers,
                player_numbers,
            }
        }
    
        fn matches(&self) -> u32 {
            self.winning_numbers
                .intersection(&self.player_numbers)
                .count() as u32
        }
    }
    
    pub struct Day04;
    
    impl Solver for Day04 {
        fn star_one(&self, input: &str) -> String {
            input
                .lines()
                .map(Scratchcard::from)
                .map(|card| {
                    let matches = card.matches();
                    if matches == 0 {
                        0
                    } else {
                        2u32.pow(matches - 1)
                    }
                })
                .sum::()
                .to_string()
        }
    
        fn star_two(&self, input: &str) -> String {
            let cards: Vec = input.lines().map(Scratchcard::from).collect();
            let mut card_counts = vec![1usize; cards.len()];
    
            for card_number in 0..cards.len() {
                let matches = cards[card_number].matches();
    
                if matches == 0 {
                    continue;
                }
    
                for i in 1..=matches {
                    card_counts[card_number + i as usize] += card_counts[card_number];
                }
            }
    
            card_counts.iter().sum::().to_string()
        }
    }
    
You've viewed 48 comments.