코드 골프 : 큐브 플레이
도전
사용자 입력에 따라 벽돌 타워 시리즈 재생을 출력하는 문자 수에 따른 가장 짧은 코드입니다.
입력은 인덱스를 따르는 현재 큐브 타워의 높이를 나타내는 일련의 숫자 (양수, 음수 및 0)입니다. 높이가 0이면 타워가없고 간격이 있음을 의미합니다.
큐브 타워는 쌓인 큐브로 구성됩니다. 현재 인덱스의 입력 번호가 양수이면 큐브가 올라가고 입력 번호가 음수이면 큐브가 내려갑니다. 다음 4 개의 선을 사용하여 단일 큐브를 그립니다.
__ / __ / | | | | | ___ | /
큐브는 3D입니다. 즉, 두 개의 타워가 나란히 배치되어 가짜 원근감을 생성 할 때 서로를 숨 깁니다.
모든 입력은 유효하고 오류가없는 것으로 가정 할 수 있습니다.-각 숫자는 하나 이상의 숫자와 함께 한 줄에 공백으로 구분됩니다.
테스트 케이스
입력: 2 -3 -2 1 2 -1 산출: __ __ / __ / | / __ / | | | | _ | | | | ___ | / | / __ | ___ | / | | | | __ _ | | | | __ | ___ | / __ / __ | ___ | ___ | / __ / | | | | | | | | | ___ | ___ | / | | ___ | / | | | | | ___ | ___ | / | | | | ___ | /
입력: 12 34 -2 4 3 2 1 산출: __ __ / __ / | / __ / | _ | | | | | | __ / __ | ___ | / | | ___ | / __ / | _ | | | | | | | | __ / __ | ___ | ___ | / | | ___ | ___ | / __ / | _ | | | | | | | | | | __ / __ | ___ | ___ | ___ | / | | ___ | ___ | ___ | / __ / | | | | | | | _ | | | | | | | ___ | ___ | ___ | ___ | / __ | ___ | ___ | ___ | ___ | / | | | | ___ | / | | | | | ___ | /
입력: 1 3 3 7 0 -2 -2 산출: __ / __ / | | | | | ___ | / | | | | | ___ | / | | | | | ___ | / | __ _ | | | / __ / __ | ___ | / | | | | | | | ___ | ___ | ___ | / | _ | | | | | / __ | ___ | ___ | ___ | / | | | | | | | __ __ | ___ | ___ | ___ | ___ | / / __ / __ / | | | | | | ___ | ___ | / | | | | | | ___ | ___ | /
코드 수에는 입력 / 출력 (예 : 전체 프로그램)이 포함됩니다.
Perl 157 자
이 항목은 코드에 큐브를 포함시키는 부분을 포함하여 gnibbler의 Ruby 항목에서 영감을 받았습니다. 또한 4 arg 버전에 대해 교육 해준 Kinopiko에게 감사드립니다 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 행 : 큐브가가는 위치에 큐브를 놓고 인쇄
루비-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 __ / __ / | | | | | ___ | / | | | | | ___ | / | | | | | ___ | / | _ | | | __ __ / __ | ___ | / __ / __ / | | | | | | | | ___ | ___ | ___ | ___ | / | _ | | | | | | / __ | ___ | ___ | ___ | ___ | / | | | | | | | | | ___ | ___ | ___ | ___ | ___ | /
파이썬-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()
루비
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]]
__ __ __ / __ / | / __ / | / __ / | | | | | | | | | | __ | ___ | / | | ___ | / | | ___ | / __ / | | | | | | | | | | __ | ___ | / | | ___ | / | ___ | / __ / | | | | | | | __ | ___ | / | | ___ | / __ / | | | | __ | | | | ___ | / | / __ / | | ___ | / | | | __ | | | | ___ | / __ / | | ___ | / | | | | | _ | | | | ___ | ___ | / | / __ | ___ | / | | | | | | | | ___ | ___ | / | ___ | /
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 < > ^ @
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}
Golfscript-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
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
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 12 34 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]);
}
Python (2.6),
1092905623501478345-
> 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)
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
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}
(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.
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
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
__
/__ /|
| | |__ __
|___|/__ /__ /|
__ | | | | |
/__ /| |___|___|___|/|
_| | |__ | | | | |
/__|___|/__ /| |___|___|___|/|
| | | | |__ _| | | | |
|___|___|___|/__ /__|___|___|___|/
| | | |
|___|___|/|
| | | |
|___|___|/|
| | | |
|___|___|/|
| | | |
|___|___|/|
| | | |
|___|___|/
참고URL : https://stackoverflow.com/questions/1609702/code-golf-playing-cubes
'UFO ET IT' 카테고리의 다른 글
VS2015 : 경고 MSB3884 : 규칙 집합 파일을 찾을 수 없습니다. (0) | 2020.11.30 |
---|---|
NSNumber와 NSInteger의 차이점은 무엇입니까? (0) | 2020.11.30 |
Mysql 열을 재정렬하는 방법은 무엇입니까? (0) | 2020.11.30 |
Rspec 테스트 redirect_to : back (0) | 2020.11.30 |
System.String.Empty를 덮어 쓰도록 Reflection API에 요청하는 의미는 무엇입니까? (0) | 2020.11.30 |