Abelian Bunker
Voici some Abelian sandpile code golf:
ab=: -[:+/@(-"2((,-)=/~i.2)|.!.0])3&<
clrs =: ".(;._2) 0 : 0 0 0 0 54 85 134 109 171 255 163 255 255 ) sand =: ,"_1/^:2 ] 3 3&$"0 (ab^:_) 12000 (<50 50)} 101 101$0 echo '../images/abelian-bunker.png' linki sand { clrs
Abelian sandpiles sit on vertices of a graph and the piles begin to topple over whenever the grains of sand outnumber the edges leaving the vertex. Our graph is just a grid graph, where each square on the plane is connected to its four neighbors. So, all entries in the matrix with sand \(\ge 4\) are redistributed to the neighboring squares. Eventually this process settles; we let the sand fall off the edge, if it gets there.
sand =: ,"_1/^:2 ] 3 3&$"0 (ab^:_) 101 101 $ 4 echo '../images/abelian-bunker-0.png' linki sand { |. clrs
Journey to smallest golf:
ab=: +[:(_4&*+[:+/((,-)(,:|.)0 1)|.!.0])3&< NB. first try mw=: +[:+/@(_4&*,((,-)2]\i:1)|.!.0])3&< NB. tangenstorm version ab=: -[:(4&*(-+/)((,-)2]\i:1)|.!.0])3&< NB. subtraction trick 1 ab=: -[:+/@(-"2((,-)=/~i.2)|.!.0])3&< NB. subtraction trick 2 ae=: {{y-+/(-"2 u|.!.0])y>:#u}} NB. direct definition version
tangentstorm's much better neighborhood phrases:
nhood =: (,-)(,:|.)0 1 NB. my original nhood =: (,-)=/~i.2 NB. Michal Wallace's improvements nhood =: (,-)2]\i:1