ASCII菱形グリッド


27

次のパラメーターを指定して、バイトカウントで測定した最短コードを記述し、菱形で構成されるASCIIグリッドを生成します。

  • m-1行の完全な菱形の数
  • n-行数
  • s-最小の菱形の側面
  • r-入れ子のレベル-「ベースのもの」(グリッドの交点間の偽物)の中にいくつの菱形があるか

1. Input: 5 3 1 0
Output:

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

A 5x3 grid of rhombi with side 1, no nesting

2. Input: 3 2 2 0
Output:

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

A 3x2 grid of rhombi with side 2, no nesting

3. Input: 5 2 1 2
Output:

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

A 5x2 grid of rhombi with side 1 (the smallest rhombus), level of nesting is 2

4. Input: 4 2 2 1
Output:

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

A 4x2 grid of rhombi with side 2 with level of nesting 1 

5. Input: 4 2 3 3
Output:

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

A 4x2 grid of rhombi with side 3, level of nesting 3

必要に応じて、端と角に部分的に見える菱形を必ず表示してください。


1
@AdmBorkBork「ネスト」とは、グリッドの交差点の間にあるベースの菱形の数を意味します。
ガレンイワノフ


@Luis Mendo-はい、それは明らかに私のタスクに関連しています。
ガレンイワノフ

@Stewie Griffin:codegolf.stackexchange.com/questions/146747/…— 3番目のテストケース
sergiol

5
ああ、どうして枕が恋しいの!
スティーヴィーグリフィン

回答:


17

SOGL V0.12、20のバイト

ā.I∫e+H╚╬8}:±№╬8╬¡∙*

ここで試してみてください! 例にあるものと逆の順序(r、s、n、m)で入力を受け取ります。

説明:

ā                     push an empty array - canvas
 .I∫      }           for each in range(input) (1-indexed)
    e+                  add the second input
      H                 decrement
       ╚                create a diagonal of that size
        ╬8              insert into the canvas
           :          create a duplicate of the canvas
            ±№        reverse it vertically and horizotally
              ╬8      insert that into the canvas
                έ    quad-palindromize
                  ∙   multiply vertically by the next input
                   *  multiply horizontally by the next input

2
これは驚くべきことです。このようなプログラムを20バイトで圧縮する方法もあります。
エリックアウトゴルファー

1
これは素晴らしいです...驚異的な28バイトで炭を打ち負かしたために私から+1:O
Mr. Xcoder

それ 』は本当に印象的です!
ガレンイワノフ

@ Mr.Xcoder 19バイトになりました。しかし、その後、私のような組み込みコマンドに対してアップだ∙*...
ニール

1
チャコールでは、ベクトル化された文字列の繰り返しはかなり短いです。ダイヤモンドの文字列表現を作成できれば、それを繰り返すのに6バイトしかかかりません。残念ながら、それを行うには50バイトかかります。一方、ダイアモンドを描画するのに必要なのは17バイトだけで、SOGLより1つ多いだけですが、キャンバスを乗算するには20バイトかかります。
ニール

8

48 39 37バイト

UO⊕Iε\F⊖IζC¹¦¹‖ML≔⊗⁺IζIεδF⊖NCδ⁰F⊖NC⁰δ

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

UO⊕Iε\

サイズの正方形を描画しますr + 1。これは、サイズ1のネストされたダイヤモンドの1/4です。

F⊖IζC¹¦¹

正方形1を右および下方向にコピーs - 1して、適切なサイズにします。

‖ML

それを反映して、完全にネストされたダイヤモンドになります。

≔⊗⁺IζIεδ

このネストされたダイヤモンドのサイズを計算します。

F⊖NCδ⁰

ダイヤモンドを適切なm - 1タイミングでコピーします。

F⊖NC⁰δ

ダイヤモンドを下方向にコピーしn - 1ます。


8

パイソン2160の 159 158バイト

ジョナサン・フレッシュのおかげで-1バイト

m,n,s,r=input()
l=~-s*' '+'/'*(r-~r)+~-s*' '
for x in range(n*2):print'\n'.join(m*(l[i:i+s+r]+l.replace(*'/\\')[i:i+s+r][::-1])for i in range(r+s))[::1-x%2*2]

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

