アスタリスクスパイラル


29

サイズSとステップのスパイラルが与えられた場合、外側の半径から内側の半径まで時計回りに構築されたアスタリスクを持つN「正方形」のS*Sスパイラルを出力しNます。

以下のテストケース(例)。

  1. 入力: 4 3

    出力:

    ***
    
  2. 入力: 4 6

    出力:

    ****
       *
       *
    
  3. 入力: 4 11

    出力:

    ****
       *
    *  *
    ****
    
  4. 入力: 6 18

    出力:

    ******
         *
         *
    *    *
    *    *
    ******
    
  5. 入力: 6 22

    出力:

    ******
    ***  *
    *    *
    *    *
    *    *
    ******
    
  6. 入力: 6 27

    出力:

    ******
    ******
    *   **
    *   **
    *   **
    ******
    
  7. 入力: 1 1

    出力:

    *
    

次の場合にケースを処理する必要はありません。

  • 与えられたNアスタリスクは、与えられたS*S次元のスパイラルに「適合」できません。

  • いずれかNまたはSゼロです。

課題はコードゴルフです。最短バイトで回答が得られ、あらゆる言語を使用できます。

出力には、必要に応じて末尾(ただし先頭ではない)のスペース/改行を含めることができます。


後続のスペース/改行を使用できますか?
user202729

2
半径ではなくS サイズ(または少なくとも直径)を呼び出す
ルイスメンドー

@ルイスフェアポイント!
ニカエル

3
親愛なる友人、質問だけでなく、答えにも投票してください。この挑戦をするのは簡単です。それに対する答えを提供することは、間違いなくもっと難しいです。
ニカエル

2
あなただけがそう思う。好評で明確な課題を書くことは非常に困難です。(ここのコメントスレッドを見てください。チャレンジが投稿された後にいくつかの提案があります)
user202729

回答:


16

MATL17 16バイト

UGlYLGoQ&P->42*c

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

説明(例付き)

入力4を検討11し、例として。

U       % Implicit input: S. Push S^2
        % STACK: 16
G       % Push S again
        % STACK: 16, 4
lYL     % Outward, clockwise, east-first spiral of that size
        % STACK: 16,
                 [ 7  8  9 10;
                   6  1  2 11;
                   5  4  3 12;
                  16 15 14 13]
GoQ     % Push S, compute parity, add 1. Gives 1 for even S, 2 for odd
        % STACK: 16,
                 [ 7  8  9 10;
                   6  1  2 11;
                   5  4  3 12;
                  16 15 14 13],
                 1
&P      % Flip along that dimension (1 is vertical, 2 is horizontal).
        % This corrects for the orientation of the spiral
        % STACK: 16,
                 [16 15 14 13;
                   5  4  3 12;
                   6  1  2 11;
                   7  8  9 10]
-       % Subtract, element-wise. The upper-left corner becomes 0
        % STACK: [ 0  1  2  3
                  11 12 13  4
                  10 15 14  5
                   9  8  7  6]
>       % Implicit input (below): N. Greater than?, element-wise.
        % This transforms the first N entries, starting from
        % upper-left, inward, east-first, into 1, and the rest
        % into 0
        % STACK: [1 1 1 1;
                  0 0 0 1;
                  1 0 0 1;
                  1 1 1 1]
42*     % Multiply each entry by 42
        % STACK: [42 42 42 42;
                   0  0  0 42;
                  42  0  0 42;
                  42 42 42 42]
c       % Convert to char. Char 0 will be displayed as space.
        % Implicit display
        % STACK: ['****';
                  '   *';
                  '*  *';
                  '****']

1
うわー、私はhere'reは:)私の感想ゴルフが得意ではなかったが、17のバイトとそれを解決する...魔法のように見えますが:)(私はおそらく短い答えが来ていることを知っていることが、あなたはしている第一および
nicael

1
ジョブの一部は、組み込みのスパイラル機能によって実行されます。説明を追加しました
ルイスメンドー

@nicael特定の目的を目的としたゴルフ言語の世界へようこそ。:)
エリックアウトゴルファー

3
説明と一緒に完全な例の+1
IanF1


6

スタックス、19 バイト

±♪☺ÿzMæ¡♠à╣♂7☼V♀§9↓

実行してデバッグする

まず、結果に含まれるすべての文字を左揃えで並べた文字列を作成します。次に、文字列の端から次第に大きなスライスを取り出し、グリッドを回転させるときにそれらをグリッドに「ラップ」します。

これは同じプログラムで、アンパック、アンゴルフ、コメント付きです。

