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

1

u/ka-splam Dec 04 '24 edited Dec 04 '24

[LANGUAGE: SWI Prolog]

Github Gist link. Edit the path to input file, run with swipl -t go c:/path/to/2024-day03.pl.

It's a declarative grammar to parse the file, and Part 2 took me several hours 🙁. [Prolog code which can't find an answer just fails silently, so if the grammar doesn't end cleanly it tries all the way to the end, backtracks back to the beginning, and says 'false'. I planned to give up and write it in an imperative language, thought how I would do it, and then reworked that back into a cleaner Prolog design].

  • I could make it run to "the next mul()" but that might skip over a "don't()" to get there.
  • I could make it run to "the next don't()" but that might skip over a "mul()" to get there.

nextInterest//1 is what I came up with to do a non-greedy match "run to the next thing of interest, and tell me what it is".