R – Code Golf: Hourglass

code-golflanguage-agnosticrosetta-stone

The challenge

The shortest code by character count to output an hourglass according to user input.

Input is composed of two numbers: First number is a greater than 1 integer that represents the height of the bulbs, second number is a percentage (0 – 100) of the hourglass' capacity.

The hourglass' height is made by adding more lines to the hourglass' bulbs, so size 2 (the minimal accepted size) would be:

_____
\   /
 \ /
 / \
/___\

Size 3 will add more lines making the bulbs be able to fit more 'sand'.

Sand will be drawn using the character x. The top bulb will contain N percent 'sand' while the bottom bulb will contain (100 – N) percent sand, where N is the second variable.

'Capacity' is measured by the amount of spaces () the hourglass contains. Where percentage is not exact, it should be rounded up.

Sand is drawn from outside in, giving the right side precedence in case percentage result is even.

Test cases

Input:
    3 71%
Output:
    _______
    \x  xx/
     \xxx/
      \x/
      / \
     /   \
    /__xx_\

Input:
    5 52%
Output:
    ___________
    \         /
     \xx   xx/
      \xxxxx/
       \xxx/
        \x/
        / \
       /   \
      /     \
     /  xxx  \
    /xxxxxxxxx\

Input:
    6 75%
Output:
     _____________
     \x         x/
      \xxxxxxxxx/
       \xxxxxxx/
        \xxxxx/
         \xxx/
          \x/
          / \
         /   \
        /     \
       /       \
      /         \
     /_xxxxxxxxx_\

Code count includes input/output (i.e full program).

Best Answer

Golfscript - 136 Chars (Fits in a Tweet)

Be sure not to have a newline after the % for the input
eg
$ echo -n 3 71%|./golfscript.rb hourglass.gs

You can animate the hourglass like this:

$ for((c=100;c>=0;c--));do echo -n "15 $c%"|./golfscript.rb hourglass.gs;echo;sleep 0.1;done;

Golfscript - 136 Chars
Make sure you don't save it with an extra newline on the end or it will print an extra number

);' ': /(~:
;0=~100.@-
.**\/:t;'_':&&
*.n
,{:y *.'\\'+{[&'x':x]0t(:t>=}:S~
(y-,{;S\+S+.}%;'/'++\+}%.{&/ *}%\-1%{-1%x/ *&/x*}%) /&[*]++n*    

Golfscript - 144 Chars

);' ':|/(~:^.*:X
 ;0=~100.@-X*\/
  X'x':x*'_':&
   @*+:s;&&&+
    ^*n^,{:y
     |*.[92
      ]+{s
       [)
       \#
      :s;]
     }:S~^(
    y-,{;S\+
   S+.}%;'/'+
  +\+}%.{&/|*}
 %\-1%{-1%x/|*&
/x*}%)|/&[*]++n*

How it works
First do the top line of underscores which is 2n+1 Create the top half of the hourglass, but use '_' chars instead of spaces, so for the 3 71% we would have.

\x__xx/
 \xxx/
  \x/

Complete the top half by replacing the "_" with " " but save a copy to generate the bottom half

The bottom half is created by reversing the whole thing

  /x\
 /xxx\
/xx__x\

Replacing all the 'x' with ' ' and then then '_' with 'x'

  / \
 /   \
/  xx \

Finally replace the ' ' in the bottom row with '_'

  / \
 /   \
/__xx_\

Roundabout but for me, the code turned out shorter than trying to generate both halves at once