ボックス内にASCIIボックスを描く


23

問題

与えられた入力 a,b,c

a,b,c正の偶数整数はどこですか

そして a > b > c

寸法付きの許可された文字のボックスを作成します a x a

b x b前の寸法内の異なる許容文字のボックスを中央に配置します

c x c前の寸法内の別の異なる許可された文字のボックスを中央に配置します

許可される文字はASCII文字です [a-zA-z0-9!@#$%^&*()+,./<>?:";=_-+]

入力 a=6, b=4, c=2

######
#****#
#*@@*#
#*@@*#
#****#
######

入力 a=8, b=6, c=2

########
#******#
#******#
#**@@**#
#**@@**#
#******#
#******#
########

入力 a=12, b=6, c=2

############
############
############
###******###
###******###
###**@@**###
###**@@**###
###******###
###******###
############
############
############

ルール

  • 最短のコードが勝つ
  • 指定された範囲内でどの文字を印刷するかを選択できることに注意してください
  • 末尾の改行を受け入れました
  • 末尾の空白が受け入れられます
  • 関数は、改行、文字列配列を含む文字列を返すか、それを出力します

5
入力は常に有効ですか(つまり、各数値は前の数値より少なくとも2少ない)。対称的な描画を保証するために、数値は常に(すべて偶数)または(すべて奇数)になりますか?
散布

年輪表示する年齢にかなり似ています。
マナトワーク

1
@Christianの最初の3行はこれらの要件を定義しています。それらが十分かどうかを教えてください。
-LiefdeWen

@StefanDelportそうですね、私はそれを見逃しました。ありがとう。
散布

回答:



7

ゼリー 20  19 バイト

`Erik the Outgolferが示唆したように、リンクを回避するためにクイックを使用して-1バイト。

H»þ`€Ḣ>ЀHSUṚm€0m0Y

... [a,b,c]を使用してボックスを印刷するリストを取得する完全なプログラムa:2 b:1 c:0
は、実際には、最大10個のボックスで動作し、最も内側のボックスが0たとえば)です。

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

どうやって?

H»þ`€Ḣ>ЀHSUṚm€0m0Y - Main link: list of boxes, B = [a, b, c]
H                   - halve B = [a/2, b/2, c/2]
    €               - for €ach:
   `                -   repeat left argument as the right argument of the dyadic operation:
  þ                 -     outer product with the dyadic operation:
 »                  -       maximum
                    - ... Note: implicit range building causes this to yield
                    -       [[max(1,1),max(1,2),...,max(1,n)],
                    -        [max(2,1),max(2,2),...,max(2,n)],
                    -        ...
                    -        [max(n,1),max(n,2),...,max(n,n)]]
                    -       for n in [a/2,b/2,c/2]
     Ḣ              - head (we only really want n=a/2 - an enumeration of a quadrant)
         H          - halve B = [a/2, b/2, c/2]
       Ѐ           - map across right with dyadic operation:
      >             -   is greater than?
                    - ...this yields three copies of the lower-right quadrant
                    -    with 0 if the location is within each box and 1 if not
          S         - sum ...yielding one with 0 for the innermost box, 1 for the next, ...
           U        - upend (reverse each) ...making it the lower-left
            Ṛ       - reverse ...making it the upper-right
             m€0    - reflect €ach row (mod-index, m, with right argument 0 reflects)
                m0  - reflect the rows ...now we have the whole thing with integers
                  Y - join with newlines ...making a mixed list of integers and characters
                    - implicit print - the representation of a mixed list is "smashed"

7

パイソン2、107の 103バイト

a,b,c=input()
r=range(1-a,a,2)
for y in r:
 s=''
 for x in r:m=max(x,y,-x,-y);s+=`(m>c)+(m>b)`
 print s

全プログラム、と箱を印刷しa=2b=1c=0

リストの内包表記(104バイト)を使用したわずかに悪い答え:

a,b,c=input()
r=range(1-a,a,2)
for y in r:print''.join(`(m>c)+(m>b)`for x in r for m in[max(x,y,-x,-y)])

