前任者が完成したアスキーキューブ


34

前任者が完成した最初のアスキーキューブ(PAC 1)は、辺の長さが1の単純なキューブで、次のようになります。

 /////\
/////  \
\\\\\  /
 \\\\\/

PAC 2は、その前身(とそれを組み合わせるような幾何学的形状である PAC 1する)側の長さ2の立方体を完了します:

      front                back
   /////////\           /////////\
  /////////  \         /////////  \
 /////\\\\\   \       /////////    \
/////  \\\\\   \     /////////      \
\\\\\  /////   /     \\\\\\\\\      /
 \\\\\/////   /       \\\\\\\\\    /
  \\\\\\\\\  /         \\\\\\\\\  /
   \\\\\\\\\/           \\\\\\\\\/

ので、バック -viewは一種退屈であり、我々は唯一に興味のあるフロント -view。

同じことがPAC 3にも当てはまります。いくつかの視覚的思考により、PAC 2を向きを変えてPAC 3に差し込んで、長辺3の立方体を形成できます。

     /////////////\
    /////////////  \
   /////\\\\\\\\\   \
  /////  \\\\\\\\\   \
 /////   /////\\\\\   \
/////   /////  \\\\\   \
\\\\\   \\\\\  /////   /
 \\\\\   \\\\\/////   /
  \\\\\  /////////   /
   \\\\\/////////   /
    \\\\\\\\\\\\\  /
     \\\\\\\\\\\\\/

PAC 4でも同様です。

       /////////////////\
      /////////////////  \
     /////\\\\\\\\\\\\\   \
    /////  \\\\\\\\\\\\\   \
   /////   /////////\\\\\   \
  /////   /////////  \\\\\   \
 /////   /////\\\\\   \\\\\   \
/////   /////  \\\\\   \\\\\   \
\\\\\   \\\\\  /////   /////   /
 \\\\\   \\\\\/////   /////   / 
  \\\\\   \\\\\\\\\  /////   /
   \\\\\   \\\\\\\\\/////   /
    \\\\\  /////////////   /
     \\\\\/////////////   /
      \\\\\\\\\\\\\\\\\  /
       \\\\\\\\\\\\\\\\\/

仕事:

入力として正の整数nを取り、上記のとおり正確にPAC nの対応するフロントビューを返すか出力する完全なプログラムまたは関数を作成します。追加の末尾の空白は許容されます。

これはなので、選択した言語でできるだけ少ないバイトを使用するようにしてください。


左右反転した出力、つまり左右を切り替えて印刷しても大丈夫ですか?
busukxuan

いいえ、「照明」の一貫性を保つことは課題の一部です。
ライコニ

通常、コードゴルフの質問に対する報奨金は、競争と回答のゴルフを奨励するために最短の回答になりますが、実際には強制することはできません。
mbomb007

回答:


11

JavaScript(ES6)、229バイト

f=
(n,l=n*4+1,r=1,i=n*2,g=a=>` / \\ /
`.replace(/./g,c=>c.repeat(a.pop())))=>n?g([r,,l,--i])+g([r,2,l,--i])+f(n-1,l-1?1:r-4,r-1?1:l-4).replace(/(\S).*(.)/g,r-1?`$1   $&$2$2$2$2`:`$1$1$1$1$&   $2`)+g([r,2,l,,,i])+g([r,,l,,,i+1]):``
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>


4

バッチ、559 432 400バイト

@echo off
set m=
for /l %%i in (1,1,%1)do call set m=  %%m%%
call:c "%m:~2%" %m: =//%/\
exit/b
:c
setlocal
set m=%~1%2%~3
echo  %m%
echo %m:/\=/  \%
set s=%~1
if "%s:~,1%"=="/" goto g
set t=%2
set t=%t:~3,-3%
if %t:~,1%==/ (call:c "%s:~2%////" /\%t:/=\% "   \%~3")else call:c "%s:~2%/   " %t:\=/%/\ "\\\\%~3"
:g
set m=%m:/=-%
set m=%m:\=/%
set m=%m:-=\%
echo %m:\/=\  /%
echo  %m%

説明:キューブの下半分は、上半分を反映して描画されます。上半分を示すこの図のように、半分はさらに7つの 6つの3つのストリップ部分に分割されます。

1       12/////////////////\233
1      12/////////////////  \233
1     ////12/\\\\\\\\\\\\\23   \3
1    ////12/  \\\\\\\\\\\\\23   \3
1   /////   12/////////\23\\\\   \3
1  /////   12/////////  \23\\\\   \3
1 /////   ////12/\\\\\23   \\\\\   \3
1/////   ////12/  \\\\\23   \\\\\   \3
  1. インデント(すべての行が減少する)および左の三角形(1行おきに増加​​する)
  2. 中央に縮小するジグザグ三角形、1行おきに交互に並んだ小さな三角形
  3. 左と同期して増加する直角三角形

編集:上半分から下半分を反映するコードを改善することにより、20%以上節約しました。左の2つのストリップと中央の3つのストリップをマージすることにより、ほぼ10%節約されました。


4

キャンバス、36 バイト

