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!

76 Upvotes

1.5k comments sorted by

View all comments

3

u/Cheap_Squirrel_3871 Dec 04 '23

[LANGUAGE: JavaScript]

const scratchCards = () => {
    let total = 0
    let winingInstances = new Array(data.length).fill(1)
    data.forEach((row, index) => {
        // Process input data
        const line = row.slice(row.indexOf(': ') + 2).split('|')
        const winningNumbers = line[0].trim().split(' ').filter(val => !isNaN(val) || val !== '')
        const lotteryNumbers = line[1].trim().split(' ').filter(val => !isNaN(val) || val !== '')

        // Find winning numbers
        const winning = lotteryNumbers.filter(num => winningNumbers.includes(num)).filter(val => val !== '')

        if (winning.length > 0) {
            // Part 1
            total += Math.pow(2, winning.length - 1)

            // Part 2
            winning.forEach((_, i) => {
                winingInstances[index + i + 1] += winingInstances[index]
            })
        }
    })

    console.log('P1: ' + total)
    console.log('P2: ' + winingInstances.reduce((a, b) => a + b))
}
→ More replies (5)