5

C#、274 232バイト

using System.Linq;(a,b,c)=>{var r=new string[a].Select(l=>new string('#',a)).ToArray();for(int i=0,j,m=(a-b)/2,n=(a-c)/2;i<b;++i)for(j=0;j<b;)r[i+m]=r[i+m].Remove(j+m,1).Insert(j+++m,i+m>=n&i+m<n+c&j+m>n&j+m<=n+c?"@":"*");return r;}

C#でもひどいのでゴルフは間違いなくできますが、私の心は空になりました。

フル/フォーマット済みバージョン:

using System;
using System.Linq;

class P
{
    static void Main()
    {
        Func<int, int, int, string[]> f = (a,b,c) =>
        {
            var r = new string[a].Select(l => new string('#', a)).ToArray();

            for (int i = 0, j, m = (a - b) / 2, n = (a - c) / 2; i < b; ++i)
                for (j = 0; j < b;)
                    r[i + m] = r[i + m].Remove(j + m, 1).Insert(j++ + m,
                        i + m >= n & i + m < n + c &
                        j + m > n & j + m <= n + c ? "@" : "*");

            return r;
        };

        Console.WriteLine(string.Join("\n", f(6,4,2)) + "\n");
        Console.WriteLine(string.Join("\n", f(8,6,2)) + "\n");
        Console.WriteLine(string.Join("\n", f(12,6,2)) + "\n");

        Console.ReadLine();
    }
}

あなたはあなたの心を取り戻したように見えることがj + m <= n + cでき ますn + c > j + m
-LiefdeWen

同様i + m >= nn < i + m
LiefdeWen

i+m4回使用するので、変数に追加してfor一部を保存することができます
-LiefdeWen

適切にこれらのチェックではなく:あなたは決して使用しないiだけで初期化、孤立してi=mと比較しますi<b+m。または... iinit i=0but loop onを使用しi<a、次にに追加r[i]=new string('#',a),j=0、チェックする条件を追加して、ループのi範囲内にあることを確認しjます(すべてのLinqを失うため、これは成果を上げるはずです)。
VisualMelon


3

JavaScript(ES6)、174 170 147バイト

a=>b=>c=>(d=("#"[r="repeat"](a)+`
`)[r](f=a/2-b/2))+(e=((g="#"[r](f))+"*"[r](b)+g+`
`)[r](h=b/2-c/2))+(g+(i="*"[r](h))+"@"[r](c)+i+g+`
`)[r](c)+e+d

それを試してみてください

fn=
a=>b=>c=>(d=("#"[r="repeat"](a)+`
`)[r](f=a/2-b/2))+(e=((g="#"[r](f))+"*"[r](b)+g+`
`)[r](h=b/2-c/2))+(g+(i="*"[r](h))+"@"[r](c)+i+g+`
`)[r](c)+e+d
oninput=_=>+x.value>+y.value&&+y.value>+z.value&&(o.innerText=fn(+x.value)(+y.value)(+z.value))
o.innerText=fn(x.value=12)(y.value=6)(z.value=2)
label,input{font-family:sans-serif;}
input{margin:0 5px 0 0;width:50px;}
<label for=x>a: </label><input id=x min=6 type=number step=2><label for=y>b: </label><input id=y min=4 type=number step=2><label for=z>c: </label><input id=z min=2 type=number step=2><pre id=o>


説明

a=>b=>c=>            :Anonymous function taking the 3 integers as input via parameters a, b & c
(d=...)              :Assign to variable d...
("#"[r="repeat"](a)  :  # repeated a times, with the repeat method aliased to variable r in the process.
+`\n`)               :  Append a literal newline.
[r](f=a/2-b/2)       :  Repeat the resulting string a/2-b/2 times, assigning the result of that calculation to variable f.
+                    :Append.
(e=...)              :Assign to variable e...
(g=...)              :  Assign to variable g...
"#"[r](f)            :    # repeated f times.
+"*"[r](b)           :  Append * repeated b times.
+g+`\n`)             :  Append g and a literal newline.
[r](h=b/2-c/2)       :  Repeat the resulting string b/2-c/2 times, assigning the result of that calculation to variable h.
+(...)               :Append ...
g+                   :  g
(i=...)              :  Assign to variable i...
"*"[r](h)            :    * repeated h times.
+"@"[r](c)           :  @ repeated c times
+i+g+`\n`)           :  Append i, g and a literal newline.
[r](c)               :...repeated c times.
+e+d                 :Append e and d.