;Xø;{1x/╋
ø╶{«5⁸2²4×⁸²«1╋²«\+;↔53╋}═

ここで試してみてください!


SOGLのASCIIアートバージョンは、ASCIIアートチャレンジでSOGLによってアウトゴルフされましたか?
ディルナン

@dylnan Canvasには、これを少しゴルフすることができる対角線スペースプリペンドが組み込まれていませんでした。チャレンジにビルトインを追加するのが適切ではないと感じた
-dzaima

とった。ちょっと興味があるんだけど。まだ2つのクールな言語
ディルナン

4

SOGL V0.12、32のバイト

ø.∫4*I└*2∙f«5└*∙+¼F«╝┼;↔±53╬5}╬±

ここで試してみてください!

簡単な説明:
1. 1..xのそれぞれに対して繰り返します。2
. i*4+1と高さ= (0-indexed)i // 2
3の
形状を作成します。4。「\」形状を水平に追加します
。5.前のステップを挿入します。その逆6。

完全なプログラム:

ø                                 push an empty string - starting stage
 .∫                          }    iterate over 1..input
   4*                               multiply the 1-indexed counter by 4
     I                              and add 1
      └*                            repeat "/" that many times
        2∙                          and repeat vertically twice. Top 2 lines are done
          f«                        push the 0-indexed counter floor divided by 2
            5└*                     push "/" repeated 5 times
               ∙                    and repeat that vertically by the result of the "f«"
                +                   add vertically, creating a "⌐" shape
                 ¼                  prepend each next line with one less space than the above
                  F«                push 1-indexed counter floor divided by 2
                    ╝               create a "\" diagonal that long
                     ┼              and append it horizontally. Shell is done of this hexagon
                      ;↔±           get the previous item on the stack and reverse it horizontally
                         53╬5       and at [5;3] insert the previous result in this
                              ╬±  once everything's done, mirror vertically to finish the hexagon

2

ハスケル232の 227 224 187 183 180 175バイト

m '/'='\\'
m '\\'='/'
m c=c
r=reverse
k=map
n?s=('/'<$[1..4*n])++s
f 0=[]
f n=n?"/\\":n?"/  \\":[1?k m(r s)++"   \\"|s<-f$n-1]
s="":k(' ':)s
((++).r<*>k(k m)).zipWith(++)s.r.f

最後の行の匿名関数は整数の引数を取り、そのサイズの立方体に対して印刷される行を生成します。

アイデアは、再帰を使用して、小さいキューブから大きいキューブを描画することです。サイズ1の立方体の上半分を見てみましょう。次に、サイズ2の立方体の上半分を、前半分をミラーリングして取得し、その周りにスラッシュとスペースの固定パターンを追加します。

                                         ///////// \
                                        ///////// \
     ///// \ ==> / \\\\\ ==> //// / \\\\\    \
    ///// \ / \\\\\ //// / \\\\\    \

したがって、サイズnの立方体を描くアルゴリズムは

  1. サイズn-1の上半分のキューブのラインを取得します。
  2. 各行を(/sと\sを反転させて)ミラーリングし、パッド//// \その周りに配置します。
  3. パターン////nプラス/\とを2行追加し/ \ます。
  4. 結果のラインをミラーリングして、完全なキューブを取得します。
  5. 行を適切な数のスペースで埋めます。

3
無名関数が許可されているため、をドロップできますg=(\l->r l++k(k m)l)はと同じですがliftM2 (++) r (k(k m))、これも短縮できます(++).r<*>k(k m)オンラインでお試しください!
ライコニ

2

ルビー174167169167バイト

