スクエア、ダイヤモンド、スクエア、ダイヤモンド


41

仕事

小文字のASCII文字a-の空でない文字列が与えられた場合、z最初の文字を取り、そして:

  • 2番目のキャラクターのコピーの正方形でそれを囲み、
  • それを3番目のキャラクターのコピーのひし形で囲み、
  • 4番目の文字のコピーの正方形でそれを囲みます…

…文字列の終わりまで正方形とダイヤモンドの境界を交互に切り替えます。ボーダー間の空のスペースは、ASCIIスペース()で表す必要があります。

正方形の境界線を追加するには、現在の「作業配列」全体を正確に囲むように正方形を描きます。

                                               sssssssssss
                                t              s    t    s
                               t t             s   t t   s
                              t   t            s  t   t  s
                             t aaa t           s t aaa t s
                            t  aca  t    =>    st  aca  ts
                             t aaa t           s t aaa t s
                              t   t            s  t   t  s
                               t t             s   t t   s
                                t              s    t    s
                                               sssssssssss

ダイヤモンドの境界線を追加するには、最も外側の正方形に斜めに接触するが、直交しない中心に配置されたダイヤモンド形状を描画します。

                                                     s            
                                                    s s           
                                                   s   s          
                                                  s     s         
                                                 s       s        
                                                s         s       
                                               s           s      
                    wwwwwwwwwww               s wwwwwwwwwww s     
                    w    o    w              s  w    o    w  s    
                    w   o o   w             s   w   o o   w   s   
                    w  o   o  w            s    w  o   o  w    s  
                    w o eee o w           s     w o eee o w     s 
                    wo  eme  ow    =>    s      wo  eme  ow      s
                    w o eee o w           s     w o eee o w     s 
                    w  o   o  w            s    w  o   o  w    s  
                    w   o o   w             s   w   o o   w   s   
                    w    o    w              s  w    o    w  s    
                    wwwwwwwwwww               s wwwwwwwwwww s     
                                               s           s      
                                                s         s       
                                                 s       s        
                                                  s     s         
                                                   s   s          
                                                    s s           
                                                     s            

プログラムは最終配列を出力する必要があります。

  • 各行には、任意の量の末尾スペースを含めることができます。
  • 行を表す文字列のリスト、またはオプションの末尾の改行で改行で区切られた単一の文字列を出力できます。
  • 空白行の先頭/末尾は許可されません。
  • スペースの先頭の列も許可されません。

これはです。バイト単位の最短コードが優先されます。

テストケース

上記の例は、出力のために期待されているcatcatsmeow、およびmeows、順序を読んでいます。処理する価値のある他のケース:

  • 入力aでは、プログラムは次を出力する必要があります。

    a
    
  • 入力abでは、プログラムは次を出力する必要があります。

    bbb
    bab
    bbb
    
  • 入力codegolfでは、プログラムは次を出力する必要があります。

    fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    f                            l                            f
    f                           l l                           f
    f                          l   l                          f
    f                         l     l                         f
    f                        l       l                        f
    f                       l         l                       f
    f                      l           l                      f
    f                     l             l                     f
    f                    l               l                    f
    f                   l                 l                   f
    f                  l                   l                  f
    f                 l                     l                 f
    f                l                       l                f
    f               l                         l               f
    f              l                           l              f
    f             l ooooooooooooooooooooooooooo l             f
    f            l  o            g            o  l            f
    f           l   o           g g           o   l           f
    f          l    o          g   g          o    l          f
    f         l     o         g     g         o     l         f
    f        l      o        g       g        o      l        f
    f       l       o       g         g       o       l       f
    f      l        o      g           g      o        l      f
    f     l         o     g eeeeeeeeeee g     o         l     f
    f    l          o    g  e    d    e  g    o          l    f
    f   l           o   g   e   d d   e   g   o           l   f
    f  l            o  g    e  d   d  e    g  o            l  f
    f l             o g     e d ooo d e     g o             l f
    fl              og      ed  oco  de      go              lf
    f l             o g     e d ooo d e     g o             l f
    f  l            o  g    e  d   d  e    g  o            l  f
    f   l           o   g   e   d d   e   g   o           l   f
    f    l          o    g  e    d    e  g    o          l    f
    f     l         o     g eeeeeeeeeee g     o         l     f
    f      l        o      g           g      o        l      f
    f       l       o       g         g       o       l       f
    f        l      o        g       g        o      l        f
    f         l     o         g     g         o     l         f
    f          l    o          g   g          o    l          f
    f           l   o           g g           o   l           f
    f            l  o            g            o  l            f
    f             l ooooooooooooooooooooooooooo l             f
    f              l                           l              f
    f               l                         l               f
    f                l                       l                f
    f                 l                     l                 f
    f                  l                   l                  f
    f                   l                 l                   f
    f                    l               l                    f
    f                     l             l                     f
    f                      l           l                      f
    f                       l         l                       f
    f                        l       l                        f
    f                         l     l                         f
    f                          l   l                          f
    f                           l l                           f
    f                            l                            f
    fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    

