Advent of Code 2022 Day 11 — Mathematica

Functions in Mathematica are symbolic expressions you can examine and modify just like any other symbolic expressions, which allows for some fun:

i=Import@"input.txt";
{items,inspect,throw}=Association/@Transpose[Thread[#[[1,1]]->{#[[2]],(x|->a[x,b])/.{a->#[[3,1]],b->If[Length@#[[3]]>1,#[[3,2]],x]},(x|->If[Divisible[x,a],b,c])/.{a->#[[4,1]],b->#[[5,1]],c->#[[6,1]]}}]&@ToExpression[StringSplit[#,Except[DigitCharacter|"*"|"+"]..]/.{"+"->Plus,"*"->Times}]&/@StringSplit[StringSplit[i,"\n\n"],"\n"]];
Times@@TakeLargest[Merge[Reap[Fold[Merge[{Append[#1,#2->{}],Sow[#2->Length@#1@#2];With[{items=Mod[inspect@#2/@#1@#2,LCM@@throw[[;;,2,1,2]]]},Thread[throw@#2/@items->items]]},Flatten]&,items,Flatten@Table[Keys@items,20]]][[2,1]],Total],2]