つまようじシーケンスを生成


10

つまようじシーケンスとは何ですか?

ウィキペディアによると

ジオメトリでは、つまようじのシーケンスは2次元パターンのシーケンスであり、シーケンスの前のパターンに線分(つまようじ)を繰り返し追加することで形成できます。

設計の最初の段階は、単一の「つまようじ」、つまりラインセグメントです。最初のステージの後の各ステージは、前のデザインを採用して形成され、露出した爪楊枝の端ごとに、その端の直角に中心を置く別の爪楊枝を配置します。

このプロセスにより、n段階のセグメント数が0.45n2から0.67n2の間のフラクタルパターンで振動する成長パターンが生じます。T(n)がステージnのセグメント数を表す場合、nが2の累乗に近いときにT(n)/ n2が最大に近いnの値が発生し、最小に近い値は2の累乗の約1.43倍の数の近くで発生します。つまようじのシーケンスの段階の構造は、T平方フラクタル、またはUlam–Warburtonセルオートマトンのセルの配置によく似ています。

パターン内のつまようじで囲まれているが、つまようじで交差していない境界付き領域はすべて、正方形または長方形である必要があります。つまようじパターンのすべての開いた四角形(つまようじで完全に囲まれているが、つまようじが内部を横切っていない四角形)の辺の長さと面積は2の累乗で、辺の長さの1つであると推測されています最大2つです。

仕事

STDIN、関数引数、またはコマンドライン引数から入力を受け取るプログラムまたは関数を作成し、その段階で余計なフラクタルを作成する必要があります。避けられない場合を除き、改行の先頭と末尾は禁止されています。境界ボックスは、先頭と末尾のスペースを含め、最低限必要です。初期ラインの場合\、空間内で2つの対角線を作ります。入力は2000未満であることが保証されています。少なくとも1行にスペース以外の文字が含まれています。後続スペースは許可されます。

テストケース

1
\ 
 \     

5
    \     
    /\    
   /\     
  / /\   
\/\/\ \ \ 
 \ \ \/\/\
    \/ /  
     \/   
    \/    
     \    

回答:


6

CJam、99 93バイト

これはかなり長くなりました...

"\ "_W%]{{Sf+W%z}4*2ew{2fewz{_sa"\//\\"4S**4/^_,3={:.e>W%2/\}&;}%z{\)a@.e>+}:Ff*}%{F}*}q~(*N*

ここでテストしてください。ウィキペディアの89のようなより大きな入力をテストする場合、DennisのTryItOnline内部ではるかに高速なJavaインタープリターを使用し、数秒でそのような入力を処理できます。

まだまだ改善の余地はあると思いますが、楽譜に満足したら説明を追加します...

これはの出力ですN = 13

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

これをさらにゴルフするときの私の参考のために、いくつかの他のアイデア:

"\ "_W%]{{Sf+W%z}4*2few2ew::.{+_a"\//\\"4S**4/^_,3={:.e>W%\}&;2/}:z{\)a@.e>+}ff*{\)a@..e>+}*}ri(*N*
"\ "_W%]{{Sf+W%z}4*2ew{2fewz{_sa"\//\\"4S**4/^_,3={:.e>W%2/\}&;}%{.{\)a@.e>+}}*}%{\)a@.e>+}*}q~(*N*

1

JavaScript(ES6)、263バイト

n=>(o=(o=[..." ".repeat(n*2)]).map(_=>o.map(_=>s=c=" ")),(g=a=>s++<n&&g(q=[],a.map(p=>o[p[4]][p[3]]==c&&(o[y=p[1]][x=p[0]]=o[y-1][(b=+p[2])?x-1:x+1]="/\\"[b],q.push([x,++y,!b,b?x+1:x-1,y],[b?x-=2:x+=2,y-2,!b,x,y-3])))))([[n,n,1,n,n]]),o.map(r=>r.join``).join`
`)

説明

n=>(                           // n = desired stage

  o=                           // o = output grid
                               //     [ [ "\\", " " ], [ " ", "\\" ], etc... ]
    (o=[..." ".repeat(n*2)])   // create an array the size of the grid
    .map(_=>o.map(_=>          // loop over it and return the output grid
      s=                       // s = current stage (" " acts the same as 0)
        c=                     // c = blank character
          " "                  // initialise each element to " "
    )),

  (g=                          // g = compute stage function
    a=>                        // a = positions to place toothpicks
                               //     [ x, y, isBackslash, checkX, checkY ]
      s++<n&&                  // do nothing if we have reached the desired stage
      g(q=[],                  // q = positions for the next stage's toothpicks
        a.map(p=>              // p = current potential toothpick position
          o[p[4]][p[3]]==c&&(  // check the position to make sure it is clear

            o[y=p[1]][x=p[0]]= // place bottom toothpick, x/y = position x/y
            o[y-1][            // place top toothpick
              (b=+p[2])        // b = isBackslash
              ?x-1:x+1         // top toothpick x depends on direction
            ]="/\\"[b],        // set the location to the appropriate character

            // Add the next toothpick positions
            q.push([x,++y,!b,b?x+1:x-1,y],
              [b?x-=2:x+=2,y-2,!b,x,y-3])
          )
        )
      )
  )([[n,n,1,n,n]]),            // place the initial toothpicks
  o.map(r=>r.join``).join`
` // return the grid converted to a string
)

テスト

Stages: <input type="number" oninput='result.innerHTML=(

n=>(o=(o=[..." ".repeat(n*2)]).map(_=>o.map(_=>s=c=" ")),(g=a=>s++<n&&g(q=[],a.map(p=>o[p[4]][p[3]]==c&&(o[y=p[1]][x=p[0]]=o[y-1][(b=+p[2])?x-1:x+1]="/\\"[b],q.push([x,++y,!b,b?x+1:x-1,y],[b?x-=2:x+=2,y-2,!b,x,y-3])))))([[n,n,1,n,n]]),o.map(r=>r.join``).join`
`)

)(+this.value)' /><pre id="result"></pre>


1

Ruby、151バイト

Golfedバージョンでは、唯一のループを使用してj、とiし、kその場で計算します。

->n{m=n*2
s=(' '*m+$/)*m
l=m*m+m
s[l/2+n]=s[l/2-n-2]=?\\
(n*l-l).times{|j|(s[i=j%l]+s[i-m-2+2*k=j/l%2]).sum==124-k*45&&s[i-m-1]=s[i-1+2*k]="/\\"[k]}
s}

テストプログラムに含まれていない

このバージョンは2つのネストされたループを使用します。

めったに使用されない組み込み関数はsum、ASCII文字列のすべてのバイトを追加することによって大まかなチェックサムを返します。

f=->n{
  m=n*2                                       #calculate grid height / width            
  s=(' '*m+$/)*m                              #fill grid with spaces, separated by newlines
  l=m*m+m                                     #calculate length of string s
  s[l/2+n]=s[l/2-n-2]=?\\                     #draw the first toothpick
  (n-1).times{|j|                             #iterate n-1 times
    l.times{|i|                               #for each character in the string
      (s[i]+s[i-m-2+2*k=j%2]).sum==124-k*45&& #if checksum of current character + character diagonally above indicates the end of a toothpick
         s[i-m-1]=s[i-1+2*k]="/\\"[k]         #draw another toothpick at the end
    }                                         
  }
s}                                            #return value = s


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