代码高尔夫球:播放立方体(Code Golf: Playing Cubes)

2019-08-31 13:21发布

我们面临的挑战

通过字符数最短的代码,这将输出播放砖根据用户输入的塔系列。

输入将是一连串的数字(正,负,零)的表示当前立方体塔下面的指数高度。 的高度为0意味着没有塔和被隔开。

立方体塔是由堆积立方体。 如果当前索引输入数为正,立方体上去,如果输入的数字为负,立方体下去。 单个立方体使用4条以下线画出:

   __
 /__ /|
|   | |
|___|/

立方体是3D - 这意味着它们隐藏彼此当两个塔下放置成彼此,产生假透视。

所有输入可以被假定为有效并且没有错误 - 每个号码与在一行空白空间分离,用至少一个数字。

测试用例

Input:
    2 -3 -2 1 2 -1
Output:
       __              __
     /__ /|          /__ /|
    |   | |        _|   | |
    |___|/|      /__|___|/|
    |   | |__  _|   |   | |__
    |___|/__ /__|___|___|/__ /|
        |   |   | |     |   | |
        |___|___|/|     |___|/
        |   |   | |
        |___|___|/
        |   | |
        |___|/

Input:
    1 2 3 4 -2 4 3 2 1
Output:
                   __      __ 
                 /__ /|  /__ /|
               _|   | | |   | |__
             /__|___|/| |___|/__ /|
           _|   |   | | |   |   | |__
         /__|___|___|/| |___|___|/__ /|
       _|   |   |   | | |   |   |   | |__
     /__|___|___|___|/| |___|___|___|/__ /|
    |   |   |   |   | |_|   |   |   |   | |
    |___|___|___|___|/__|___|___|___|___|/
                    |   | |
                    |___|/|
                    |   | |
                    |___|/

Input:
    1 3 3 7 0 -2 -2
Output:
                   __
                 /__ /|
                |   | |
                |___|/| 
                |   | | 
                |___|/| 
                |   | | 
                |___|/| 
           __  _|   | | 
         /__ /__|___|/| 
        |   |   |   | | 
        |___|___|___|/| 
       _|   |   |   | | 
     /__|___|___|___|/| 
    |   |   |   |   | |    __  __
    |___|___|___|___|/   /__ /__ /|
                        |   |   | |
                        |___|___|/|
                        |   |   | |
                        |___|___|/

码计数包括输入/​​输出(即完整的程序)。

Answer 1:

Perl的157个字符

此条目是由gnibbler的Ruby入门的启发,包括有关在代码中嵌入立方体的一部分。 同时也感谢Kinopiko上学我在的4 ARG版本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

说明:

1号线:选择在何处去立方体
第2-5行:将冰块冰块哪里去,打印



Answer 2:

红宝石 - 233

可变字符串;)无乱“99”的限制。 pfft:P
不得不失去立方体的图片源,虽然

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}

红宝石 - 243

删除多余的空间,我把在那里得到立方体的漂亮的图片

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
           __              
         /__ /|            
        |   | |            
        |___|/|            
        |   | |            
        |___|/|            
        |   | |            
        |___|/|            
       _|   | |__  __      
     /__|___|/__ /__ /|    
    |   |   |   |   | |    
    |___|___|___|___|/|    
   _|   |   |   |   | |    
 /__|___|___|___|___|/|    
|   |   |   |   |   | |    
|___|___|___|___|___|/  


Answer 3:

蟒蛇 - 249

for循环的第二个被缩进标签

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

蟒蛇 - 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()


Answer 4:

红宝石,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

与运行ruby -n -a v2.rb

此项略微过度设计的,它可以开始和结束堆在任何级别,而不仅仅是为0有没有办法在“比赛版”来指定这个,但如果你与更换前4行c=eval $_;n=99则下降到203个字节 ,可以这样做:<

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


Answer 5:

Befunge-93(太多字符)

非常没有优化。 我的第一个Befunge程序。 =]

>~: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   <
>   ^                      @


Answer 6:

哈斯克尔,349个字符:

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}


Answer 7:

高尔夫球脚本 - 154

换行符显著。 如果你不能去掉尾随换行符,你会得到在底部印刷一个额外的号码! 之间有一个TAB -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

高尔夫球脚本- 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

高尔夫球脚本- 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


Answer 8:

C,287个字符

#define F(r)for(r=0;r<98;r++)
#define C(y,s)for(k=0;s[k];k++)b[49-i][w+k+y]=s[k];i++;
char k,i,j,w,t,b[98][99];main(c,v)char**v;{F(i)F(j)b[i][j]=32;
for(j=0;j<c;j++){i=2*atoi(v[j]);t=0;if(i>0)t=i,i=0;for(;i<t;i-=2){
C(0,"|___|/")C(0,"|   | |")C(1,"/__ /|")C(3,"__")}w+=4;}F(i)puts(b[i]);}

(该字符数不包括两个新行)

这将运行作为一个命令行程序,如

./cubes 1 2 3 4 5 0 1 3 2 -1 -10

您可以尝试在这里运行它: http://codepad.org/tu4HDqSy (此版本被改变,因为codepad.org不允许命令行参数。)

一个方便的尖端未高尔夫球是通过运行它cpp然后indent

