15
\$\begingroup\$

Introduction

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:

  1. If the space at x+1 at its current height is empty the current apple goes there.
  2. Else if the space at x-1 is empty the current apple goes there.
  3. Otherwise the current apple stays where it is on top of that other apple.

Challenge

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 .. The shortest answer in bytes wins!

Examples

Input: [-3, 5, 5, -4, -4, 5, -3]

Output:

  a|   |
aaa|   | aaa
---|---|----

Input: [3, 3, 3, 3, 8, 9]

Output:

|   |a
|   |aaa  aa
|---|-------

Input: [-5, -5, -5, -5, -5, -5, -5, -5, -5]

Output:

  a  |   |
 aaa |   |
aaaaa|   |
-----|---|
\$\endgroup\$
5
  • \$\begingroup\$ Fond memories. :) \$\endgroup\$ Commented Jan 21, 2016 at 13:49
  • \$\begingroup\$ Your examples imply that the minimal possible extent in the x direction should be shown (no empty ground to the left and right of the trees and apples). Is that a requirement? Also, are we allowed to print trailing spaces, at least to pad the output to a rectangle? \$\endgroup\$ Commented Jan 21, 2016 at 13:52
  • 1
    \$\begingroup\$ Bonuses aren't very popular. I think requiring the "visible" output to be minimal (no empty rows at the top, no ground cells at the edges which are empty) is fine, but I would definitely allow padding the output to a rectangle with spaces. \$\endgroup\$ Commented Jan 21, 2016 at 14:04
  • \$\begingroup\$ Also, your bonus currently doesn't make sense because the spec doesn't actually allow any different output format to begin with. \$\endgroup\$ Commented Jan 21, 2016 at 14:05
  • 2
    \$\begingroup\$ I will give a 100 rep bounty to the first answer in Marbelous. Falling apples computed by falling marbles...it's just too fitting. \$\endgroup\$
    – quintopia
    Commented Jan 22, 2016 at 15:51

2 Answers 2

1
\$\begingroup\$

PHP, 230 bytes

I've added the first two newlines for readibility.

function($l){for($y=count($l);$y>=0;$f[]="$a|   |$a")$a=str_repeat($y--?$w=' ':'-',99);
foreach($l as$x){for($x+=101,$s=$y=0;!$s;$b[$x]!=$w?$b[$x+1]!=$w?$b[$x-1]!=$w?$s=1:--$x
:++$x:0)$b=$f[$y++];$f[$y-2][$x]=a;}echo join('
',$f);};

Ideone

Here is the ungolfed version:

function drawApples(array $listStartX)
{
    $field = [];
    $maximalHeight = count($listStartX);
    for ($y = $maximalHeight; $y >= 0; --$y)
    {
        $line = str_repeat($y > 0 ? ' ' : '-', 98);
        $field[] = $line .'|   |'. $line;
    }

    foreach ($listStartX as $x)
    {
        $x += 100;
        for ($y = 0; true; ++$y)
        {
            if ($field[$y][$x] === ' ') {
                continue;
            }

            if ($field[$y][$x + 1] === ' ') {
                ++$x;
                continue;
            }

            if ($field[$y][$x - 1] === ' ') {
                --$x;
                continue;
            }

            $field[$y - 1][$x] = 'a';
            break;
        }
    }

    echo implode("\n", $field);
}
\$\endgroup\$
1
\$\begingroup\$

Python 2.7, 282 bytes

from collections import*;a=Counter();a[2]=-1;a[-2]=-1
for d in input():
 while 0<=a[d+1]<a[d]:d+=1
 while 0<=a[d-1]<a[d]:d-=1
 a[d]+=1
m=max(a.values())+1
print'\n'.join(''.join(' '*(m-a[i]-1)+'a'*a[i]+'-'if i**2!=4 else'|'*m for i in range(min(a),max(a)+1))[i::m]for i in range(m))

Ideone

Well… I tried.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.