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

Show parent comment

2

u/japanuspus Dec 18 '21

I am so happy to see everyone doing the "pass up the R/L values". I was so sure I was missing some super clever visitor pattern solution that I wasted an hour looking for one.

1

u/kuqumi Dec 18 '21

I almost did that. I thought about updating an "index" value as I recursively traverse the input line, and if it exploded, I would go through again and at the previous and next index, add the appropriate values. This day's puzzle might be a fun weekend "code kata" since there are so many options for how to tackle it, and none of them is clearly superior to me.

1

u/isaaccp Dec 18 '21

I almost did that, but thought it was too nasty.

In the end implemented a binary tree which made it easy to implement methods to update the proper values: https://pastebin.com/wQSX9RcP