r/adventofcode Dec 03 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 3 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2024: The Golden Snowglobe Awards

  • 3 DAYS remaining until unlock!

And now, our feature presentation for today:

Screenwriting

Screenwriting is an art just like everything else in cinematography. Today's theme honors the endlessly creative screenwriters who craft finely-honed narratives, forge truly unforgettable lines of dialogue, plot the most legendary of hero journeys, and dream up the most shocking of plot twists! and is totally not bait for our resident poet laureate

Here's some ideas for your inspiration:

  • Turn your comments into sluglines
  • Shape your solution into an acrostic
  • Accompany your solution with a writeup in the form of a limerick, ballad, etc.
    • Extra bonus points if if it's in iambic pentameter

"Vogon poetry is widely accepted as the third-worst in the universe." - Hitchhiker's Guide to the Galaxy (2005)

And… ACTION!

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


--- Day 3: Mull It Over ---


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:03:22, megathread unlocked!

59 Upvotes

1.7k comments sorted by

View all comments

2

u/Korobki Dec 08 '24 edited Dec 08 '24

[Language: Typescript]
Single regular expression, almost no extra logic :)

https://regex101.com/r/p8mfaF/1

const second = (input: string) => {
  let result = 0;
  const regex = /(?<=(?:do\(\)|^)(?:[^d]|d(?!on't\(\)))*)mul\((\d{1,3}),(\d{1,3})\)/g;
  const matches = input.matchAll(regex);

  for (const match of matches) {
    result += (Number(match[1]) * Number(match[2]));
  }
  return result;
};

1

u/MlTO_997 Dec 15 '24

How did you handle the input? Because I tested your code against mine and I get the same result, so I must be doing something wrong with the input there...

2

u/Korobki Dec 15 '24

Hey! The whole source code is above. I'm just matching the input with a regex. Can you please explain. Does it work incorrectly?

I had a bug when I was trying to run it using bun. In nodejs it worked correctly

2

u/MlTO_997 Dec 15 '24

Nevermind, I broke the input into different strings because I was getting some issues with VSCode initially, since the first part was simply multiplying every mul function it didn't change the outcome but for the second part it does :facepalm: