编程高尔夫:玩魔方

发布于 2024-08-08 20:11:04 字数 1897 浏览 2 评论 0原文

挑战

按字符数计算的最短代码,将根据用户输入输出积木塔系列。

输入将是一系列数字(正数、负数和零),表示当前立方体塔在其索引之后的高度。高度为 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:
                   __
                 /__ /|
                |   | |
                |___|/| 
                |   | | 
                |___|/| 
                |   | | 
                |___|/| 
           __  _|   | | 
         /__ /__|___|/| 
        |   |   |   | | 
        |___|___|___|/| 
       _|   |   |   | | 
     /__|___|___|___|/| 
    |   |   |   |   | |    __  __
    |___|___|___|___|/   /__ /__ /|
                        |   |   | |
                        |___|___|/|
                        |   |   | |
                        |___|___|/

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

The challenge

The shortest code by character count, that will output playing bricks tower series according to user input.

The input will be a series of numbers (positive, negative and zero) that represents the height of the current cube tower following their index. A height of 0 means no tower and is spaced.

A cube tower is composed of stacked cubes. If the input number on the current index is positive, the cubes go up, if the input number is negative, the cubes go down. A single cube is drawn using the 4 following lines:

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

Cubes are 3D - this means they hide each other when two towers are placed next to each other, generating fake perspective.

All input can be assumed to be valid and without errors - Each number is separated with a white space on a single line, with at least one number.

Test cases

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

梅窗月明清似水 2024-08-15 20:11:04

Perl 157 个字符

这个条目的灵感来自于 gnibbler 的 Ruby 条目,包括关于在代码中嵌入立方体的部分。还要感谢 Kinopiko 指导我学习 substr 的 4 arg 版本。

@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 行:将立方体放在立方体所在的位置,打印

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

反差帅 2024-08-15 20:11:04

Ruby - 233 个

可变字符串;)没有任意的“99”限制。噗: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}

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

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
           __              
         /__ /|            
        |   | |            
        |___|/|            
        |   | |            
        |___|/|            
        |   | |            
        |___|/|            
       _|   | |__  __      
     /__|___|/__ /__ /|    
    |   |   |   |   | |    
    |___|___|___|___|/|    
   _|   |   |   |   | |    
 /__|___|___|___|___|/|    
|   |   |   |   |   | |    
|___|___|___|___|___|/  
朕就是辣么酷 2024-08-15 20:11:04

Python - 249

第二个 for 循环用 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()

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()
海螺姑娘 2024-08-15 20:11:04

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

此条目设计得有点过度,因为它可以在任何级别开始和结束堆栈,而不仅仅是在0. 在“竞赛版本”中无法指定这一点,但如果用 c=eval $_;n=99 替换前 4 行,那么它会下降到 203 字节 你可以这样做:<

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

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]]
           __      __          __                                                  
         /__ /|  /__ /|      /__ /|                                                
        |   | | |   | |     |   | |__                                              
        |___|/| |___|/|     |___|/__ /|                                            
        |   | | |   | |         |   | |__                                          
        |___|/| |___|/          |___|/__ /|                                        
        |   | |                     |   | |__                                      
        |___|/|                     |___|/__ /|                                    
        |   | |            __           |   | |                                    
        |___|/|          /__ /|         |___|/                                     
        |   | |__       |   | |                                                    
        |___|/__ /|     |___|/|                                                    
        |   |   | |    _|   | |                                                    
        |___|___|/|  /__|___|/                                                     
        |   |   | | |   | |                                                        
        |___|___|/  |___|/   
第几種人 2024-08-15 20:11:04

Befunge-93(字符太多)

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

>~:88+`v6 >11p>:!|v                 g13
lt;       v            
lt;
 000090#8 + >      >68*31p  v >     1-:!|!:-1g14<p+g11g13+g12g 14<
   __  :* *      5 ^       < > 31pvvp16<>:41p1- 31g1+g :68*-!#^_ ^
 /__ /||\-6      >>1-:       |^8   < 
lt;|       `g16 
lt;   <
|   | |>-*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   <
>   ^                      @

Befunge-93 (too many characters)

Very unoptimized. My first Befunge program. =]

>~:88+`v6 >11p>:!|v                 g13
lt;       v            
lt;
 000090#8 + >      >68*31p  v >     1-:!|!:-1g14<p+g11g13+g12g 14<
   __  :* *      5 ^       < > 31pvvp16<>:41p1- 31g1+g :68*-!#^_ ^
 /__ /||\-6      >>1-:       |^8   < 
lt;|       `g16 
lt;   <
|   | |>-*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   <
>   ^                      @
葬花如无物 2024-08-15 20:11:04

Haskell,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}

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}
蓝海 2024-08-15 20:11:04

Golfscript - 154

条换行符很重要。如果您未能去掉尾随的换行符,您将在底部打印一个额外的数字! -0 之间有一个 TAB

" "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

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
自此以后,行同陌路 2024-08-15 20:11:04

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

您可以尝试在此处运行它:

://codepad.org/tu4HDqSy" rel="nofollow noreferrer">http://codepad.org/tu4HDqSy (此版本已更改, 对于非高尔夫,是通过 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]);
}

C, 287 characters

#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]);}

(this character count excludes two newlines)

This runs as a command line program, like

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

You can try running it here: http://codepad.org/tu4HDqSy (This version is altered because codepad.org doesn't allow command line arguments.)

A handy tip for un-golfing is to run it through cpp and then 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]);
}
揽月 2024-08-15 20:11:04

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)

Python (2.6), 1092 905 623 501 478 345 --> 318 characters

All comments welcome!

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)
轻许诺言 2024-08-15 20:11:04

VB.NET 9 的 627 字节(不计算新行)

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

627 bytes of VB.NET 9 (without counting new lines)

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
天生の放荡 2024-08-15 20:11:04

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}

(有一个换行符不算在内,它之所以存在,是因为否则那里会有很长的一行。)

我使用了制作二维字符数组的明显方法然后一次一个地粘贴立方体,从下到上,从左到右。

313 bytes of 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}

(There is one newline that doesn't count, it's only there because otherwise there would be a very long line in there.)

I used the obvious method of making a 2D array of characters and pasting in the cubes one at a time, bottom to top and left to right.

丿*梦醉红颜 2024-08-15 20:11:04

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

Lua, 453 characters

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
折戟 2024-08-15 20:11:04

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

PHP - 447 398

I Know it's not as short as the others here, but I'm happy with it ;)

<?$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);?>

Input / Output

   C:\development\code-golf>php cubes1.php 1 2 1 -5 -5 4 3 3
                       __
                     /__ /|
                    |   | |__  __
                    |___|/__ /__ /|
       __           |   |   |   | |
     /__ /|         |___|___|___|/|
   _|   | |__       |   |   |   | |
 /__|___|/__ /|     |___|___|___|/|
|   |   |   | |__  _|   |   |   | |
|___|___|___|/__ /__|___|___|___|/
            |   |   | |
            |___|___|/|
            |   |   | |
            |___|___|/|
            |   |   | |
            |___|___|/|
            |   |   | |
            |___|___|/|
            |   |   | |
            |___|___|/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文