'**     repeat "*" specified number of times
,J(     square the top of the input stack, and right-pad string to that length
z       push an empty array - this is the result grid built up in the loop
{       begin a block to loop
  ~     push grid to the input stack
  ihNv  push -(i / 2) - 1 where i is the 0-based iteration index using integer division
  :/]   split the string at that index and wrap the second half in a singleton array
  ,     pop the grid from the input stack
  rM+   rotate the grid clockwise, then prepend the split string as the new first row
  n     copy what's left of the original string to top of stack for the loop condition
w       while; execute block until condition is truthy
m       display resulting grid

実行してデバッグする


2
アンドロイドでは、この回答にオレンジ色のブロブスマイリーが含まれていることに、私は非常に興味をそそられます。
StarWeaver

@StarWeaver Staxにはそうする多くの答えがあります。
ウェイジュン周

説明を読んで見たとき、私は本当に混乱しました。Staxには本当に奇妙なコードページがあると思っただけです。
ndm13

@ ndm13:奇妙なコードページがあると思います。CP437から派生します。CP437は、同じ文字を含む「実際の」エンコーディングです。携帯電話でそのリンクをたどると、同じ笑顔が見えるはずです。
再帰的


4

APL(Dyalog)、65バイト

' *'[1+⎕>⊖∘⌽⍣o(⊖×⍨-,⍨⍴∘(⍋+\)×⍨↑(⌈2÷⍨×⍨),(+⍨⍴1,⊢,¯1,-)(/⍨)2/⍳)o←⎕]

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

スパイラルマトリックスのコードは、私の別の回答から取得したものです。


コードNが奇数の場合、間違った方向にスパイラルを描画します:)
nicael

@nicaelが修正されました(パッチのようなものです)。感謝
ウリエル


たぶん、間違った方法で入力を使用していますか?
ニカエル

@nicael arghh。OK、今は大丈夫だと思う。
ウリエル



3

、34バイト

NθFE⮌E⊗N∨ι¹÷⁺鬬겫F‹θι≔θι×ι*≧⁻ιθ↷

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

Nθ

入力N

FE⮌E⊗N∨ι¹÷⁺鬬겫

(隅を除く)スパイラルアームの長さがありS-1S-1S-1S-2S-2S-3、...、 、3221、。1 1これは、0から1 0を除く範囲で始まり2S、0から1に変更し、最初の要素の後の各要素に1を追加し、最後にすべての要素を2で割った整数で形成されます。このリストはループされます。

F‹θι≔θι

描画する星の数が次の腕の長さより少ない場合は、腕をその長さに減らします。

×ι*

適切な数の星を描きます。

≧⁻ιθ

残りの星の数から引きます。

描画方向を時計回りに90度回転します。


3

J、60 56バイト

-4バイト。スパイラルのビルドプロセスを変更して、y ^ 2から減算する必要がなかった。

