Abelian sandpiles
Created on May 19, 2020 by Julien Palard
Simulate gravity applied to a sandpile.
Write a function apply_gravity that given a 2D numpy array representing
sand will "apply gravity" on it.
- Each element of the array is an integer representing the height of the sandpile
- 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.
Your function have to return the sandpile.
Example
>>> import numpy as np
>>> sandpile = np.zeros((5, 5), dtype=np.uint32)
>>> sandpile[2, 2] = 16
>>> print(apply_gravity(sandpile))
[[0 0 1 0 0]
[0 2 1 2 0]
[1 1 0 1 1]
[0 2 1 2 0]
[0 0 1 0 0]]
Resources
https://en.wikipedia.org/wiki/Abelian_sandpile_model
Bonus on your machine (not tested by Genepy)
Use matplotlib to display the result as an image:
sandpile = apply_gravity(sandpile)
plt.imshow(sandpile)
plt.show()
To showcase the results, I computed a pile of 2 ** 26 grains of sand.
There's no corrections yet, hit the `Submit` button to send your code to the correction bot.
Keyboard shortcuts:
- Ctrl-Enter: Send your code to the correction bot.
- Escape: Get back to the instructions tab.