59
13
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.
2Can the input t be
0
? What's the ouput then? – Luis Mendo – 2016-09-04T21:46:24.0631Is it correct for at a given timestep multiple cascades can take place in a row? So in that timestep the cascades keep happening until every cell is again 3 or less? – flawr – 2016-09-04T21:55:14.637
2@flawr: Yes, that would be correct. Look at the difference between t=15 and t=16. – El'endia Starman – 2016-09-04T22:02:59.517
@LuisMendo The input is specified as positive t, so zero isn't a valid input. – Eric Tressler – 2016-09-04T22:28:45.673
1Is it REALLY that necessary to have
.
for empty cells? Can we have0
as a valid empty cell? – Andreï Kostyrka – 2016-09-05T00:31:21.813@AndreïKostyrka I guess it's not necessary. I'll update the spec. – Eric Tressler – 2016-09-05T00:33:39.197
Also, is it really that necessary to output a
n×n
text area? Can matrices or arrays be outputted instead? I mean, normally, in such challenges, it is the result that matters, not the fancyness of the output. Arrays and matrices are so common, there is no need to reformat them as square text blocks, unless the formatting trick is a part of the challenge. – Andreï Kostyrka – 2016-09-05T00:33:47.590@EricTressler Thank you! – Andreï Kostyrka – 2016-09-05T00:34:18.823
@EricTressler Since you have relaxed the output format, you should notify other answers so that they can maybe shorten their code (I already have) – Luis Mendo – 2016-09-05T09:02:20.087