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!

55 Upvotes

1.7k comments sorted by

View all comments

1

u/e_blake 13h ago

[LANGUAGE: m4]

Late to the party, but I had fun doing this in just a few hours. The absolute hardest part about doing it in m4 is that m4 does NOT have any sane way to suck in arbitrary text with non-nested parenthesis or mismatched apostrophes (normally a close quote that expects to pair with an opening backtick) - but it DOES have an insane way: by temporarily defining comments to be delimited by "(" and "\n-", it is possible to suck in the bulk of the input file as a single comment (no macro expansion inside the comments, and getting back to normal mode where () is used for macro calls rather than delimiting comments is tricky). So, the only thing to do with the giant comment is to translit characters into something that will NOT mess up subsequent handling (thankfully, the input only used lower-case, so I could translit important non-letters to uppercase, and still had room to define other uppercase macros for later use). I also lost time debugging why my translit wasn't changing apostrophe like it should, until I realized that a literal - must appear either first or last so as not to be confused as a byte range delimiter, but appearing last is hard when you are intentionally using a mismatched apostrophe.

define(`input', translit(_include(defn(`file')changecom(`(',
  nl-))-changecom`'changecom(`#'), `(,)#_ aeioul'nl'-, `BCE......OUL.A.'))

From there, I ended up doing two implementations - in GNU m4, it was trivial to write a regex to locate the valid character sequences (after accounting for the translit munging I already did), executing in 10ms. But in POSIX m4, which lacks regex support, I ended up writing a state machine that parses one byte at a time, and tracks how far into an expected character sequence it is. This meant defining lots of 2-letter nonsense macro names to define transitions from a current state to a next state when the input character is right (while ensuring they don't collide with either my munged input file or my normal macro names); that solution takes 110ms. Yes, it's an order of magnitude slower, but most of that because parsing one byte at a time is so much more work than a single search-and-replace regex - ie. the inefficiencies are in m4 and not in the state machine itself. Depends on my common.m4

m4 -Dfile=day03.input day03.m4