->n{a=(1..m=n*4).map{' '*m}
(-n..0).map{|i|(-2*i).times{|k|a[y=k+2*j=n+i][m+~k+i*2,2+k*2-s=4*i]=?/*(1-~j%2*s)+'  '*k+?\\*(1-j%2*s)
a[~y]=a[y].tr('/\\','\\\\/')}}
a*$/}

オンラインでお試しください!

n*4スペースで満たされた文字列の配列を作成し、連続して小さいキューブで上書きします。

コメント付きコード

->n{a=(1..m=n*4).map{' '*m}                 #make an array of n*4 strings of n*4 spaces (up to centreline of final output)
  (-n..0).map{|i|                           #iterate through cube sizes from n down to 0 (i is negative -n to 0)
    (-2*i).times{|k|                        #iterate through the number of lines in the top half of the cube
      a[y=k+2*j=n+i][m+~k+i*2,2+k*2-s=4*i]= #overwrite the correct part of the correct line
      ?/*(1-~j%2*s)+'  '*k+?\\*(1-j%2*s)    #with the correct string of the form "/spaces\" with an additional -4*i symbols on one side
      a[~y]=a[y].tr('/\\','\\\\/')          #copy the current line from top half into bottom half, subsituting / for \ and vice versa
    }
  }
a*$/}                                       #return the elements of the array a, concatenated with newlines $/

2

パイソン2254の 234 226 203 201 199バイト

ついにサブ200!

P=[];n=0
M=lambda r:(u''+r).translate({47:92,92:47})
exec r"n+=1;q='/'*4;P=[q*n+'/\\',q*n+'/  \\']+[q+'%s   \\'%M(r[::-1])for r in P];"*input()
print'\n'.join(l.center(8*n)for l in(P+map(M,P[::-1])))

トリック:

この機能は、すべてのスワップに使用されている\/し、その逆
AはPython2に長いビット-ユニコードでのみ動作
チェックしてください。このことがどのように動作するかのために

M=lambda r:(u''+r).translate({47:92,92:47})

反復ごとに新しい上位2行を生成する
今のところ、以前の反復からこの行を受け取るコンパクトな方法は見つかりません

P=[q*n+'/\\',q*n+'/  \\']

前の反復のすべての行を反転し、スラッシュを交換します

[q+'%s   \\'%M(r[::-1])for r in P]

上半分をコピーし、行で反転し、スラッシュを交換します

P+map(M,P[::-1])

センターパディング文字列のためのきちんとした方法

l.center(8*n)

オンラインでお試しください!


1

スタックス、36 バイト

äª.$u>↓Z╙╝B↨EXª¡╜?Xáhç∙╩p/♂ù⌠r↨c⌐f/*

実行してデバッグする

このアプローチは、出力の上半分を繰り返し構築します。メインブロックを指定された回数実行します。そのブロックでは、各行がミラーリングされ、プレフィックスとサフィックスが追加されています。上の2つの新しい行は個別に追加されます。すべての行が作成されると、それらは中央に配置され、下部が垂直にミラーリングされます。

以下にプログラムを展開し、アンゴルフし、コメントを追加します。

{               begin block to repeat
  iHYH^H'\*     make a string of '\' characters and set the Y register
                  pseudo-code: push(((y = (i * 2)) * 2 + 1) * 2 * '\')
                  given the 0-based iteration index `i`, let y = 2 * i
                  and push a string consisting of (i*8 + 2) backslashes
  2M            split the string into to equal size (i*4 + 1) substrings
  ~+            push array to input stack and concatenate
                  on the first iteration, this is a no-op
                  subsequently, it prepends the array to the result so far
  {             begin a block to use for mapping
    4'\*+       append 4 backslashes to this element
    :R          "brace-wise" reverse
                  this reverses the string AND the slashes in it
    |YH3+92&    write 92 to index (++y * 2 + 3) in the array
                  this puts the trailing backslash at the correct position
                  this will be an out of bounds index, so the string will grow
                  92 is the character code for backslash 
  m             execute map using block
}*              repeat block specified number of times
|C              vertically center all rows
{               begin block for output mapping
  Q             output this line without popping
  :Rr           "brace-wise" reverse, and then actually reverse
                  net effect is to swap forward and backward slashes
m               execute map using block
rm              reverse array, and output all rows

これを実行する


1

Haskell、193バイト

勝者よりも長いが、アプローチが面白いかもしれ-用途にもcos及びpi:)

コード:

i s e f|max e f>s=""|max e f<s=" "|e==s="\\"|1<2="/"
w n y x=head$do{d<-[1..n];o<-[-2*d..2*d];z<-[(cos(pi*(n-d)))*o+x];i(2*d)(abs$z-y)(abs$z+y-1)}++" "
n!h=h<$>[1-2*n..2*n]
f n=((2*n)!)<$>n!w n

次のように実行します。

mapM_ putStrLn (f 4)

このプログラムは、基本的にこのような多くのダイヤモンドを「描画」します。

------------------------
------------------------
-----------/\-----------
----------/  \----------
---------/    \---------
--------/      \--------
--------\      /--------
---------\    /---------
----------\  /----------
-----------\/-----------
------------------------
------------------------

この関数はi s e fサイズの1個のダイヤモンド「描く」se, fですが(abs$z-y)(abs$z+y-1)

関数wは、描画されたダイヤモンドi を正しい場所に移動します。headその定義で使用されているのは、最上位のレイヤーのみを表示することです。

ここで試してみてください


1
誰かがコードを短くする方法を考えているのでしょうか?
ラデク

0

、42バイト

FN«↗×⊗⊕ι/↓↘G↖²→⊕×⁴⊕ι↘²\G←⁴↘⊕⊗ι→⁴\M⁴→‖T»‖M↓

オンラインでお試しください!リンクは、コードの詳細バージョンです。説明:

FN«

キューブを最小から最大まで描画します。(最大から最小に描画すると、奇数の鏡像になり、修正するにはバイトが多すぎます。)

↗×⊗⊕ι/

行を印刷する /sのます。(これは\右側のsになりますが、ループの終わりでミラーリングする方がゴルファーなので、図面はミラーリングされます。)

↓↘G↖²→⊕×⁴⊕ι↘²\

の上の2行を印刷します \sのます。(\1つのポリゴンにすべてのsを描画すると、カーソルが厄介な位置になってしまい、修正するにはバイトが多すぎます。)

G←⁴↘⊕⊗ι→⁴\

の左4行を印刷します \sのます。(5番目の行は前のキューブからのものです。)

M⁴→

次のキューブの先頭に移動します。

‖T»

次のキューブに備えて水平に反射します。

‖M↓

キューブを完成させるためにすべてを垂直にミラーリングします。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.