19
\$\begingroup\$

This is a cake:

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

It is 8 wide, 3 tall, and 1 deep.

You must write a program that makes a cake from 3 inputs. The first input controls how many underscores there are in the middle and =s on the top. Here's the first cake with a width of 10 instead of 8:

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

The second input controls how tall the cake is. Here's the second cake with a height of 4 instead of 3:

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

Note the repetition of the layers.

The third input controls how deep it is. That just how many | |s to include on the top. Here's the third cake with a depth of 2 instead of 1:

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

You can print trailing whitespace. Test cases:

Input: 3, 3, 3

Output:

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

(I hope I never get this cake)

Input: 3, 2, 1

Output:

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

Input: 5, 5, 5

Output:

_=====_
|     |
|     |
|     |
|     |
|     |
+_____+
|     |
+_____+
|     |
+_____+
|     |
+_____+
|     |
+_____+
\$\endgroup\$
7
  • \$\begingroup\$ Will inputs always be positive integers? \$\endgroup\$ Commented May 20, 2017 at 18:05
  • \$\begingroup\$ @NickClifford yes. \$\endgroup\$
    – user68614
    Commented May 20, 2017 at 18:34
  • \$\begingroup\$ Are trailing newlines allowed? \$\endgroup\$
    – Shaggy
    Commented May 20, 2017 at 18:37
  • 4
    \$\begingroup\$ Related \$\endgroup\$
    – anna328p
    Commented May 20, 2017 at 18:47
  • \$\begingroup\$ @Shaggy i'd assume so, it is yes by default on Meta. \$\endgroup\$
    – user58826
    Commented May 20, 2017 at 18:51

21 Answers 21

9
\$\begingroup\$

V, 25, 20 bytes

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

Try it online!

Hexdump:

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

Thanks to @nmjmcman101 for saving three bytes, and reminding me of an old operator that saved another two bytes.

Explanation:

a, b, and c are the three arguments.

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
\$\endgroup\$
2
  • \$\begingroup\$ I think you can replace Ò r|$. with Ò|è for some bytes.Try it online! \$\endgroup\$
    – nmjcman101
    Commented Oct 30, 2017 at 2:03
  • \$\begingroup\$ @nmjcman101 Oh man, I completely forgot that è even existed. I had to rack my brain to remember what it does. But that's really clever! In fact, using the uppercase variant, È<char> can probably save some more bytes in a few other places too. Thanks for reminding me! :) \$\endgroup\$
    – DJMcMayhem
    Commented Oct 30, 2017 at 17:32
4
\$\begingroup\$

Charcoal, 34 26 bytes

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

Try it online! Link is to verbose version of code. Takes parameters in the order width, depth, height. Explanation:

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.
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 33 31 bytes

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

Try it online!

Explanation

'_'=¹×«Ć,'|¹úRĆ³G=}²F='+'_¹×«Ć,   Full program
'_                                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
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 167 bytes

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]}]&
\$\endgroup\$
2
\$\begingroup\$

Jelly, 30 29 bytes

-1 byte switching from an addition to an XOR to translate between outer and inner columns, allowing for a 5 character lookup rather than having two _ entries.

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

Full program taking three program arguments of depth, height, width and printing the cake.

Try it online!

How?

ṬṚ;⁹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
\$\endgroup\$
2
\$\begingroup\$

PHP>=7.1, 104 Bytes

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

Online Version

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Not bad. Found 3 byte: 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"; \$\endgroup\$
    – Christoph
    Commented May 23, 2017 at 9:32
  • 1
    \$\begingroup\$ And another 3 byte: $b=$i++<$t?$i>1:1+$_++%2. \$\endgroup\$
    – Christoph
    Commented May 23, 2017 at 9:39
  • \$\begingroup\$ @Christoph Nice idea Thank You \$\endgroup\$ Commented May 23, 2017 at 10:16
2
\$\begingroup\$

Ruby, 62 bytes

