Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Golf: Hourglass

C/C++, a dismal 945 characters...

Takes input as parameters: a.out 5 52%

#include<stdio.h>
#include<memory.h>
#include<stdlib.h>
#define p printf

int h,c,*l,i,w,j,*q,k;const char*
 z;int main(int argc,char**argv)
  {h=atoi(argv[1]);c=(h*h*atoi(
   argv[2])+99)/100;l=new int[
    h*3];for(q=l,i=0,w=1;i<h;
     i++,c=(c-w)&~((c-w)>>31
      ),w+=2)if(c>=w){*q++=
       0;*q++ =0;* q++=w;}
        else {*q++=(c+1)/
         2;*q++=w-c;*q++
          =c/2;}p("_");
           for(i=0;i<h
            ;i ++)p (
             "__");p
              ("\n"
               );q
                =
               l+h
              *3-1;
             for (i=
            --h;i>=0;
           i--){p("%*"
          "s\\",h-i,"")
         ; z= "x\0 \0x";
        for(k=0;k<3;k++,q
       --,z+=2)for(j=0;j<*
      q;j++)p(z);q-=0;p("/"
     "\n");}q=l;for(i=0;i<=h
    ;i++){z =i==h? "_\0x\0_":
   " \0x\0 ";p("%*s/",h-i,"");
  for(k=0;k<3;k++,q++,z+=2)for(
 j=0;j<*q;j++)p(z);p("\\\n") ;}}

...and the decrypted version of this for us mere humans:

#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

#define p printf

int h, c, *l, i, w, j, *q, k;
const char *z;

int main(int argc, char** argv)
{
    h = atoi(argv [1]);
    c = (h*h*atoi(argv[2])+99)/100;
    l = new int[h*3];
    for (q = l,i = 0,w = 1; i<h; i++,c = (c-w)&~((c-w)>>31),w += 2) {
        if (c>=w) {
            *q++ = 0;
            *q++ = 0;
            *q++ = w;
        } else {
            *q++ = (c+1)/2;
            *q++ = w-c;
            *q++ = c/2;
        }
    }
    p("_");
    for (i = 0; i<h; i++) {
        p("__");
    }
    p("\n");
    q = l+h*3-1;
    for (i = --h; i>=0; i--) {
        p("%*s\\",h-i,"");
        z = "x\0 \0x";
        for (k = 0; k<3; k++,q--,z += 2) {
            for (j = 0; j<*q; j++) {
                p(z);
            }
        }
        p("/\n");
    }
    q = l;
    for (i = 0; i<=h; i++) {
        z = i==h ? "_\0x\0_" : " \0x\0 ";
        p("%*s/",h-i,"");
        for (k = 0; k<3; k++,q++,z += 2) {
            for (j = 0; j<*q; j++) {
                p(z);
            }
        }
        p("\\\n") ;
    }
}

Perl, 191 char

205 199 191 chars.

$S=-int((1-.01*pop)*($N=pop)*$N)+$N*$N;$S-=$s=$S>++$r?$r:$S,
$\=$/.$"x$N."\\".x x($v=$s/2).$"x($t=$r++-$s).x x($w=$v+.5)."/$\
".$"x$N."/".($^=$N?$":_)x$w.x x$t.$^x$v."\\"while$N--;print$^x++$r

Explicit newline required between the 2nd and 3rd lines.

And with help of the new Acme::AsciiArtinator module:

$S=-int((1-.01*pop)*($N=pop
)                         *
 $                       N
  )                     +
   $                   N
    *$N;(        ${B},$
     F,${x})=qw(\\ / x
      );while($N){;/l
       ater/g;$S-=$s
        =$S>++$r?$r
         :$S;'than
          you';@o
           =(" "
            x--
            $ N
           .   $
          B     .
         x       x
        (         $
       v           =
      $             s
     /               2
    )     .$"x($t=    $
   r++-$s).x x($w=$v+.5)
  .$F,@o,$"x$N.$F.($^=$N?
 $":_)x$w.x x$t.$^x$v.$B);
$,=$/}print$^x++$r,@o;think

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


Python, 213 char

N,p=map(int,raw_input()[:-1].split())
S=N*N-N*N*(100-p)/100
_,e,x,b,f,n=C='_ x\/\n'
o=""
r=1
while N:N-=1;z=C[N>0];s=min(S,r);S-=s;t=r-s;v=s/2;w=s-v;r+=2;o=n+e*N+b+x*v+e*t+x*w+f+o+n+e*N+f+z*w+x*t+z*v+b
print _*r+o