Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Golf: Playing Cubes

Perl 157 characters

This entry was inspired by gnibbler's Ruby entry, including the part about embedding the cube in the code. Also thanks to Kinopiko for schooling me on the 4 arg version of substr.

@O=($/.$"x99)x99;map{for$n(0..$_-1,$_..-1){map{substr$O[50-2*$n+$_],$W+4/$_,6,
(_,"__"
,"/__ /|",
"|   | |",
"|___|/")[$_]}1..4}$W+=4}@ARGV;print grep/\S/,@O

Explanation:

Line 1: Choose where cubes go
Lines 2-5: Put cubes where cubes go, print


Ruby - 233

mutable strings ;) No arbitrary "99" limits. pfft :p
Had to lose the picture of the cube in the source though

S=gets.split.map{|x|x.to_i}<<0
S.max.downto(S.min){|y|e=" ";q=e*3;r=e*3
S.map{|x|a=0,x;if(w=a.min)<=y&&x!=0&&y<z=a.max
r[-3,4]="|   | |";q[-3,4]="|___|/"+(y>w ?"|":e)else(z=z!=y)?q+=e*4:q[-2,4]="/__ /|"
r+=z ?e*4:"__  "end}
puts r,q}

Ruby - 243

Delete the extra space I put in there to get the nice picture of the cube

S=gets.split.map{|x|x.to_i}<<0
S.max.downto(S.min){|y|e=" ";q=e*3;r=e*3
S.map{|x|a=0,x;if(w=a.min)<=y&&x!=0&&y<=z=a.max
if z==y;r+="__  "
 q[-2,3]="/__ /|"else
r[-3,4]="|   | |"
q[-3,4]="|___|/"+(y>w ?e:"|")end
else r+=e*4;q+=e*4 end}
puts r,q}
$ echo 1 3 7 3 3 |ruby pc.rb
           __              
         /__ /|            
        |   | |            
        |___|/|            
        |   | |            
        |___|/|            
        |   | |            
        |___|/|            
       _|   | |__  __      
     /__|___|/__ /__ /|    
    |   |   |   |   | |    
    |___|___|___|___|/|    
   _|   |   |   |   | |    
 /__|___|___|___|___|/|    
|   |   |   |   |   | |    
|___|___|___|___|___|/  

Python - 249

The second for loop is indented with tab

S=map(int,raw_input().split())+[0]
for y in range(max(S),min(S)-1,-1):
 q=r=e=" "*4
 for x in S:
    w=x*(x<0);z=x*(x>0)
    if w<=y<z:r=r[:-3]+"|   | |";q=q[:-3]+"|___|/"+" |"[y>w]
    elif(y==z)*x:q=q[:-2]+"/__ /|";r+="__  "
    else:q+=e;r+=e
 print r+"\n"+q

Python - 393

S=map(int,raw_input().split())+[0]
for Y in range(max(S),min(S)-1,-1):
 Q=R="";B=s=t=" "*4;N=0
 for y in S:
  if(y>0)*(y==Y)+(Y==0)*(y<0):
   q="_  _";r=" /__";s="_   ";t=" /| "
   if(N<y>0)+(N==0):q="   _"
   if y<N>0:q="| |_";r="|/__"
  elif(y>Y>=0)+(y<=Y<0):q="|   ";r="|___";s="| | ";t="|/"+("| "[(y==Y<0)+(Y==0)])+" "
  else:q=s;r=t;s=t=B
  Q+=q;R+=r;N=y
 print Q.rstrip()+"\n"+R.rstrip()

Ruby, 261 258 250 242

c=[]
n=99
$F.map{|e|e=e.to_i
c<<(e<0?[e,-1]:[0,e-1])}
m=[]
x=0
c.map{|d|x+=4
k,l=d
(k+n..l+n).map{|y|y*=2
[[3,3,2,'__'],[2,1,6,'/__ /|'],[1,0,7,'|   | |'],[0,0,6,'|___|/']].map{|e|a,b,c,s=e
(m[y+a]||=' '*79)[x+b,c]=s}}}
puts m.compact.reverse

Run with ruby -n -a v2.rb

