Given a string or list, calculate the effects of gravity on that string/list.
The string will be made of 5 characters:
Nhas a weight of 2
Lhas a weight of 1
Shas a weight of -2
Dhas a weight of -1
(the 5th character is whatever you want, it means empty space. I use
(space) in my examples)
Items with negative weight fall upwards (towards the top of the string), and items with positive weight fall downwards (towards the bottom of the string).
When an object collides with something, one of three things happens:
If the object collides with the top or bottom "walls" of the input, then it just stops.
If the object collides with another object, their weight/velocity is summed (i.e.
1). If the result is positive, both objects fall downwards. If it's negative, they fall upwards.
If an object (let's call it
A) collides with an object
Bis already pinned against the ceiling/floor,
Awill just come to rest against
All non-occupied spaces of the string/list should be filled with the empty character.
If it wasn't clear already: each character in the string/line is on top of the next one. i.e.
NL is the same as:
(blank) S D
Test cases: (uses
(the space character) to show empty space)
" L D " -> " LD " or " LD " " SDLN " -> "SD LN" " NLLSSD " -> "NLLSSD "