How to resolve the algorithm Abelian sandpile model step by step in the Lua programming language
How to resolve the algorithm Abelian sandpile model step by step in the Lua programming language
Table of Contents
Problem Statement
Implement the Abelian sandpile model also known as Bak–Tang–Wiesenfeld model. Its history, mathematical definition and properties can be found under its wikipedia article. The task requires the creation of a 2D grid of arbitrary size on which "piles of sand" can be placed. Any "pile" that has 4 or more sand particles on it collapses, resulting in four particles being subtracted from the pile and distributed among its neighbors. It is recommended to display the output in some kind of image format, as terminal emulators are usually too small to display images larger than a few dozen characters tall. As an example of how to accomplish this, see the Bitmap/Write a PPM file task. Examples up to 2^30, wow! javascript running on web Examples:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Abelian sandpile model step by step in the Lua programming language
Source code in the lua programming language
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()
You may also check:How to resolve the algorithm Negative base numbers step by step in the Ring programming language
You may also check:How to resolve the algorithm Entropy step by step in the BQN programming language
You may also check:How to resolve the algorithm Damm algorithm step by step in the Python programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Chapel programming language
You may also check:How to resolve the algorithm Loops/While step by step in the REXX programming language