リファレンス実装

プログラムの出力が(少なくとも視覚的に)一致するはずのジュリアコードをいくつか書きました。オンラインでお試しください!

回答:


5

MATL、63 54バイト

l&)"X@o?TT@I$Ya}t&n2/Xkw2+-|thYa9Mqt_w&:|&+tleXM=@*+]c

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

説明

            % Implicitly grab the input as a string
l&)         % Separate the first letter from the rest and push them onto the stack
"           % For every letter in the string besides the first
  X@o?      % If this is an odd iteration through the loop
    TT      % Push the array [1, 1]
    @       % Get current character
    I$Ya    % Pad the matrix with 1 copy of this character on all sides
  }         % Else (this is a diamond)
    t       % Duplicate the matrix
    &n      % Determine the number of rows and columns (push separately to stack)
    2/Xk    % Compute the radius (half the # of columns rounded up)
    w2+     % Flip the stack to get the # of rows and add 2. This is the new radius.
    -|      % Subtract the new and old radii and take the absolute value
    thYa    % Apply the necessary number of padding zeros to the matrix
    9Mqt_w&:% Generate the array [-radius ... 0 ... radius]
    |&+     % Take the absolute value and compute the element-wise sum with itself
            % and automatically broadcast to a matrix. For a radius of 5 this yields
            % the following:
            %
            %   8   7   6   5   4   5   6   7   8
            %   7   6   5   4   3   4   5   6   7
            %   6   5   4   3   2   3   4   5   6
            %   5   4   3   2   1   2   3   4   5
            %   4   3   2   1   0   1   2   3   4
            %   5   4   3   2   1   2   3   4   5
            %   6   5   4   3   2   3   4   5   6
            %   7   6   5   4   3   4   5   6   7
            %   8   7   6   5   4   5   6   7   8
            % 
    tleXM   % Duplicate and compute the mode of this matrix (4 in the example above)
    =@*     % Find the locations where the matrix is equal to the mode and
            % replace with the current character. Set all else to 0.
    +       % Add this to the padded version of the previous iteration. 
 ]          % End of if...else
 c          % Cast the current matrix as a character array (zeros are spaces)
            % Implicit end of for loop and implicit display 

17

JavaScriptの(ES6)、294 288 287 284 256 246 240 238 220バイト

このサイトでの私の最初のゴルフ。入力文字列を関数に渡しますf、例えばf('codegolf')

f=s=>{N=n=>(2<<n/2+.5)-3-n%2
D=N(l=s.length)
for(a=[],i=S=D*2+1;i--;)a[i]=a[S+~i]=Array(S).fill` `
for(;l--;)for(o=n=N(l+1),I=0;I<=n;I++,o-=~l&1)a[y=D+I][x=D+o]=a[x][y]=a[y][D-o]=a[x][D-I]=s[l]
return a.map(r=>r.join``)}

@Sunny Punのおかげで-6バイト:テンプレート文字列を渡すときに括弧を削除します。

-2 @ETHproductionsのおかげでバイト:変更S-i-1S+~il%2||o--o-=~l&1

@Sunnyしゃれ、@ edc65の提案あたり-18バイト:削除prompt()し、console.log()関数形式の賛成では、ない.join()文字列の最後の配列を


2
256バイトをカウントします。改行を1(\r\n)ではなくそれぞれ2バイト()としてカウントしている可能性はあります\nか?
メゴ

