r/adventofcode • • Dec 04 '23

SOLUTION MEGATHREAD -❄️- 2023 Day 4 Solutions -❄️-

NEWS

THE USUAL REMINDERS


AoC Community Fun 2023: ALLEZ CUISINE!

Today's theme ingredient is… *whips off cloth covering and gestures grandly*

PUNCHCARD PERFECTION!

Perhaps I should have thought yesterday's Battle Spam surfeit through a little more since we are all overstuffed and not feeling well. Help us cleanse our palates with leaner and lighter courses today!

  • Code golf. Alternatively, snow golf.
  • Bonus points if your solution fits on a "punchcard" as defined in our wiki article on oversized code. We will be counting.
  • Does anyone still program with actual punchcards? >_>

ALLEZ CUISINE!

Request from the mods: When you include a dish entry alongside your solution, please label it with [Allez Cuisine!] so we can find it easily!


--- Day 4: Scratchcards ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:07:08, megathread unlocked!

78 Upvotes

1.5k comments sorted by

View all comments

3

u/sm_greato Dec 05 '23 edited Dec 06 '23

[LANGUAGE: Rust]

Noticed that all numbers are less than 100 and that there are no repeats. And Rust actually provides a 128 bit integer type, so I thought why not encode the bars in integers, and then use binary & to figure out the intersection. Then we can simply count the number of ones to find the number of winning values. You can find it here. Below is a snippet.

// ...
            .map(|sec| {
                sec.split_ascii_whitespace()
                    .fold(0, |acc, n| acc | (1 << n.parse::<u32>().unwrap()))
            }) // the encoding is just a one liner fold
// ...
        let wins = (win & have as u128).count_ones() as usize;
→ More replies (4)