An abelian sandpile, for our purposes, is an infinite grid with integer coordinates, initially empty of sand. After each second, a grain of sand is placed at (0,0). Whenever a grid cell has 4 or more grains of sand, it spills one grain of sand to each of its four neighbors simultaneously. The neighbors of (x,y) are (x-1,y), (x+1,y), (x,y-1), and (x,y+1).
When a cell spills, it may cause its neighbors to spill. Some facts:
- This cascade will eventually stop.
- The order in which the cells spill is irrelevant; the result will be the same.
Example
After 3 seconds, the grid looks like
.....
.....
..3..
.....
.....
After 4 seconds:
.....
..1..
.1.1.
..1..
.....
After 15 seconds:
.....
..3..
.333.
..3..
.....
And after 16 seconds:
..1..
.212.
11.11
.212.
..1..
The challenge
In as few bytes as possible, write a function that takes a single positive integer t and outputs a picture of the sandpile after t seconds.
Input
A single positive integer t, in any format you choose.
Output
A picture of the sandpile after t seconds, using the characters
. 1 2 3
Edit: Use any four distinct characters you like, or draw a picture. If you're not using ".123" or "0123", specify in your answer what the characters signify.
Unlike in the examples, your output should contain the minimal number of rows and columns necessary to show the nonzero part of the sandpile.
That is, for input 3, the output should be
3
For 4, the output should be
.1.
1.1
.1.
Scoring
Standard golf scoring applies.
Rules
No language functions or libraries that already know what a sandpile is are allowed.
Edit: The output section has been edited, the character set restriction has been completely lifted. Use any four distinct characters or colors you like.
0
? What's the ouput then? \$\endgroup\$.
for empty cells? Can we have0
as a valid empty cell? \$\endgroup\$