ケーキを焼いてください!


15

これはケーキです:

_========_
|        |
+________+
|        |
+________+
|        |
+________+

幅8、高さ3、深さ1です。

3つの入力からケーキを作成するプログラムを作成する必要があります。最初の入力は、中央にアンダースコアをいくつ=、一番上にsを置くかを制御します。以下は、8ではなく10の幅を持つ最初のケーキです。

_==========_
|          |
+__________+
|          |
+__________+
|          |
+__________+

2番目の入力は、ケーキの高さを制御します。これは、3ではなく4の高さを持つ2番目のケーキです。

_==========_
|          |
+__________+
|          |
+__________+
|          |
+__________+
|          |
+__________+

レイヤーの繰り返しに注意してください。

3番目の入力は、その深さを制御します。その| |上に含める数。これは、深さ1ではなく2の3番目のケーキです。

_==========_
|          |
|          |
+__________+
|          |
+__________+
|          |
+__________+
|          |
+__________+

末尾の空白を印刷できます。テストケース:

入力:333

出力:

_===_
|   |
|   |
|   |
+___+
|   |
+___+
|   |
+___+

(私はこのケーキを手に入れないことを望みます)

入力:321

出力:

_===_
|   |
+___+
|   |
+___+

入力:555

出力:

_=====_
|     |
|     |
|     |
|     |
|     |
+_____+
|     |
+_____+
|     |
+_____+
|     |
+_____+
|     |
+_____+

入力は常に正の整数ですか?
ニッククリフォード

@NickCliffordはい。

末尾の改行は許可されますか?
シャギー


@Shaggy私はそう思いますが、Metaではデフォルトでyesです。
Programmer5000

回答:


9

V25、20のバイト

2é_Àé=ÙÒ|èÙÒ+È_ÀäkÀÄ

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

Hexdump:

00000000: 32e9 5fc0 e93d d9d2 7ce8 d9d2 2bc8 5fc0  2._..=..|...+._.
00000010: e46b c0c4                                .k..

おかげで@ nmjmcman1013バイトを節約して、さらに2バイトを節約した古い演算子を思い出させてくれます。

説明:

ab、およびc3つの引数です。

2é_                   " Insert two '_' characters
   Àé=                " Insert 'a' '=' characters between them
      Ù               " Duplicate this line
       Ò|             " Replace this whole line with '|'s
         è            " *Hollow* this line (replace all the middle characters with spaces)
          Ù           " Duplicate this line
           Ò+         " Replace this whole line with '+'s
             È_       " *Hollow* this line again, but use '_' instead of spaces
               Àäk    " Make 'b' copies of this line and the line above it
                  ÀÄ  " Make 'c' copies of this line

私はあなたが交換することができると思うÒ r|$.Ò|è、いくつかのバイトを。オンラインでお試しください!
nmjcman101

@ nmjcman101あら、è存在することすら忘れていた。私はそれが何をするかを覚えるために頭を悩ませる必要がありました。しかし、それは本当に賢いです!実際、大文字のバリアントを使用すると、È<char>おそらく他のいくつかの場所でさらに多くのバイトを節約できます。思い出させてくれてありがとう!:)
DJMcMayhem

4

34 26バイト

Nγ←×γ_↑+↑N_×γ=‖BOγF⁻N¹C⁰±²

オンラインでお試しください!リンクは、コードの詳細バージョンです。幅、深さ、高さの順にパラメーターを取ります。説明:

Nγ          Input the width.
←×γ_        Print a set of _s that go at the bottom of each layer.
↑+          Print one of the +s that go on the left.
↑N          Input the depth and print that many left |s.
_           Print the top left _.
×γ=         Print the =s along the top.
‖BOγ        Copy the left column to the right.
F           Repeat:
 ⁻ ¹         One time fewer than:
  N           Input of the height:
    C⁰±²        Copy the whole cake up 2 characters.

2

Mathematica、167バイト