これは、菱形の下部が上部が反転している([::-1])という事実を使用して、繰り返し処理し、上部または下部のどちらであるかを制御するためにrange(n*2)使用~x%2*2-1します。
上部(および下部)の右側はちょうど左側が反転され、置き換え/られます\-> l.replace(*'/\\')..[::-1]
パターンを生成するために/ネスト~-s*' '+'/'*(1+r*2)+~-s*' 'が使用され、そのような文字列 /// が繰り返され、切り刻まれます:

   '|  //|/  '  
  ' | ///|  '  
 '  |/// | '  
'  /|//  |'  

(1+r*2)できると思います(r-~r)
ジョナサンフレッチ

7

ジュリア0.6、190バイト

g(n,m,s,r)=(s+=r+1;f(i,j,u=mod(i-j+r,2s),v=mod(j+i+r,2s))=(" \\/")[abs(u-r)<abs(v-r)?1+(u<=2r):1+2(v<=2r)];
for i=1:2(s-1)m println((f(i+(i-1)÷(s-1),s+j+(j-1)÷(s-1))for j=1:2(s-1)n)...)end)

これはi,j、シンボルが表示されるべきものをすべてのインデックスペアに対して計算する機能的なソリューションです。

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

グリッドから始める

f(i,j,u=mod(i,2s),v=mod(j,2s))=(" -|*")[1+(u==0)+2(v==0)]
for i=1:2(s-1)m println((f(i-1,j)for j=1:2(s-1)n)...)end

---------*---------*----
         |         |    
         |         |    
         |         |    
         |         |    
         |         |    
         |         |    
         |         |    
         |         |    
         |         |    
---------*---------*----
         |         |    
         |         |    
         |         |    
         |         |    
         |         |    

r > 0 より太い線を意味する

f(i,j,u=mod(i+r,2s),v=mod(j+r,2s))=(" -|*")[1+(u<=2r)+2(v<=2r)]
for i=1:2(s-1)m println((f(i,j)for j=1:2(s-1)n)...)end

**-----*****-----*****--
**-----*****-----*****--
||     |||||     |||||  
||     |||||     |||||  
||     |||||     |||||  
||     |||||     |||||  
||     |||||     |||||  
**-----*****-----*****--
**-----*****-----*****--
**-----*****-----*****--
**-----*****-----*****--
**-----*****-----*****--
||     |||||     |||||  
||     |||||     |||||  
||     |||||     |||||  
||     |||||     ||||| 

元のグリッドのどの線が最も近いかを確認して、角を処理します

f(i,j,u=mod(i+r,2s),v=mod(j+r,2s))=(" -|")[abs(u-r)<abs(v-r)?1+(u<=2r):1+2(v<=2r)]
for i=1:2(s-1)m println((f(i,j)for j=1:2(s-1)n)...)end

|-------|||-------|||---
||-----|||||-----|||||--
||     |||||     |||||  
||     |||||     |||||  
||     |||||     |||||  
||     |||||     |||||  
||     |||||     |||||  
||-----|||||-----|||||--
|-------|||-------|||---
---------|---------|----
|-------|||-------|||---
||-----|||||-----|||||--
||     |||||     |||||  
||     |||||     |||||  
||     |||||     |||||  
||     |||||     |||||  

妖精はすべてのs-1行を削除するように指示します

f(i,j,u=mod(i+r,2s),v=mod(j+r,2s))=(" -|")[abs(u-r)<abs(v-r)?1+(u<=2r):1+2(v<=2r)]
for i=1:2(s-1)m println((f(i+(i-1)÷(s-1),s+j+(j-1)÷(s-1))for j=1:2(s-1)n)...)end

---||------||------||---
--||||----||||----||||--
  ||||    ||||    ||||  
  ||||    ||||    ||||  
  ||||    ||||    ||||  
  ||||    ||||    ||||  
--||||----||||----||||--
---||------||------||---
---||------||------||---
--||||----||||----||||--
  ||||    ||||    ||||  
  ||||    ||||    ||||  
  ||||    ||||    ||||  
  ||||    ||||    ||||  
--||||----||||----||||--
---||------||------||---

斜めにトラバースして完了

f(i,j,u=mod(i-j+r,2s),v=mod(j+i+r,2s))=(" \\/")[abs(u-r)<abs(v-r)?1+(u<=2r):1+2(v<=2r)]
for i=1:2(s-1)m println((f(i+(i-1)÷(s-1),s+j+(j-1)÷(s-1))for j=1:2(s-1)n)...)end

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

