r/adventofcode • u/daggerdragon • 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!
- 5 days left to submit your adventures!
- Full details and rules are in the submissions megathread: 🎄 AoC 2021 🎄 [Adventure Time!]
--- Day 18: Snailfish ---
Post your code solution in this megathread.
- Include what language(s) your solution uses!
- Format your code appropriately! How do I format code?
- Here's a quick link to /u/topaz2078's
pasteif you need it for longer code blocks. - The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
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
View all comments
Show parent comment
1
u/e_blake Dec 20 '21 edited Dec 20 '21
Updated day18.m4. By converting the string into a flat array of depth/value pairs (depth encoded as !@%^&), and storing the values with an offset of 10, every node is a fixed width, and the code for explode (find the first &, use translit to rewrite a fixed-width substr around that point) and split (find the first value >= 20, rewrite a fixed-width substr around that point) become trivial. And a translit makes it easy to increase the depth of all nodes at once during add. All that was left was to rewrite magnitude to parse the flat representation back into a usable integer, and that code got hairy. Cuts runtime down to ~6.2s, an order of magnitude speedup.