Abelian sandpile model: Difference between revisions
Content added Content deleted
m (Added library reference.) |
(→{{header|Lua}}: added Lua solution) |
||
Line 1,395: | Line 1,395: | ||
[http://alahonua.com/temp/Abel_Z_color_100000.png Link to PNG output file for N=100000 ie. AbelSand.move(100000)] <br /> |
[http://alahonua.com/temp/Abel_Z_color_100000.png Link to PNG output file for N=100000 ie. AbelSand.move(100000)] <br /> |
||
[http://alahonua.com/temp/Abel_Z_color_1000000.png Link to PNG output file (run time >90 min) for N=1000000 (move(1000000))] |
[http://alahonua.com/temp/Abel_Z_color_1000000.png Link to PNG output file (run time >90 min) for N=1000000 (move(1000000))] |
||
=={{header|Lua}}== |
|||
<lang lua>local sandpile = { |
|||
init = function(self, dim, val) |
|||
self.cell, self.dim = {}, dim |
|||
for r = 1, dim do |
|||
self.cell[r] = {} |
|||
for c = 1, dim do |
|||
self.cell[r][c] = 0 |
|||
end |
|||
end |
|||
self.cell[math.floor(dim/2)+1][math.floor(dim/2)+1] = val |
|||
end, |
|||
iter = function(self) |
|||
local dim, cel, more = self.dim, self.cell |
|||
repeat |
|||
more = false |
|||
for r = 1, dim do |
|||
for c = 1, dim do |
|||
if cel[r][c] >= 4 then |
|||
cel[r][c] = cel[r][c] - 4 |
|||
if c > 1 then cel[r][c-1], more = cel[r][c-1]+1, more or cel[r][c-1]>=3 end |
|||
if c < dim then cel[r][c+1], more = cel[r][c+1]+1, more or cel[r][c+1]>=3 end |
|||
if r > 1 then cel[r-1][c], more = cel[r-1][c]+1, more or cel[r-1][c]>=3 end |
|||
if r < dim then cel[r+1][c], more = cel[r+1][c]+1, more or cel[r+1][c]>=3 end |
|||
end |
|||
more = more or cel[r][c] >= 4 |
|||
end |
|||
end |
|||
until not more |
|||
end, |
|||
draw = function(self) |
|||
for r = 1, self.dim do |
|||
print(table.concat(self.cell[r]," ")) |
|||
end |
|||
end, |
|||
} |
|||
sandpile:init(15, 256) |
|||
sandpile:iter() |
|||
sandpile:draw()</lang> |
|||
{{out}} |
|||
<pre>0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
|||
0 0 0 0 0 1 2 2 2 1 0 0 0 0 0 |
|||
0 0 0 0 2 1 0 2 0 1 2 0 0 0 0 |
|||
0 0 0 2 3 3 3 2 3 3 3 2 0 0 0 |
|||
0 0 2 3 2 2 2 3 2 2 2 3 2 0 0 |
|||
0 1 1 3 2 2 3 0 3 2 2 3 1 1 0 |
|||
0 2 0 3 2 3 2 3 2 3 2 3 0 2 0 |
|||
0 2 2 2 3 0 3 0 3 0 3 2 2 2 0 |
|||
0 2 0 3 2 3 2 3 2 3 2 3 0 2 0 |
|||
0 1 1 3 2 2 3 0 3 2 2 3 1 1 0 |
|||
0 0 2 3 2 2 2 3 2 2 2 3 2 0 0 |
|||
0 0 0 2 3 3 3 2 3 3 3 2 0 0 0 |
|||
0 0 0 0 2 1 0 2 0 1 2 0 0 0 0 |
|||
0 0 0 0 0 1 2 2 2 1 0 0 0 0 0 |
|||
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0</pre> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |