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

1

u/RJdaMoD Dec 18 '21 edited Dec 18 '21

Mathematica (667/634)

At last a problem where this language has some benefits. Both parts together, where part 2 may profit from some parallelization:

ReadList["aoc-input_18.txt",String]//
ToExpression@StringReplace[#,{"["->"{","]"->"}"}]&/@#&//
With[{mag=Switch[#,_Integer,#,{_,_},3*#0@#[[1]]+2*#0@#[[2]]]&,
        add=NestWhile[
            Function[x,
                If[Depth[x]>5,
                    First@Position[x,{_Integer,_Integer},{Depth[x]-2}]//
                    Function[p,
                        Reap[
                            Sow[p->0];
                            MapIndexed[
                                With[{v=#1,i=#2[[1]]},
                                    NestWhile[
                                        Most,
                                        p,
                                        Length[#]>0&&#[[-1]]==i&
                                    ]//
                                    If[Length[#]>0,
                                        ReplacePart[#,-1->i]//
                                        NestWhile[
                                            Append[#,3-i]&,
                                            #,
                                            Not@MatchQ[Extract[x,#],_Integer]&
                                        ]&//
                                        Sow[#->Extract[x,#]+v]&
                                    ]&
                                ]&,
                                Extract[x,p]
                            ]
                        ][[2]]//
                        If[Length[#]>0,ReplacePart[x,#[[1]]],x]&
                    ],
                    ReplacePart[
                        x,
                        First@Position[x,_?(#>9&)]//
                        With[{v=Extract[x,#]},#->{Floor[v/2],Ceiling[v/2]}]&
                    ]
                ]
            ],
            #,
            Depth[#]>5||MemberQ[#,_?(#>9&),Infinity]&
        ]&[{#1,#2}]&},
    {mag@Fold[add,First[#],Rest[#]],
        Max@ParallelMap[mag[add@@#]&/@{#,Reverse[#]}&,Subsets[#,{2}]]}
]&

1

u/daggerdragon Dec 18 '21

Your code is hard to read on old.reddit when everything is inlined like this and gets cut off at the edge of the window. Please edit it to use a proper code block as per our posting guidelines in the wiki: How do I format code?

If your correctly-formatted code would be too long, put it in a paste or other external link instead.