r/adventofcode • • Dec 18 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 18 Solutions -🎄-

NEW AND NOTEWORTHY

  • FYI: 23:59 Amsterdam time zone is 17:59 EST


Advent of Code 2021: Adventure Time!


--- Day 18: Snailfish ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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:43:50, megathread unlocked!

45 Upvotes

595 comments sorted by

View all comments

2

u/e_blake Dec 20 '21

m4 day18.m4

Depends on my framework common.m4. A strictly recursive solution, but spends way too much time in reduce (~3s for part1, ~66s for part2). I could probably speed it up by hard-coding operations on an array (after all, a reduced form has at most 16 elements, and 32 elements after a worst-case addition is not too hard to work with, compared to doing just one layer at a time). I had fun using m4's ability to handle nested () in my favor - I didn't have to do any parsing, just translit the input from [] to (). I coded magnitude in just a few seconds (2 lines); split was the next easiest (6 lines) and my attempts to get explode working took a while (my first try of doing it all in one pass didn't work, so I had to refactor to explode only 1 pair per top-level explode, 10 lines).

→ More replies (2)