2
関数にするpromptと、とを削除してバイトを節約できますconsole.log。また、あなたはあなた(のような文字列の関数呼び出しごとに2バイト節約テンプレート文字列を使用することfill(' ')になりをfill` `
サニーしゃれ

4
PPCGへようこそ!他の人が言ったことと一緒に行くためには、短縮することができますS-i-1S+~i、とi%2||o--しますo-=~i&1
ETHproductions

@Megoはカウントを再確認しました。256が正しいです。ありがとう!
-darrylyeo

a=Array(D*2+1).fill .map((_,$,a)=>[...a])10バイト節約できると思います。
ニール

11

ハスケル、138の 137 136バイト

q 1=0
q x=(mod x 2+1)*(q(x-1)+1)
f x|m<-q$length x,n<-abs<$>[-m..m]=[do j<-n;max" "[c|(k,c)<-zip[1..]x,cycle[max i j,i+j]!!k==q k]|i<-n]

f結果を行のリストとして返す関数を定義します

@Lynnのおかげで-1バイト


8

C ++ - 373の366 362 359バイト

-1 by @ TuukkaX、-6 by @Kevin Cruijssen、-4 by imports、-3 by @Baum mit Augen

#import<string>
#import<vector>
#define A(x) abs(x-r)
using S=std::string;using V=std::vector<S>;int R(int n){return n?(n%2)?R(n-1)+1:2*R(n-1)+2:0;}V f(S w){int l=w.size(),r=R(l-1),s=r*2+1,i,j,k;V x(s);for(i=0;i<s;++i){x[i]=S(s,46);for(j=0;j<s;++j)for(k=0;k<l;++k){if(A(i)<=R(k)&A(j)<=R(k)&k%2?A(i)==R(k)|A(j)==R(k):(A(i)+A(j)==R(k)))x[i][j]=w[k];}}return x;}

http://ideone.com/5y54mx

注:このソリューションでは、ドット(「。」)を「スペース」文字として使用して、空のスペースをより見やすくします。代わりにスペースを使用するには、に変更S(s,46)する必要がありS(s,32)ます。スコアは同じままです。

ゴルフをしていない:

#import<string>
#import<vector>

#define A(x) abs(x-r) //r will be the center coordinate

using S=std::string;
using V=std::vector<S>;

int R(int n){ //recursive function returning the radius of a square/diamond
    return n?  //if(n!=0)
        (n%2)? //if(n%2 != 0)
            R(n-1)+1 //square -> just increase radius by 1
        :
            2*R(n-1)+2 //diamond -> radius doubles + 2
    :
        0 //n==0 -> end of recursion
    ;
}

V f(S w){ //function taking a string and returning a vector<string>
    int l=w.size(),r=R(l-1),s=r*2+1,i,j,k;  //r is radius (==center coord), s is size
    V x(s);  //vector<string>
    for(i=0;i<s;++i){  //loop over vector
        x[i]=S(s,46); //initialize string with dots. use 32 for spaces
        for(j=0;j<s;++j)
            for(k=0;k<l;++k){ //loop over word
                if(
                    A(i)<=R(k) //check bounds
                    &
                    A(j)<=R(k)
                    &
                    k%2?A(i)==R(k)|A(j)==R(k):(A(i)+A(j)==R(k))
                    //for square: check if distance to center of one coordinate = R
                    //for diamond: check if sum of distances = R
                )
                    x[i][j]=w[k]; //if any is true, replace default char
            }
    }

    return x;
}

ungolfedをオンラインで試す

使い方:

include<iostream>

void printSquare(std::string word){
    auto result = f(word);
    for(auto& s:result)
        std::cout << s << std::endl;
    std::cout << std::endl << std::endl;
}

int main() {
    printSquare("a");
    printSquare("ab");
    printSquare("cat");
    printSquare("cats");
    printSquare("meow");
    printSquare("meows");
    printSquare("codegolf");
    return 0;
}

出力:

a


bbb
bab
bbb


....t....
...t.t...
..t...t..
.t.aaa.t.
t..aca..t
.t.aaa.t.
..t...t..
...t.t...
....t....


sssssssssss
s....t....s
s...t.t...s
s..t...t..s
s.t.aaa.t.s
st..aca..ts
s.t.aaa.t.s
s..t...t..s
s...t.t...s
s....t....s
sssssssssss


wwwwwwwwwww
w....o....w
w...o.o...w
w..o...o..w
w.o.eee.o.w
wo..eme..ow
w.o.eee.o.w
w..o...o..w
w...o.o...w
w....o....w
wwwwwwwwwww


............s............
...........s.s...........
..........s...s..........
.........s.....s.........
........s.......s........
.......s.........s.......
......s...........s......
.....s.wwwwwwwwwww.s.....
....s..w....o....w..s....
...s...w...o.o...w...s...
..s....w..o...o..w....s..
.s.....w.o.eee.o.w.....s.
s......wo..eme..ow......s
.s.....w.o.eee.o.w.....s.
..s....w..o...o..w....s..
...s...w...o.o...w...s...
....s..w....o....w..s....
.....s.wwwwwwwwwww.s.....
......s...........s......
.......s.........s.......
........s.......s........
.........s.....s.........
..........s...s..........
...........s.s...........
............s............


fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
f............................l............................f
f...........................l.l...........................f
f..........................l...l..........................f
f.........................l.....l.........................f
f........................l.......l........................f
f.......................l.........l.......................f
f......................l...........l......................f
f.....................l.............l.....................f
f....................l...............l....................f
f...................l.................l...................f
f..................l...................l..................f
f.................l.....................l.................f
f................l.......................l................f
f...............l.........................l...............f
f..............l...........................l..............f
f.............l.ooooooooooooooooooooooooooo.l.............f
f............l..o............g............o..l............f
f...........l...o...........g.g...........o...l...........f
f..........l....o..........g...g..........o....l..........f
f.........l.....o.........g.....g.........o.....l.........f
f........l......o........g.......g........o......l........f
f.......l.......o.......g.........g.......o.......l.......f
f......l........o......g...........g......o........l......f
f.....l.........o.....g.eeeeeeeeeee.g.....o.........l.....f
f....l..........o....g..e....d....e..g....o..........l....f
f...l...........o...g...e...d.d...e...g...o...........l...f
f..l............o..g....e..d...d..e....g..o............l..f
f.l.............o.g.....e.d.ooo.d.e.....g.o.............l.f
fl..............og......ed..oco..de......go..............lf
f.l.............o.g.....e.d.ooo.d.e.....g.o.............l.f
f..l............o..g....e..d...d..e....g..o............l..f
f...l...........o...g...e...d.d...e...g...o...........l...f
f....l..........o....g..e....d....e..g....o..........l....f
f.....l.........o.....g.eeeeeeeeeee.g.....o.........l.....f
f......l........o......g...........g......o........l......f
f.......l.......o.......g.........g.......o.......l.......f
f........l......o........g.......g........o......l........f
f.........l.....o.........g.....g.........o.....l.........f
f..........l....o..........g...g..........o....l..........f
f...........l...o...........g.g...........o...l...........f
f............l..o............g............o..l............f
f.............l.ooooooooooooooooooooooooooo.l.............f
f..............l...........................l..............f
f...............l.........................l...............f
f................l.......................l................f
f.................l.....................l.................f
f..................l...................l..................f
f...................l.................l...................f
f....................l...............l....................f
f.....................l.............l.....................f
f......................l...........l......................f
f.......................l.........l.......................f
f........................l.......l........................f
f.........................l.....l.........................f
f..........................l...l..........................f
f...........................l.l...........................f
f............................l............................f
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+............................................................+............................................................+
+...........................................................+.+...........................................................+
+..........................................................+...+..........................................................+
+.........................................................+.....+.........................................................+
+........................................................+.......+........................................................+
+.......................................................+.........+.......................................................+
+......................................................+...........+......................................................+
+.....................................................+.............+.....................................................+
+....................................................+...............+....................................................+
+...................................................+.................+...................................................+
+..................................................+...................+..................................................+
+.................................................+.....................+.................................................+
+................................................+.......................+................................................+
+...............................................+.........................+...............................................+
+..............................................+...........................+..............................................+
+.............................................+.............................+.............................................+
+............................................+...............................+............................................+
+...........................................+.................................+...........................................+
+..........................................+...................................+..........................................+
+.........................................+.....................................+.........................................+
+........................................+.......................................+........................................+
+.......................................+.........................................+.......................................+
+......................................+...........................................+......................................+
+.....................................+.............................................+.....................................+
+....................................+...............................................+....................................+
+...................................+.................................................+...................................+
+..................................+...................................................+..................................+
+.................................+.....................................................+.................................+
+................................+.......................................................+................................+
+...............................+.........................................................+...............................+
+..............................+...........................................................+..............................+
+.............................+.CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC.+.............................+
+............................+..C............................r............................C..+............................+
+...........................+...C...........................r.r...........................C...+...........................+
+..........................+....C..........................r...r..........................C....+..........................+
+.........................+.....C.........................r.....r.........................C.....+.........................+
+........................+......C........................r.......r........................C......+........................+
+.......................+.......C.......................r.........r.......................C.......+.......................+
+......................+........C......................r...........r......................C........+......................+
+.....................+.........C.....................r.............r.....................C.........+.....................+
+....................+..........C....................r...............r....................C..........+....................+
+...................+...........C...................r.................r...................C...........+...................+
+..................+............C..................r...................r..................C............+..................+
+.................+.............C.................r.....................r.................C.............+.................+
+................+..............C................r.......................r................C..............+................+
+...............+...............C...............r.........................r...............C...............+...............+
+..............+................C..............r...........................r..............C................+..............+
+.............+.................C.............r.ooooooooooooooooooooooooooo.r.............C.................+.............+
+............+..................C............r..o............F............o..r............C..................+............+
+...........+...................C...........r...o...........F.F...........o...r...........C...................+...........+
+..........+....................C..........r....o..........F...F..........o....r..........C....................+..........+
+.........+.....................C.........r.....o.........F.....F.........o.....r.........C.....................+.........+
+........+......................C........r......o........F.......F........o......r........C......................+........+
+.......+.......................C.......r.......o.......F.........F.......o.......r.......C.......................+.......+
+......+........................C......r........o......F...........F......o........r......C........................+......+
+.....+.........................C.....r.........o.....F.eeeeeeeeeee.F.....o.........r.....C.........................+.....+
+....+..........................C....r..........o....F..e....t....e..F....o..........r....C..........................+....+
+...+...........................C...r...........o...F...e...t.t...e...F...o...........r...C...........................+...+
+..+............................C..r............o..F....e..t...t..e....F..o............r..C............................+..+
+.+.............................C.r.............o.F.....e.t.ooo.t.e.....F.o.............r.C.............................+.+
++..............................Cr..............oF......et..oVo..te......Fo..............rC..............................++
+.+.............................C.r.............o.F.....e.t.ooo.t.e.....F.o.............r.C.............................+.+
+..+............................C..r............o..F....e..t...t..e....F..o............r..C............................+..+
+...+...........................C...r...........o...F...e...t.t...e...F...o...........r...C...........................+...+
+....+..........................C....r..........o....F..e....t....e..F....o..........r....C..........................+....+
+.....+.........................C.....r.........o.....F.eeeeeeeeeee.F.....o.........r.....C.........................+.....+
+......+........................C......r........o......F...........F......o........r......C........................+......+
+.......+.......................C.......r.......o.......F.........F.......o.......r.......C.......................+.......+
+........+......................C........r......o........F.......F........o......r........C......................+........+
+.........+.....................C.........r.....o.........F.....F.........o.....r.........C.....................+.........+
+..........+....................C..........r....o..........F...F..........o....r..........C....................+..........+
+...........+...................C...........r...o...........F.F...........o...r...........C...................+...........+
+............+..................C............r..o............F............o..r............C..................+............+
+.............+.................C.............r.ooooooooooooooooooooooooooo.r.............C.................+.............+
+..............+................C..............r...........................r..............C................+..............+
+...............+...............C...............r.........................r...............C...............+...............+
+................+..............C................r.......................r................C..............+................+
+.................+.............C.................r.....................r.................C.............+.................+
+..................+............C..................r...................r..................C............+..................+
+...................+...........C...................r.................r...................C...........+...................+
+....................+..........C....................r...............r....................C..........+....................+
+.....................+.........C.....................r.............r.....................C.........+.....................+
+......................+........C......................r...........r......................C........+......................+
+.......................+.......C.......................r.........r.......................C.......+.......................+
+........................+......C........................r.......r........................C......+........................+
+.........................+.....C.........................r.....r.........................C.....+.........................+
+..........................+....C..........................r...r..........................C....+..........................+
+...........................+...C...........................r.r...........................C...+...........................+
+............................+..C............................r............................C..+............................+
+.............................+.CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC.+.............................+
+..............................+...........................................................+..............................+
+...............................+.........................................................+...............................+
+................................+.......................................................+................................+
+.................................+.....................................................+.................................+
+..................................+...................................................+..................................+
+...................................+.................................................+...................................+
+....................................+...............................................+....................................+
+.....................................+.............................................+.....................................+
+......................................+...........................................+......................................+
+.......................................+.........................................+.......................................+
+........................................+.......................................+........................................+
+.........................................+.....................................+.........................................+
+..........................................+...................................+..........................................+
+...........................................+.................................+...........................................+
+............................................+...............................+............................................+
+.............................................+.............................+.............................................+
+..............................................+...........................+..............................................+
+...............................................+.........................+...............................................+
+................................................+.......................+................................................+
+.................................................+.....................+.................................................+
+..................................................+...................+..................................................+
+...................................................+.................+...................................................+
+....................................................+...............+....................................................+
+.....................................................+.............+.....................................................+
+......................................................+...........+......................................................+
+.......................................................+.........+.......................................................+
+........................................................+.......+........................................................+
+.........................................................+.....+.........................................................+
+..........................................................+...+..........................................................+
+...........................................................+.+...........................................................+
+............................................................+............................................................+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2
std::vector<S> V無駄な空白があります。
-Yytsi

1
印象的な答え!1あなたはゴルフ、それはいくつかのより多くしかし次のことができますでとすることができ、およびのための-3バイト。そして、なることができるし、その後になることができ、別-3バイトため。if(A(i)<=R(k)&&A(j)<=R(k)&&k%2?A(i)==R(k)||A(j)==R(k)&&||&|int r=R(w.size()-1),...int l=w.size(),r=R(l-1),...for(k=0;k<w.size();++k)for(k=0;k<l;++k)
ケビンCruijssen

#importを使用して、iostreamの代わりに文字列をインポートすることにより、さらに4バイトを保存しました。iostreamはprintSquare()にのみ必要です。
アネダー

1
C ++ 11の時点では、を使用using S=std::string;してあたり2バイトを保護できますtypedef
バウムmit Augen

1
@grooveplex回答で述べたように、「注:このソリューションでは、ドット(「。」)を「スペース」文字として使用して、空のスペースをより見やすくします。代わりにスペースを使用するには、S(s、46) S(s、32)。スコアは同じままです。」
アネダー

4

Mathematica、167バイト

Grid@Fold[g=Normal@SparseArray[h=f;a_List/;Plus@@Abs[a-#-1]==#->#3,2{#,#}+1]+a[#2,#/2+1]&[Length@#+1,#,#2]&;h=f=(h=g;a=ArrayPad)[#,1,#2]&;h@##&,{{#}},{##2}]/.Rule[0,]&

入力は文字のリストでなければなりません。

例えば

Grid@Fold[ ... ]/.Rule[0,]&["i","n","p","u","t"]

3

JavaScript(ES6)、252バイト

f=([c,...r],a,n,d)=>c?a?d?f(r,[...Array(m=n+n+3)].map((_,i,b)=>b.map((_,j)=>(a[i-g]||[])[j-g]||(i+j-h&&j+h-i&&h+i-j&&i+j-h*3?` `:c)),h=m>>1,g=m-n>>1),m,0):f(r,[b=[...a].fill(c),...a,b].map(b=>[c,...b,c]),n+2,1):f(r,[[c]],1,0):a.map(b=>b.join``).join`\n`
<input oninput=o.textContent=this.value&&f(this.value)><pre id=o>

where \nは、リテラルの改行文字を表します。


3

JavaScript(ES6)、204 229 238

文字列の配列を返す匿名関数

s=>[...s].map(v=>{T=(x,y)=>((r=[...c[y+z]||' '.repeat(z+z)])[z+x]=r[z-x]=v,c[z+y]=c[z-y]=r.join``);for(y=o-~o,o*=++i%2+1;y--;i%2?T(q,y):T(o,q,T(q,o)))q=y-o;++o},l=s.length,z=(2<<-~l/2)-3-l%2,c=[i=o=0])&&c

少ないゴルフ

s=>(
  l = s.length,
  z = (2 << l/2 + .5) - 3 - l%2, // zero position, total width and height are z+z+1
  c = [], // output canvas
  o = 0, // base drawing offset from z
  [...s].map( (v, i) => {
    // execute for each char v at position i in input string
    // helper function to put current char v at position (z+y, z+x),(z+y,z-x),(z-y,z+x),(z-y,z-x)
    // as the image has vertical and horizontal symmetry
    T=(x,y) =>(
      r = [...c[y+z]||' '.repeat(z+z)]), // convert string to array to set char at given pos
      r[z+x] = v, 
      r[z-x] = v,
      c[z+y] = c[z-y] = r.join`` // array to string again
    ); 
    // shorter than if(i%2) {for ...}else{for ...}
    // o is doubled at every other step
    //   and incremented (below) at each step
    for(y=o+o, i % 2 ? 0 : o=o*2; y >= 0; y--)
      q = y-o,
      i % 2 // even and odd steps have different shapes
        ? ( T(o,q), T(q,o))
        : T(q,y);
    ++o 
  }),
  c
)

テスト

f=
s=>[...s].map(v=>{T=(x,y)=>((r=[...c[y+z]||' '.repeat(z+z)])[z+x]=r[z-x]=v,c[z+y]=c[z-y]=r.join``);for(y=o-~o,o*=++i%2+1;y--;i%2?T(q,y):T(o,q,T(q,o)))q=y-o;++o},l=s.length,z=(2<<-~l/2)-3-l%2,c=[i=o=0])&&c

function update()
{
  O.textContent=f(I.value).join`\n`
}

update()
<input id=I value=CodeGolf oninput='update()'>
<pre id=O></pre>


2

Java- 429 417バイト

int r,l;int D(int n){return Math.abs(n-r);}int R(int n){return n=(n!=0)?((n%2!=0)?(R(n-1)+1):(2*R(n-1))+2):n;}char[][]C(String a){char[]c=a.toCharArray();l=a.length();r=R(l-1);int s=r*2+1,i,j,k;char[][]x=new char[s][s];for(i=0;i<s;i++){for(j=0;j<s;j++){x[i][j]=' ';for(k=0;k<l;k++){if(D(i)<=R(k)&&D(j)<=R(k)){if(k%2!=0){if(D(i)==R(k)||D(j)==R(k)){x[i][j]=c[k];}}else{if(D(i)+D(j)==R(k)){x[i][j]=c[k];}}}}}}return x;}

この回答の構造に影響を与えたため、クレジットは@Anedarに送られます

ゴルフをしていない:

int r, l;

int D(int n) {
    return Math.abs(n - r);
}

int R(int n) {
    return n = (n != 0) ? ((n % 2 != 0) ? (R(n - 1) + 1) : (2 * R(n - 1)) + 2) : n;
}

char[][] C(String a) {
    char[] c = a.toCharArray();
    l = a.length();
    r = R(l - 1);
    int s = r * 2 + 1, i, j, k;
    char[][] x = new char[s][s];
    for (i = 0; i < s; i++) {
        for (j = 0; j < s; j++) {
            x[i][j] = ' ';
            for (k = 0; k < l; k++) {
                if (D(i) <= R(k) && D(j) <= R(k)) {
                    if (k % 2 != 0) {
                        if (D(i) == R(k) || D(j) == R(k)) {
                            x[i][j] = c[k];
                        }
                    } else if (D(i) + D(j) == R(k)) {
                        x[i][j] = c[k];
                    }
                }
            }
        }
    }
    return x;
}

1

JavaScript(ES6)、322バイト

非競争的

var L=s=>{var A=Array,a,D,c,d=-1,i,k,w,O,Z,P=(u,v)=>{Z=O-(d>>1);a[D?u-v+O:u+Z][D?u+v+O-d+1:v+Z]=c},G=(c,i)=>(D=~i&1)?(d+=2,w=2*d-1):(d+=d+1,w=d);s=[...s];s.map(G);a=A(w).fill(0).map(a=>A(w).fill` `);O=w>>1;d=-1;s.map((C,i)=>{G(0,i);c=C;for(k=d;k--;)P(0,k),P(d-1,k),P(k,0),P(k,d-1)});return a.map(v=>v.join("")).join("\n")}

ゴルファーが少ない

var L=s=>{
    var A=Array,a,D,c,d=-1,i,k,w,O,Z
    P=(u,v)=>{
        Z=O-(d>>1)
        a[D?u-v+O:u+Z][D?u+v+O-d+1:v+Z]=c},
    G=(c,i)=>(D=~i&1)?(d+=2,w=2*d-1):(d+=d+1,w=d)
    s=[...s]
    s.map(G)
    a=A(w).fill(0).map( a=>A(w).fill` `)
    O=w>>1
    d=-1
    s.map((C,i)=>{
        G(0,i)
        c=C
        for(k=d;k--;)
            P(0,k),P(d-1,k),P(k,0),P(k,d-1)})
    return a.map(v=>v.join("")).join("\n")
}

でテスト

L("arty*")

リードを除外することによって簡単に利益var L=.join("\n")-17バイト returnステートメントを省略することでますが、コードをテストするためにそれらを再度入力します:D

幅を評価する2パスの回答です(wPは、2回目のパスで回転と平行移動を使用してポイントをプロットする前に、正方形または菱形のプロットをプロットするために必要な)をです。d平行移動または回転前の回転していない正方形の寸法(高さまたは幅)です。

√2詳細による回転とスケールMathematicaの答えこのスレッドの最初の回答からのタグ付き文字列テンプレートの使用。学習してくれてありがとう。質問と回答への投票。


1

Powershell、269 245 238バイト

$m,$a=$args
if($a){$a|%{$c=$_
$w=$m[0].Length
$m=&({$i=0
($m|%{$_+'.'*$w+'.'})+(0..$w|%{'..'*$w+'.'})|%{($s=$_|% t*y)[--$i]=$c
-join$s}},{$m|%{$_+$c}
$c*$w+$c})[++$k%2]}
$m=($w=$m[0].Length-1)..1+0..$w|%{$m[$_]|%{-join($_[$w..1]+$_)}}}
$m

Ungolfed、説明、テストスクリプト:

$f={

# $args is input char array
# $m is canvas.
# This script draws a right-bottom quadrant (axes included) on the canvas.
# If $args contains one char only then $m contains this char and whole image at same time.
$m,$a=$args                             # $a is other chars array
if($a){
    $a|%{
        $c=$_                           # $c is current char
        $w=$m[0].Length                 # $w is width of the canvas
        $m=&({                          # $m is the result of executing one of the two scriptblocks
            # draw Diamond quadrant
            $i=0
            ($m|%{$_+'.'*$w+'.'})+      # expand the canvas
            (0..$w|%{'..'*$w+'.'})|%{   # add new lines
                ($s=$_|% t*y)[--$i]=$c  # take a diamond edge on each line of the canvas
                -join$s
            }
        },{
            # draw Square quadrant
            $m|%{$_+$c}                 # expand the canvas
            $c*$w+$c                    # add new line
        })[++$k%2]                      # get a scriptblock from the array and execute it
    }

    # We have a right-bottom part of drawn AsciiArt now. Flip, flip, paste it.
    $w=$m[0].Length-1                   # width of the canvas
    $m=$w..1+0..$w|%{$m[$_]|%{          # flip vertical
        -join($_[$w..1]+$_)             # flip horizontal
    }}
}
$m

}

@(
,('c',@"
c
"@)
,('ca',@"
aaa
aca
aaa
"@)
,('cat',@"
....t....
...t.t...
..t...t..
.t.aaa.t.
t..aca..t
.t.aaa.t.
..t...t..
...t.t...
....t....
"@)
,('cats',@"
sssssssssss
s....t....s
s...t.t...s
s..t...t..s
s.t.aaa.t.s
st..aca..ts
s.t.aaa.t.s
s..t...t..s
s...t.t...s
s....t....s
sssssssssss
"@)
,('meows',@"
............s............
...........s.s...........
..........s...s..........
.........s.....s.........
........s.......s........
.......s.........s.......
......s...........s......
.....s.wwwwwwwwwww.s.....
....s..w....o....w..s....
...s...w...o.o...w...s...
..s....w..o...o..w....s..
.s.....w.o.eee.o.w.....s.
s......wo..eme..ow......s
.s.....w.o.eee.o.w.....s.
..s....w..o...o..w....s..
...s...w...o.o...w...s...
....s..w....o....w..s....
.....s.wwwwwwwwwww.s.....
......s...........s......
.......s.........s.......
........s.......s........
.........s.....s.........
..........s...s..........
...........s.s...........
............s............
"@)
,('codegolf',@"
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
f............................l............................f
f...........................l.l...........................f
f..........................l...l..........................f
f.........................l.....l.........................f
f........................l.......l........................f
f.......................l.........l.......................f
f......................l...........l......................f
f.....................l.............l.....................f
f....................l...............l....................f
f...................l.................l...................f
f..................l...................l..................f
f.................l.....................l.................f
f................l.......................l................f
f...............l.........................l...............f
f..............l...........................l..............f
f.............l.ooooooooooooooooooooooooooo.l.............f
f............l..o............g............o..l............f
f...........l...o...........g.g...........o...l...........f
f..........l....o..........g...g..........o....l..........f
f.........l.....o.........g.....g.........o.....l.........f
f........l......o........g.......g........o......l........f
f.......l.......o.......g.........g.......o.......l.......f
f......l........o......g...........g......o........l......f
f.....l.........o.....g.eeeeeeeeeee.g.....o.........l.....f
f....l..........o....g..e....d....e..g....o..........l....f
f...l...........o...g...e...d.d...e...g...o...........l...f
f..l............o..g....e..d...d..e....g..o............l..f
f.l.............o.g.....e.d.ooo.d.e.....g.o.............l.f
fl..............og......ed..oco..de......go..............lf
f.l.............o.g.....e.d.ooo.d.e.....g.o.............l.f
f..l............o..g....e..d...d..e....g..o............l..f
f...l...........o...g...e...d.d...e...g...o...........l...f
f....l..........o....g..e....d....e..g....o..........l....f
f.....l.........o.....g.eeeeeeeeeee.g.....o.........l.....f
f......l........o......g...........g......o........l......f
f.......l.......o.......g.........g.......o.......l.......f
f........l......o........g.......g........o......l........f
f.........l.....o.........g.....g.........o.....l.........f
f..........l....o..........g...g..........o....l..........f
f...........l...o...........g.g...........o...l...........f
f............l..o............g............o..l............f
f.............l.ooooooooooooooooooooooooooo.l.............f
f..............l...........................l..............f
f...............l.........................l...............f
f................l.......................l................f
f.................l.....................l.................f
f..................l...................l..................f
f...................l.................l...................f
f....................l...............l....................f
f.....................l.............l.....................f
f......................l...........l......................f
f.......................l.........l.......................f
f........................l.......l........................f
f.........................l.....l.........................f
f..........................l...l..........................f
f...........................l.l...........................f
f............................l............................f
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
"@)
#,('codegolfes',"")
) | % {
    $s, $e = $_
    $c = $s-split''-ne''
    $r = &$f @c
    $r = $r-join"`n"
    "$($e-eq$r): $s`n$r"
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.