char                k        , i, j, w, t, b[98][99];
main(c, v)
    char          **v;
{
    for (i = 0; i < 98; i++)
        for (j = 0; j < 98; j++)
            b[i][j] = 32;
    for (j = 0; j < c; j++) {
        i = 2 * atoi(v[j]);
        t = 0;
        if (i > 0)
            t = i, i = 0;
        for (; i < t; i -= 2) {
            for (k = 0; "|___|/"[k]; k++)
                b[49 - i][w + k + 0] = "|___|/"[k];
            i++;
            for (k = 0; "|   | |"[k]; k++)
                b[49 - i][w + k + 0] = "|   | |"[k];
            i++;
            for (k = 0; "/__ /|"[k]; k++)
                b[49 - i][w + k + 1] = "/__ /|"[k];
            i++;
            for (k = 0; "__"[k]; k++)
                b[49 - i][w + k + 3] = "__"[k];
            i++;
        } w += 4;
    } for (i = 0; i < 98; i++)
        puts(b[i]);
}


Answer 9:

Python(2.6),1092 905 623 501 478 345 - > 318个字符

所有评论欢迎!

r=range;p="  __","/__ /|","|   | |","|___|/"
l=map(int,raw_input().split())+[0];d=max(l)
g=[[" "]*(len(l)+1)*4 for i in r(d-min(l)+1)*2]
for i,e in enumerate(l):
 for x,y in sorted([(i*4,(d-e+y-(0,1)[e<0])*2)for y in r(0,e,(1,-1)[e<0])])[::-1]:
  for i in r(4):g[y+i][(x,x+1)[i<2]:x+6]=p[i]
for k in g:print ''.join(k)


Answer 10:

627个字节VB.NET 9的 (不计新线)

Dim a As New List(Of Int32)
Dim b=InputBox("").Split(" ".ToCharArray)
For Each d In b
a.Add(Int32.Parse(d))
Next
Dim e=New String(){"|___|/","|   | |","/__ /|","__"}
Dim f=a.Min
Dim g=a.Max
Dim h=a.Count
Dim i As New List(Of String)
Dim j=(If(g>0,g,0)+If(f<0,-f,0))*2+1
For d=0To j
i.Add(Space(h*6))
Next
For d=f To g
If (d<>0) Then
For k=0To 3
Dim l=i(j)
Dim m=0
While m<h
If (d<0And a(m)<=d)Or(d>0And a(m)>=d) Then
Dim n=m*4+If(k>1,(k-2)*2+1,0)
l=l.Substring(0,n)&e(k)&l.Substring(n+e(k).Length)
i(j)=l
endif
m+=1
End While
j-=1
Next
j+=2
EndIf
Next
For Each d In i
Console.WriteLine(d)
Next


Answer 11:

313个字节的Ruby

c=gets.split.map{|n|n.to_i}+[0];l=[" "*(c.length*5)]*((c.max-c.min)*2+2)
c.each_index{|i|h=c[i];h==0&&next
(h<0?(h...0):(0...h)).to_a.each{|b|y=c.max*2+1-b*2;x=i*4;s=(x+7)..-1
4.times{|a|l[y-a]=l[y-a][0,x+[0,0,1,3][a]]+['|___|/','|   | |','/__ /|','__'
][a]+(l[y-a][[(x+6)..-1,s,s,s][a]]||'')}}};l.each{|e|puts e}

(有一个换行符不计,这只是那里,因为否则将是有一个很长的线。)

我用使字符和粘贴的2D阵列中立方体一次一个,底部到顶部的显而易见的方法和左到右。



Answer 12:

Lua中,453个字符

s=io.read("*l")x={}for v in s:gmatch("-?%d+")do x[#x+1]=tonumber(v)end m=math
u=unpack s=string c={s.byte("   __   /__ /||###|#||___|/ ",1,28)}t=m.max(0,u(x))-1
b=m.min(0,u(x))l=#x*4+3 a={}for n=b,t do for p,v in ipairs(x)do y=n<0 and v<=n
or n>=0 and v>n for i=1,4 do e=(t-n)*2+i a[e]=a[e]or{}for j=1,7 do
d=c[(i-1)*7+j]f=(p-1)*4+j a[e][f]=y and d~=32 and d or a[e][f] or 32 end
end end end for i,v in ipairs(a)do print((s.char(u(v)):gsub("#"," ")))end


Answer 13:

PHP - 447 398

我知道这不是短这里的其他人,但我很高兴与它;)

<?$g=$argv;unset($g[0]);$c=array('|___|/',' '=>'|   | |','/__ 
/|',3=>'__');$x=max(max($g),0);$m=$i=min(min($g),0);$row=$y=0;
while($x>=$i){$n=$row++*2;foreach ($c as $w=>$p){
$L[]=str_pad('',count($g)*4," ");$t='';$y=0;foreach ($g as $h)
{if((($h>=$i&&$i>0)||($h<$i&&$i<=0)))$L[$n]=substr_replace($L[$n],$p,4*$y+$w,
strlen($p));$y++;}$n++;}$i++;}array_splice($L,$n);krsort($L);
print implode("\n",$L);?>

输入输出

   C:\development\code-golf>php cubes1.php 1 2 1 -5 -5 4 3 3
                       __
                     /__ /|
                    |   | |__  __
                    |___|/__ /__ /|
       __           |   |   |   | |
     /__ /|         |___|___|___|/|
   _|   | |__       |   |   |   | |
 /__|___|/__ /|     |___|___|___|/|
|   |   |   | |__  _|   |   |   | |
|___|___|___|/__ /__|___|___|___|/
            |   |   | |
            |___|___|/|
            |   |   | |
            |___|___|/|
            |   |   | |
            |___|___|/|
            |   |   | |
            |___|___|/|
            |   |   | |
            |___|___|/


文章来源: Code Golf: Playing Cubes