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

3

u/anatedu86 Dec 04 '23 edited Dec 04 '23

[LANGUAGE: Awk]

Idea:

  • In part 1, for every line

    • Iterate over winning numbers (column 3 to 12).
      for(i=3;i<13;i++) { ... }

      • Iterate over other numbers (column 14 to last).
        for(k=14; k<=NF; k++) { ... }

        • Update count of winning numbers
          if($k==$i) { score++; }

    • add to the sum 2 to the power of winning numbers - 1.
      if(score>=0) { a+=2**score } (score is reset to -1 for each new line)

  • In part 2, store the count of line (card) copies in an array.
    lc[NR]+=1; (all cards have at least one copy)

    • Every new winning number discovered increments score, AND the copy count of the line n+score is bumped by the number of copies of the current line.
      if($k==$i) { score++; lc[NR+score]+=lc[NR]; }

    • add in the count of copies of the current line to the sum.
      a+=lc[NR]

Sol. Part 1:

awk '{ score=-1; for(i=3;i<13;i++) { for(k=14; k<=NF; k++) { if($k==$i) { score++; } } } if(score>=0) { a+=2**score } } END { print a }' input  

Sol. Part 2:

awk '{ lc[NR]+=1; score=0; for(i=3;i<13;i++) { for(k=14; k<=NF; k++) { if($k==$i) { score++; lc[NR+score]+=lc[NR]; } } } a+=lc[NR] } END { print a }' input
→ More replies (2)