c=Column;r=Row;t=Table;f=Flatten;c[c/@{r/@f[{{{"_",r@t["=",#],"_"}},t[{"|",r@t[" ",#],"|"},#3-1]},1],c/@f[{t[{r@{"|",r@t[" ",#],"|"},r@{"+",r@t["_",#],"+"}},#2]},1]}]&

2

PHP> = 7.1、104バイト

for([,$w,$h,$t]=$argv;$i<2*$h+$t;)echo str_pad($e="_|+"[$b=$i++<$t?$i>1:1+$_++%2],$w+1,"= _"[$b])."$e
";

オンライン版


1
悪くない。3バイトが見つかりました:for([,$w,$h,$t]=$argv;$i<2*$h+$t;)echo str_pad($e="_|+"[$b=$i++<$t?$i>1:2-($i-$t&1)],$w+1,"= _"[$b])."$e\n";
クリストフ

1
また、別の3バイト:$b=$i++<$t?$i>1:1+$_++%2
クリストフ

@クリストフ素敵なアイデアありがとうございます
ヨルクヒュルサーマン



1

ゼリー30 29 バイト

XORへの追加から-1バイトの切り替えにより、外側の列と内側の列を変換し、2つの_エントリを持たずに5文字のルックアップを可能にします。

ṬṚ;⁹RḤṬḤ¤Wµ^9ẋ⁵;@;µZị“_+= |”Y

全プログラムの3つのプログラムの引数を取りdepthheightwidthとケーキを印刷します。

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

どうやって?

ṬṚ;⁹RḤṬḤ¤Wµ^9ẋ⁵;@;µZị“_+= |”Y - Main link: depth, height (width is a program argument)
Ṭ                             - untruth   [0,0,0,...1] such that the length is the depth
 Ṛ                            - reverse   [1,0,0,...0]
        ¤                     - nilad followed by link(s) as a nilad:
   ⁹                          -   link's right argument, height
    R                         -   range   [1,2,3,...,height]
     Ḥ                        -   double  [2,4,6,...,2*height]
      Ṭ                       -   untruth [0,1,0,1,0,1,...,0,1] (length double height)
       Ḥ                      -   double  [0,2,0,2,0,2,...,0,2]
  ;                           - concatenate  [1,0,0,...,0,0,2,0,2,0,2,...,0,2]
                              -     ...this is the form of a column of cake!
         W                    - wrap in a list
          µ                   - monadic chain separation, call that c
           ^9                 - bitwise XOR c with 9 [8,9,9,...,9,9,11,9,11,9,11,...,9,11]
              ⁵               - program's 3rd argument, width
             ẋ                - repeat the augmented c width times
               ;@             - concatenate with a copy of c
                 ;            - concatenate a copy of c
                  µ           - monadic chain separation call that sideways cake
                   Z          - transpose the sideways cake to put it the right way up
                     “_+= |”  - literal ['_','+','=',' ','|'] (cake decoration)
                    ị         - index into (1 based and modular, so 8,9, and 11 are, mod 5,
                                            3, 4, and 1 yielding '=', ' ', and '_')
                            Y - join with new lines
                              - implicit print



1

Javaの7169の 164 158バイト

String f(int...a){String s="_",t="|",u="+";for(;a[0]-->0;s+="=",t+=" ")u+="_";s=s+"_";t="\n"+t+"|";u=t+"\n"+u+"+";for(;a[2]-->1;)s+=t;for(;a[1]-->0;)s+=u;return s;}

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

ゴルフをしていない:

String f(int...a)                    // saves two bytes over int a, int b, int c
{
    String s="_", t="|", u="+";      // set up the start of each row

    for(; a[0]-->0; s+="=", t+=" ")  // Uses the goes-to operator to fill the row
        u+="_";                      

    s += "_\n";                      // adds the end of each row
    t += "|\n";              
    u = t + u + "+\n";               // and combining t into u

    for(; a[2]-->1; )                // add the top of the cake
        s += t;

    for(; a[1]-->0; )                // add the rest of the cake
        s += u;

    return s;
}

あなたは入れて1つのバイトを保存することができますu=t+u+"+\n" inside the for-loop: のために(U = T + U + "+ \ N; [2] - > 1;)、S + = T;。。私から`しかし、素敵な答えは、1
ケビンCruijssen

1

05AB1E33 31バイト

'_'=¹×«Ć,'|¹úRóG=}²F='+'_¹×«Ć,

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

説明

'_'=¹×«Ć,'|¹úRóG=}²F='+'_¹×«Ć,   Main link. Args: w h d
'_                                Push literal '_'
  '=¹×                            Push '=' w times
      «Ć,                         Concat, enclose and print
         '|                       Push literal '|'
           ¹ú                     Pad with w spaces in front
             RĆ                   Reverse and ecnlose
               ³G }               d - 1 times do:
                 =                Print without consuming
                   ²F             h times do:
                     =            Print without consuming
                      '+          Push literal '+'
                        '_¹×      Push '_' w times
                            «Ć,   Concat, enclose and print

1

Windowsバッチ、211 180 163バイト

@Neilのおかげで合計48バイトをゴルフできました!

@for /l %%p in (1,1,%1)do @call set w= %%w%%
@echo _%w: ==%_
@for /l %%p in (2,1,%3)do @echo ^|%w%^|
@for /l %%p in (1,1,%2)do @echo ^|%w%^|&echo +%w: =_%+
@set w=

1
1. @各行およびそれ以降での使用doは、に比べてわずかに短くなり@echo offます。2. @call set w=%%w%%_回避しsetlocal enabledelayedexpansionます。3.を使用し@for /l %%p in (2,1,%2)ます。4. @set w=コードが複数回機能するように追加する必要があります。
ニール

最後の部分で混乱しています。追加しない@set w=と、コードが何度も機能しなくなりますか?
-stevefestl

1
1.あなた@for /l %%p in (1,1,%1)doが行方不明になったようです。2.ケーキの高さが間違っているようです。少なくとも試してみると、テストケースと一致していないようです。3.同じコマンドセッションでスクリプトを2回実行すると、ケーキの幅が広がります。
ニール

@Neilそれはすべて修正されました:)
stevefestl

1
すごい!私が気づいた最後のいくつかのこと:1 . @後は必要ありません&。2 2番目のループを(2,1,%3)3 番目のループに変更すると、(1,1,%2)そのecho +%w%+行を削除できると思います。3.正しくカウントした場合はw_sの代わりにスペースで埋める必要があると思います。これにより、置換が少なくなります。(@call set w= %%w%%そうでなければスペースを見つけるのは難しいでしょう!)
ニール

1

Haskell、87バイト

f w t d=["_=| +_\n"!!j|i<-0:([2..d]>>[2])++([1..t]>>[2,4]),j<-i:([1..w]>>[i+1])++[i,6]]

1
ゴルフ以外のほとんどの言語を後回しにしていい仕事だ。中置宣言(w#t)d= ...はバイトを節約します。
ライコニ

1

SOGL V0.12、25のバイト

e =*¼_Oe↕¼|.⌡Qe╔*¼+.H«{Q;

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

入力は幅、深さ、高さの順になります。


入力を並べ替えることはできますか?規定の順序で指定されているようです。
再帰的

1
@recursive通常、それは許可されており、質問が順序付けを強制することはなく、2番目に多く投票された回答は入力を並べ替えます。
-dzaima

1

Python 2、124 122 120 105 92バイト

w,t,d=input()
a="\n|"+w*" "+"|"
print"_"+w*"="+"_"+(d-1)*a+t*(a+"\n+"+w*"_"+"+")

プログラム引数の代わりにSTDINを使用して-15バイト

Python 2に切り替えて-13バイト(input()整数とprintステートメントの場合)

Caird Coinheringaahingから-12バイト

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

Python 3、124 122 120 105バイト

w,t,d=[int(input())for n in(1,2,3)]
a="\n|"+w*" "+"|"
print("_"+w*"="+"_"+(d-1)*a+t*(a+"\n+"+w*"_"+"+"))

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

完全なプログラムが必要でない場合:

Python 3、87 84バイト

lambda w,t,d:"_"+w*"="+"_"+(d-1)*("\n|"+w*" "+"|")+t*("\n|"+w*" "+"|\n+"+w*"_"+"+")

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



@cairdcoinheringaahing 78バイト
ジョナサンフレッチ

0

Javascript(ES6)、161 157バイト

f=(w,h,d)=>{r=x=>x.repeat(w);_='_';m='+'+r(_)+'+';b='|'+r(' ')+'|';c=[_+r('=')+_];for(i=d-1;i--;)
c.push(b);for(i=h;i--;)
c.push(b+'\n'+m);return c.join`\n`}

console.log(f(8,3,1));




0

JavaScript / ES6、90バイト

大まかな解決策を書いたところ、たまたま56バイトで既存のJSの答えを打ち負かしました。それから、私は11バイトをゴルフしました。

(w,h,d,g=a=>a+a[1][r='repeat'](w)+a[0]+`
`)=>g('_=')+(l=g('| '))[r](d-1)+(l+g('+_'))[r](h)

これがデモです。

var F = (w,h,d,f=a=>a+a[1][r='repeat'](w)+a[0]+`
`)=>f('_=')+(l=f('| '))[r](d-1)+(l+f('+_'))[r](h);

console.log(F(prompt('width') || 3, prompt('height') || 3, prompt('depth') || 3));
console.log(F.toString().length);


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