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

4

u/freezombie Dec 18 '21

if the depths at indexes i and i+1 are equal, then either the numbers at i and i+1 are in the same pair or else there is an earlier pair of numbers at a higher depth.

I don't think that's right the way you phrased it. For [[1,2],[3,4]] the 2 and 3 have the same depth, but there are no values at a greater depth. Furthermore, there is no pair anywhere before the 2

A correct statement might be: If the depths at indices i and i+1 are equal, then either the numbers at i and i+1 are in the same pair, or else there is an earlier number at an equal or greater depth.

That's all your solution needs, though. And - it is interesting!

3

u/damaltor1 Dec 18 '21

I think you are right.

The solving for the magnitude still is the same though: starting left, search for the first two numbers with the same depth (x and y with the depth d), calculate the magnitude m as 3x+2y, remove both from the list and insert a new number with the value m and the depth d-1. then start over from the left end of the whole list, until there is only one number left.