3

JavaScript(ES6)、154バイト

f=
(m,n,s,r)=>[...Array((s+=r)*n*2)].map((_,i)=>[...Array(s*m*2)].map((_,j)=>i/s&1^j/s&1?`\\ `[g(j%s)]:`/ `[g(s-1-j%s)],g=j=>i%s-j>r|j-i%s>r).join``).join`
`
<div oninput=o.textContent=f(+m.value,+n.value,+s.value,+r.value)>
m: <input type=number min=0 id=m><br>
n: <input type=number min=0 id=n><br>
s: <input type=number min=0 id=s><br>
r: <input type=number min=0 id=r></div>
<pre id=o>

出力の各セルで文字を直接計算します。


2

CJam、44

q~:R+,_ff{-zR>}{_W%:~\+"\ /"f=}%_W%Wf%+*f*N*

オンラインで試す

説明:

q~           read and evaluate the input
:R+          save the last number in variable R, then add the last 2 numbers (s+r)
,_           make an array [0 1 … s+r-1] and duplicate it
ff{…}        make a matrix, calculating for each (x,y) pair from the 2 arrays:
  -z         abs(x-y)
  R>         compared with R (1 if greater, 0 if not)
{…}%         transform each row of the matrix:
  _W%        duplicate and reverse it
  :~         bitwise-NOT each element (0 → -1, 1 → -2)
  \+         prepend to the original row
  "\ /"f=    replace numbers with characters from this string (by index):
              0 → '\'; 1, -2 → ' '; -1 → '/'
              this generates the "/\" part
_W%          duplicate the matrix and reverse the rows
Wf%+         reverse each row, then append (by rows) to the original matrix
              this adds the "\/" part
*            repeat the matrix (by rows) n times
f*           repeat each row m times
N*           join the rows with newlines

2

C#(.NET Core)、167バイト

(c,r,s,n)=>{int w=s+n,i=0,j;var g="";for(;i<r*2*w;i++,g+="\n")for(j=0;j<c*2*w;){int y=i%w,q=(j/w+i/w+1)%2,p=~-w*q+j++%w*(1-2*q);g+=p>y+n|p<y-n?' ':"\\/"[q];}return g;}

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

私が管理したサイズに驚いています。最初はもっと長い解決策を期待していました。これを言って、私が逃した他のトリックがあると確信しています。

デゴルフド

(c,r,s,n)=>{
    int w=s+n, // rhombus quadrant width, and height
        i=0, // string row
        j; // string column

    var g="";

    // go through every character row and column
    for(; i < r*2*w; i++, g += "\n")
        for(j = 0; j < c*2*w;)
        {
            int y = i % w, // vertical position in the quadrant
                q = ( j / w + i / w + 1) % 2, // Get the rhombus quadrant as a 0 or 1
                p = ~-w * q + j++ % w * (1-2*q); // horizontal position in quadrant. the value is either ascending or descending depending on the quadrant

            // select which character to use at this [i,j] position
            g += p > y + n | p < y - n ? ' ' : "\\/"[q];
        }

    return g;
}

1

パイソン2201の 189バイト

def f(m,n,s,r):Z=range(s+r);R=[r-~min(i,s+~i+r,min(s-1,r))for i in Z];L=[m*(' '*(s+~i)+'/'*R[i]+'  '*(i-r)+'\\'*R[i]+' '*(s+~i))for i in Z];print'\n'.join((L+[l[s+r:]+l[:s+r]for l in L])*n)

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

下部が上部と同じであるが、シフトしているという事実を使用します。

Top:       /\/\/\
Bottom:     /\/\/\

Jonathan Frechのおかげで22バイト節約


s-i-1s+~i2バイトを節約できます。、別のものを保存r+1+min(...できますr-~min(...。1バイトを節約L=[(...)*m forできL=[m*(...)forます。
ジョナサンフレッチ

L+=[...];print'\n'.join(L*n)print'\n'.join((L+[...])*n)別のバイトを保存して、196バイトにすることができます
ジョナサンフレッチ

195バイト、1にも置き換えられた場合s+r-i-1s+~i+r
ジョナサンフレッチ


代替の192バイトバージョンまたは191バイトバージョンを使用execした文字列フォーマット。
ジョナサンフレッチ

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