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/Valletta6789 Dec 04 '23 edited Dec 04 '23

[LANGUAGE: Python]

Here is mine approach:

def get_num_set(input_list):
    return set(map(int, input_list.split()))


def sum_values(n):
    return 2 ** (n - 1) if n > 0 else 0

for line in lines:
    _, win_list, my_list = re.split(r': | \| ', line)
    win_list = get_num_set(win_list)
    my_list = get_num_set(my_list)
    total += sum_values(len(win_list & my_list))

For part2, using extra dict to store num of copies:

copies_num = len(win_list & my_list)
    for i in range(card_num + 1, card_num + copies_num + 1):
        copies_dict[i] = copies_dict.get(i, 0) + 1 + copies_dict.get(card_num, 0)

return sum(v for v in copies_dict.values()) + max_card_num

https://github.com/Aigul9/AdventOfCode/blob/master/year2023/Day_04/main.py

1

u/AutoModerator Dec 04 '23

AutoModerator did not detect the required [LANGUAGE: xyz] string literal at the beginning of your solution submission.

Please edit your comment to state your programming language.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.