2

V70、44、42バイト

Àé#@aÄÀG@b|{r*ÀG@c|{r@òjdòÍ.“.
ç./æ$pYHP

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

これは恐ろしいです。うわー ずっといい。まだ最短ではありませんが、少なくともややゴルフです。

@ nmjmcman101のおかげで2バイト節約できました!

Hexdump:

00000000: c0e9 2340 61c4 c047 4062 7c16 7b72 2ac0  ..#@a..G@b|.{r*.
00000010: 4740 637c 167b 7240 f26a 64f2 cd2e 932e  G@c|.{r@.jd.....
00000020: 0ae7 2e2f e624 7059 4850                 .../.$pYHP

最後の2行を組み合わせて2バイト節約できますオンラインで試してみてください!
nmjcman101

@ nmjcman101ああ、良い点。ありがとう!
DJMcMayhem


1

Mathematica、49バイト

Print@@@Fold[#~CenterArray~{#2,#2}+1&,{{}},{##}]&

入力を受け取ります[c, b, a]。出力はa=1, b=2, c=3です。

どうやって?

Print@@@Fold[#~CenterArray~{#2,#2}+1&,{{}},{##}]&
                                                &  (* Function *)
        Fold[                        ,{{}},{##}]   (* Begin with an empty 2D array.
                                                      iterate through the input: *)
                                    &              (* Function *)
             #~CenterArray~{#2,#2}                 (* Create a 0-filled array, size
                                                      (input)x(input), with the array
                                                      from the previous iteration
                                                      in the center *)
                                  +1               (* Add one *)
Print@@@                                           (* Print the result *)

@Jenny_mathyで質問:*関数は、改行して文字列配列を文字列を返す、またはそれを印刷することができる」Gridことはありません。StringまたそれんPrint。それ
JungHwan分

0

PHP> = 7.1、180バイト

この場合、変数が$PHPで始まることを嫌います

for([,$x,$y,$z]=$argv;$i<$x*$x;$p=$r%$x)echo XYZ[($o<($l=$x-$a=($x-$y)/2)&$o>($k=$a-1)&$p>$k&$p<$l)+($o>($m=$k+$b=($y-$z)/2)&$o<($n=$l-$b)&$p>$m&$p<$n)],($o=++$i%$x)?"":"\n".!++$r;

PHPサンドボックスオンライン


この場合、印刷前のペイントはかなり短くなります。:Dそれとも$argv、配列として使用するからですか?試しましたか?別々の三元を試しましたか?
タイタス

@タイタス私は知りませんが、私のアプローチは無効な奇数の入力を修正します
ヨルグヒュルサーマン

0

Mathematica、173バイト

(d=((a=#1)-(b=#2))/2;e=(b-(c=#3))/2;z=1+d;x=a-d;h=Table["*",a,a];h[[z;;x,z;;x]]=h[[z;;x,z;;x]]/.{"*"->"#"};h[[z+e;;x-e,z+e;;x-e]]=h[[z+e;;x-e,z+e;;x-e]]/.{"#"->"@"};Grid@h)&

入力

[12,6,2]


0

Python 2、87バイト

a,_,_=t=input();r=~a
exec"r+=2;print sum(10**x/9*10**((a-x)/2)*(r*r<x*x)for x in t);"*a

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

フォームの数値を加算することにより、印刷する数値を算術的に計算します111100。多くのさがあり、おそらく改善の余地があります。


0

Java 8、265 252バイト

 a->b->c->{int r[][]=new int[a][a],x=0,y,t;for(;x<a;x++)for(y=0;y<a;r[x][y++]=0);for(t=(a-b)/2,x=t;x<b+t;x++)for(y=t;y<b+t;r[x][y++]=1);for(t=(a-c)/2,x=t;x<c+t;x++)for(y=t;y<c+t;r[x][y++]=8);String s="";for(int[]q:r){for(int Q:q)s+=Q;s+="\n";}return s;}

文字を数字に置き換えて-13バイト。

別のアプローチを使用して間違いなくゴルフをすることができます。

説明:

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

a->b->c->{                         // Method with three integer parameters and String return-type
  int r[][]=new int[a][a],         //  Create a grid the size of `a` by `a`
      x=0,y,t;                     //  Some temp integers
  for(;x<a;x++)for(y=0;y<a;r[x][y++]=0);
                                   //  Fill the entire grid with zeros
  for(t=(a-b)/2,x=t;               //  Start at position `(a-b)/2` (inclusive)
      x<b+t;x++)                   //  to position `b+((a-b)/2)` (exclusive)
    for(y=t;y<b+t;r[x][y++]=1);    //   And replace the zeros with ones
  for(t=(a-c)/2,x=t;               //  Start at position `(a-c)/2` (inclusive)
      x<c+t;x++)                   //  to position `c+((a-b)/2)` (exclusive)
    for(y=t;y<c+t;r[x][y++]=8);    //   And replace the ones with eights
  String s="";                     //  Create a return-String
  for(int[]q:r){                   //  Loop over the rows
    for(int Q:q)                   //   Inner loop over the columns
      s+=Q;                        //    and append the result-String with the current digit
                                   //   End of columns-loop (implicit / single-line body)
    s+="\n";                       //   End add a new-line after every row
  }                                //  End of rows-loop
  return s;                        //  Return the result-String
}                                  // End of method

0

JavaScript(ES6)、112

複数行の文字列を返す匿名関数。文字0、1、2

(a,b,c)=>eval("for(o='',i=-a;i<a;o+=`\n`,i+=2)for(j=-a;j<a;j+=2)o+=(i>=c|i<-c|j>=c|j<-c)+(i>=b|i<-b|j>=b|j<-b)")

少ないゴルフ

(a,b,c)=>{
  for(o='',i=-a;i<a;o+=`\n`,i+=2)
    for(j=-a;j<a;j+=2)
      o+=(i>=c|i<-c|j>=c|j<-c)+(i>=b|i<-b|j>=b|j<-b)
  return o
}  

var F=
(a,b,c)=>eval("for(o='',i=-a;i<a;o+=`\n`,i+=2)for(j=-a;j<a;j+=2)o+=(i>=c|i<-c|j>=c|j<-c)+(i>=b|i<-b|j>=b|j<-b)")

function update()
{
  var [a,b,c]=I.value.match(/\d+/g)
  O.textContent=F(a,b,c)
}

update()
  
a,b,c <input id=I value='10 6 2' oninput='update()'>
<pre id=O></pre>


0

PHP 5.6以上、121バイト

for($r=A;$p?:$p=($z=$argv[++$i])**2;)$r[((--$p/$z|0)+$o=($w=$w?:$z)-$z>>1)*$w+$p%$z+$o]=_BCD[$i];echo chunk_split($r,$w);

オンラインで実行する-nr、オンラインでテストします

再びループを組み合わせて...私はそれらが大好きです!

壊す

for($r=A;                           # initialize $r (result) to string
    $p?:$p=($z=$argv[++$i])**2;)    # loop $z through arguments, loop $p from $z**2-1 to 0
    $r[((--$p/$z|0)+$o=
        ($w=$w?:$z)                     # set $w (total width) to first argument
        -$z>>1)*$w+$p%$z+$o]            # calculate position: (y+offset)*$w+x+offset
        =_BCD[$i];                      # paint allowed character (depending on $i)
echo chunk_split($r,$w);            # insert newline every $w characters and print
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.