4 :'''* ''{~x<|."1|.(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y'

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

説明は近日公開です。

説明:

4 :'''* ''{~x<|."1|.(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y'  | Explicit dyad definition
                    (|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y   | Generate a y by y inward spiral
                                                  ,.*:y   | The matrix [[y^2]]
                    (                   )^:(+:<:y)        | 2*(y-1) times...
                     |:@|.                                | Rotate
                          ,                               | Append
                                    i.@#                  | [0..len(n)-1]
                           <:@{:@{:-                      | Subtracted from the previous value and decremented
              |."1|.                                      | Flip around antidiagonal
            x>                                            | Test if each entry is less than x
    '' *''{~                                              | ' ' for 0, '*' for 1

例:

   3 :'(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y' 4
7  8  9 10
6 15 16 11
5 14 13 12
4  3  2  1
   3 :'|."1|.(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y' 4
1  2  3 4
12 13 14 5
11 16 15 6
10  9  8 7
   11(4 :'x<|."1|.(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y') 4
0 0 0 0
1 1 1 0
0 1 1 0
0 0 0 0
   11(4 :'''* ''{~x<|."1|.(|:@|.,<:@{:@{:-i.@#)^:(+:<:y),.*:y') 4
****
   *
*  *
****

実行可能な例へのリンクも追加できますか?
ニカエル

@nicaelが追加されました:)
ボルチェ・ビュシエール

2

Kotlin361 355 353 334バイト

6は、ジョナサンのおかげで保存されたバイト
場合2バイトに変化保存
19のバイトは、ラムダへの切り替え&外縁を追跡保存しました

{s:Int,n:Int->var a=Array(s,{_->Array(s,{_->' '})})
var r=0
var c=0
var d=0
var e=0
var f=1
var g=s-1
var h=g
for(i in 1..n){a[r][c]='*'
when(d){0->if(c<g)c++
else{d=1
r++
g--}
1->if(r<h)r++
else{d=2
c--
h--}
2->if(c>e)c--
else{d=3
r--
e++}
3->if(r>f)r--
else{d=0
c++
f++}}}
for(i in 0..s-1){for(j in 0..s-1)print(a[i][j])
println()}}

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


1
入力フィールドが空なので、どうやって試すかわかりません。
ニカエル

1
@nicaelこれは関数です。これは使いやすいかもしれません-呼び出しはフッターで行われます。
ジョナサンアラン

1
Kotlinについてはまったく知りません==' 'が、に置き換えられるかもしれません<'*'。またd==0d<1してd==3d>2。これらはかなり基本的なゴルフのように見えるので、おそらく他にもあります!
ジョナサンアラン

@nicaelでは、入力フィールドに2つの整数、最初の行にサイズ、2番目に数値を入力できます。
ジョンウェルズ

1
@JohnWellsは確かに機能します。どういうわけか遅すぎますが、それは問題ではありません。
ニカエル

2

Java 10、284 282 281 263バイト

s->n->{var c=new char[s][s];for(var d:c)java.util.Arrays.fill(d,' ');for(int i=0,j=0,y=0,x=1,u=s-1,l=0;n-->0;c[j][i]=42,i+=x,j+=y,l+=i==l&x==0?1:0,u-=i==l&j==l&y<1?1:0)if(x!=0){var b=x>0?i<u:i>l;y=b?0:x;x=b?x:0;}else{var b=y>0?j<u:j>l;x=b?0:-y;y=b?y:0;}return c;}

楽しいチャレンジ!

こちらからオンラインでお試しください。

18バイトのゴルフをしてくれたKevin Cruijssenに感謝します。

ゴルフされていないバージョン:

s -> n -> { // lambda taking two integer arguments in currying syntax
    var c = new char[s][s]; // the matrix containing the spiral
    for(var d : c) // for every row
        java.util.Arrays.fill(d, ' '); // fill it with spaces
    for(int i = 0, j = 0, // the coordinates of the next '*'
            y = 0, x = 1, // the direction to move in
            u = s-1, l = 0; // the upper and lower bounds
        n-- > 0; // decrecement the length of the spiral and repeat as many times
        c[j][i] = 42, // draw the '*', 42 is ASCII code
        i += x, j += y, // move to the next cell
        l += i == l & x == 0 ? 1 : 0, // adjust lower bound if necessary
        u -= i == l & j == l & y < 1 ? 1 : 0) // adjust upper bound if necessary
        if(x != 0) { // if moving in x direction
            var b = x > 0 ? i < u : i > l; // if we hit the bounds
            y = b ? 0 : x; // flip directions,
            x = b ? x : 0; // turning around
        } else { // if moving in y direction
            var b = y > 0 ? j < u : j > l; // if we hit the bounds
            x = b ? 0 : -y; // flip directions,
            y = b ? y : 0;  // turning around
        }
    return c; // return the matrix
}

263バイト最後の2つのループは主に変更され、a var bが追加されるため、各2回ではなくx>0?i<u:i>ly>0?j<u:j>l1回と1回だけを行う必要があります。
ケビンクルーッセン

@KevinCruijssen素晴らしいゴルフ、ありがとう!
OOBalance

2

JavaScriptの(Node.jsの)167の 164 163バイト

  • @Erik the Outgolferと@nicaelのおかげでスペース(3バイト)
  • joinの@michaのおかげで,、マップの代わりに分割(1バイト)
(l,s)=>{a=(b=[...Array(l)]).map(x=>b.map(_=>" "))
for(d=1,x=y=D=0;s--;x+=d,y+=D)a[y][x]="*",(a[y+D]||[])[x+d]!=" "?[d,D]=[-D,d]:0
return a.join`
`.split`,`.join``}

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


1
うまくいきました!スペース/改行を削除してさらに短くすることはできますか?
ニカエル


1
綺麗な!KotlinとJavaバージョンが同じメソッドを使用する場合、それらははるかに短くなります!スパイラルまたは境界に当たって「亀」を回したときを検出するこのようなエレガントな方法。非常に賢い!1バイト少ない:リターンをに変更しreturn a.join` `.split`,`.join``ます。
ミカ

@michaまず最初にありがとう:)。2番目のa.join` .split、 `.join``は、スパイラルを(新しい行で)"うまく "出力しないので、問題だと思います
DanielIndie

@DanielIndie、改行はフォーマットされ、最初の結合には改行が必要です。それを見て
micha
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.