This entry is slightly overdesigned in that it can begin and end stacks at any level, not just at 0. There is no way to specify this in the "competition version", but if you replace the first 4 lines with c=eval $_;n=99 then it drops to 203 bytes and you can do: <

[[-3,3],[-3,-2],[2,3],[-3,-3],[-2,-1],[3,3],[2,2],[1,1],[0,0]]
           __      __          __                                                  
         /__ /|  /__ /|      /__ /|                                                
        |   | | |   | |     |   | |__                                              
        |___|/| |___|/|     |___|/__ /|                                            
        |   | | |   | |         |   | |__                                          
        |___|/| |___|/          |___|/__ /|                                        
        |   | |                     |   | |__                                      
        |___|/|                     |___|/__ /|                                    
        |   | |            __           |   | |                                    
        |___|/|          /__ /|         |___|/                                     
        |   | |__       |   | |                                                    
        |___|/__ /|     |___|/|                                                    
        |   |   | |    _|   | |                                                    
        |___|___|/|  /__|___|/                                                     
        |   |   | | |   | |                                                        
        |___|___|/  |___|/   

Befunge-93 (too many characters)

Very unoptimized. My first Befunge program. =]

>~:88+`v6 >11p>:!|v                 g13$<       v            $<
 000090#8 + >      >68*31p  v >     1-:!|!:-1g14<p+g11g13+g12g 14<
   __  :* *      5 ^       < > 31pvvp16<>:41p1- 31g1+g :68*-!#^_ ^
 /__ /||\-6      >>1-:       |^8   < $<|       `g16 $<   <
|   | |>-*8 ^ ^ p11-2g11-1$  <    >31g   11g+:::51g` |   1
|___|/   8^0     >#-#<           v    ^<             >51p^
        <  <         |`0:       p 56 p34:p30:p26:p25:p22:p21:p20:*68<
^  v95:< 6^ *2:*  -10<           >21g4+21p 11
1  >*-   |            >     > 31g51gg,31g21g-3-!#v_v
1   >$ ^v<                 | ,+55-g16p15+1:g15   <
|!-*48 <~> ^                ^           p13+1g13   <
>   ^                      @

Haskell, 349 characters:

r=replicate
f=foldl
k=repeat
o n a s=r n a++s++k a
main=do{a<-getLine;let{n=map read$words a;u=f max 0 n;d=f min 0 n;i=r(2*(1+u-d))$r(4*length n+3)' '};putStr$unlines$f(\j(x,n)->f(\i y->[[if a=='x'then b else a|(a,b)<-zip m n]|(m,n)<-zip(o(2*(u-y))(k 'x')$map(o(4*x)'x')["xxx__xx","x/__ /|","|   | |","|___|/x"])i])j[1+min 0 n..max 0 n])i$zip[0..]n}

Golfscript - 154

Newlines are significant. If you fail to strip the trailing newline you will get an extra number printed at the bottom! There is a TAB between - and 0

" "4*:s%{~}%0+: $):
;0=:g;{2,{:r;s {[
-   0
-]$(:b\(:t\;=!t!0t<1b>*2*+*[{s}[{"__  "}{-2<"/__ /|"}]r={-3<["|   | |""|___|/"["| "b!=]+]r=}]\=~+}%n}%
(:
g<!}do

Golfscript - 163

0`+" ":s%{~}%:A$):y;0=1-:g;{2,{:r;3s*A{[y- 0y-]$(:b\(:t\;=!:j;[{4s*}[{"__  "}{-2<"/__ /|"}]r={-3<["|   | |""|___|/"["| "b!=]+]r=}]0t=0t<1b>*2*+j*=~+}%n}%y(:y g>}do

Golfscript - 165

0`+" ":s%{~}%:A$):y;0=1-:g;{2,{:r;3s*A{[y- 0y-]$(:b\(:t\;=!:j;[
{4s*}.{"__  "
}{-2<"/__ /|"}
{-3<"|   | |"}
{-3<"|___|/"["| "b!=]+}]0t=2*0t<1b>*4*+r+j*=~+}%n}%y(:y g>}do