->w,h,d{l="|#{' '*w}|
";"_#{?=*w}_
"+l*~-d+(l+"+#{?_*w}+
")*h}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Haskell, 87 bytes

f w t d=["_=| +_\n"!!j|i<-0:([2..d]>>[2])++([1..t]>>[2,4]),j<-i:([1..w]>>[i+1])++[i,6]]
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Nice job leaving most non-golfing languages way behind. An infix declaration (w#t)d= ... saves a byte. \$\endgroup\$
    – Laikoni
    Commented May 23, 2017 at 20:01
1
\$\begingroup\$

Japt, 51 47 bytes

"_{ç'=}_"+WçA="
|{ç}|" +(B="
+{ç'_}+" +(´V çA+B

Try it online!

Input is taken in the order width, height, depth.

\$\endgroup\$
1
\$\begingroup\$

dc, 108 bytes

?sdstsw95P61sc[lwsf[lcPlf1-dsf0<a]dsax]dsbx[_]p[[124P32sclbx[|]pld1-dsd0<j]dsjx43P95sclbx[+]plt1-dst0<h]dshx

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Röda, 65 bytes

a,b,c{d=" "*a;[`_${"="*a}_
`,`|$d|
`*(c-1),`|$d|
+${"_"*a}+
`*b]}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Java 7, 169 164 158 bytes

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;}

Try it online!

Ungolfed:

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;
}
\$\endgroup\$
1
  • \$\begingroup\$ You can save 1 byte by putting u=t+u+"+\n" inside the for-loop: for(u=t+u+"+\n;a[2]-->1;)s+=t;`. But nice answer, +1 from me. \$\endgroup\$ Commented Oct 30, 2017 at 12:43
1
\$\begingroup\$

Windows Batch, 211 180 163 bytes

Golfed a total of 48 bytes thanks to @Neil!

@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=
\$\endgroup\$
5
  • 1
    \$\begingroup\$ 1. Using @ on each line and after do is slightly shorter than @echo off. 2. @call set w=%%w%%_ avoids setlocal enabledelayedexpansion. 3. Use @for /l %%p in (2,1,%2). 4. IMHO you should add @set w= so that the code works more than once. \$\endgroup\$
    – Neil
    Commented May 23, 2017 at 7:59
  • \$\begingroup\$ I am confused by the last part, if I don't add @set w=, will the code not work for more then once? \$\endgroup\$
    – stevefestl
    Commented May 23, 2017 at 8:34
  • 1
    \$\begingroup\$ 1. Your @for /l %%p in (1,1,%1)do seems to have gone missing. 2. Your cake seems to be the wrong height, at least it doesn't seem to match the test case when I try it. 3. If you run the script twice in the same command session then the cake just gets wider and wider. \$\endgroup\$
    – Neil
    Commented May 23, 2017 at 8:49
  • \$\begingroup\$ @Neil It's all fixed :) \$\endgroup\$
    – stevefestl
    Commented May 23, 2017 at 8:57
  • 1
    \$\begingroup\$ Great! Just a few last things I noticed: 1. You don't need the @ after the &. 2 If you change the second loop to (2,1,%3) and the third loop to (1,1,%2) then I think you can delete the echo +%w%+ line. 3. If I've counted correctly, then I think you should fill w with spaces instead of _s, as that will result in fewer substitutions. (I would use @call set w= %%w%% as otherwise the space is hard to spot!) \$\endgroup\$
    – Neil
    Commented May 23, 2017 at 9:10
1
\$\begingroup\$

SOGL V0.12, 25 bytes

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

Try it Here!

Expects the input as width, depth, then height.

\$\endgroup\$
2
  • \$\begingroup\$ Is it allowed to re-order inputs? They seem to specified in a prescribed order. \$\endgroup\$
    – recursive
    Commented Oct 30, 2017 at 21:28
  • 1
    \$\begingroup\$ @recursive Usually it's allowed and nowhere does the question force the ordering and the 2nd most voted answer reorders the inputs and probably has been seen by the OP, so I'd assume it's fine. \$\endgroup\$
    – dzaima
    Commented Oct 30, 2017 at 21:32
1
\$\begingroup\$

Python 2, 124 122 120 105 92 bytes

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

-15 bytes by using STDIN instead of program arguments

-13 bytes by switching to Python 2 (for input()ing integers and print statement)

-12 bytes from Caird Coinheringaahing

Try it online!

Python 3, 124 122 120 105 bytes

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

Try it online!

If a full program is not required:

Python 3, 87 84 bytes

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

Try it online!

\$\endgroup\$
2
0
\$\begingroup\$

Javascript (ES6), 161 157 bytes

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));

\$\endgroup\$
0
\$\begingroup\$

Python 2, 93 bytes

i,j,k=input()
l=["|"+" "*i+"|"]
for x in["_"+"="*i+"_"]+l*(k-1)+(l+["+"+"_"*i+"+"])*j:print x

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 85 + 1 (-a) = 86 bytes

say"_".'='x($w=$F[0])."_\n".($l='|'.$"x$w."|\n")x($F[2]-1).("$l+".'_'x$w."+\n")x$F[1]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JavaScript/ES6, 90 bytes

I just wrote a rough solution, and happened to beat the existing JS answer by a whopping 56 bytes. Then I golfed off 11 bytes.

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

Here's a demo.

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);

\$\endgroup\$
0
\$\begingroup\$

Canvas, 23 bytes

|*_⁷⇵×+×∔║│P
=×_eP⁸╷{1⁸

Try it here!

\$\endgroup\$
0
\$\begingroup\$

Google Sheets, 89

Inputs A1, A2, A3.

  • A4 - _
  • A5 - (Discount 1 for ")
    ="|"&REPT(" ",A1)&"|
    "
    

Final output (Discount 1 for )):

=A4&REPT("=",A1)&"_
"&REPT(A5,A3-1)&REPT(A5&"+"&REPT(A4,A1)&"+
",A2)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.