There is an Apple tree trunk positioned between -2 to 2 on the x-axis, where some apples fall down around it:
| | | | <-------|---|-------> -2 2
Everyday, n apples fall down. Each apple maintains its x coordinate as falls straight to the ground.
But if it lands on top of another apple, it will roll according to the following rules until it reaches the ground or a supporting layer of apples:
- If the space at x+1 at its current height is empty the current apple goes there.
- Else if the space at x-1 is empty the current apple goes there.
- Otherwise the current apple stays where it is on top of that other apple.
The input will be the n starting positions of each apple in order. You can take it as an array or as separated numbers or in any other valid way, just make sure you explain it in your answer.
The output should be an ASCII drawing of the tree trunk and the apples around. You are not required to draw the x-axis to the left of the leftmost apple and to the right of the rightmost apple, but you do need to draw it anywhere it is beneath some apple. You can also extend the tree above the highest apple.
You can assume all x-coordinates are between -100 and 100, but not between -2 and 2.
This is code-golf .. The shortest answer in bytes wins!
[-3, 5, 5, -4, -4, 5, -3]
a| | aaa| | aaa ---|---|----
[3, 3, 3, 3, 8, 9]
| |a | |aaa aa |---|-------
[-5, -5, -5, -5, -5, -5, -5, -5, -5]
a | | aaa | | aaaaa| | -----|---|