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.
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..
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.
A single positive integer t, in any format you choose.
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
For 4, the output should be
.1. 1.1 .1.
Standard golf scoring applies.
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.