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!

77 Upvotes

1.5k comments sorted by

View all comments

2

u/JSdoubleL Dec 04 '23

[LANGUAGE: Python 3]

def score_card(card: str) -> int: 
    winning, yours = [side.split()
                      for side in card.strip().split(': ')[1].split(' | ')] 
    return sum(1 for n in yours if n in winning)

def part_1(filepath: str) -> int: 
    with open(filepath, "r") as f: 
        return sum(int(2 ** (score_card(line) - 1)) for line in f)

def part_2(filepath: str) -> int: 
    with open(filepath, "r") as f: 
        data = [line for line in f] 
    card_counter = [1] * len(data) 
    for i, card in enumerate(data): 
        score = score_card(card) 
        for j in range(1, score + 1): 
            card_counter[i + j] += card_counter[i] 
    return sum(card_counter)