Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Today is Bacon Day which is probably why the name of the hat for participating today is "Mmmm Bacon". What better way to celebrate bacon day with fun filled 2d matrices made out of bacon! A 1 by 1 bacon strip is represented by this:

----- 
)===)
(===(
)===)
-----

You goal here is given two coordinates in a tuple, (x, y), where x and y are nonzero positive integers, you are to create the bacon and return in some format (list, array, string).

Input and Output:

Input: (2, 1)

Output:
----------  
)===))===)
(===((===(
)===))===)
----------

Input: (1, 2)

Output:

----- 
)===)
(===(
)===)
----- 
)===)
(===(
)===)
-----

Input: (2, 2)

---------- 
)===))===)
(===((===(
)===))===)
---------- 
)===))===)
(===((===(
)===))===)
----------

Rules:

  • As you can see with the second test case, if multiple pieces of bacon are stacked together, only one ----- separates with each piece of bacon above and/or below it. That means stacking bacon like this is invalid:

    ----- 
    )===)
    (===(
    )===)
    -----
    ----- 
    )===)
    (===(
    )===)
    -----   
    
  • Standard loopholes are forbidden

  • The code must work for the above test cases and the following: (4, 4), (1, 6), (5, 1), (2, 3), (3, 2)

  • Provide an interpreter where the above test cases can be tested

Winning Criteria:

Shortest code wins! Happy Bacon Day to everyone!

share|improve this question
    
This conversation has been moved to chat. – Dennis 17 mins ago

16 Answers 16

V, 28, 26 bytes

Àñ2Á)3é=ñY4PÒ-G.MÓ)/(
kÀäG

Try it online!

Explanation:

Àñ      ñ             " Arg 1 times:
  2Á)                 "   Insert two ')'
     3é=              "   Insert 3 '='
         Y            " Yank this line
          4P          " Paste four times
            Ò-        " Replace this line with '-'
              G.      " Repeat on the last line
                M     " Move to the middle line
                 Ó)/( " Replace ')' with '('
k                     " Move up (to the second line)
 À                    " Arg 2 times
  äG                  " Duplicate everything up to the last line
share|improve this answer
    
Works for all test cases. Except that if I enter (14, 0), it returns the result for (14, 1). – PythonMaster yesterday
    
@Pythonmaster Yes, that's expected behavior. From your post it seems we do not need to handle inputs of 0. Is that true? If we need to handle inputs of 0, you should clarify that in your post, and I'll fix it for +1 byte. – DJMcMayhem yesterday
    
Oops never mind. I forgot about the "nonzero" part. – PythonMaster yesterday
3  
I love how this answer is only 3 bytes longer than a single bacon :D – FlipTack yesterday
3  
@FlipTack There are Newlines. It's actually 2 shorter. :P – DJMcMayhem yesterday

Python 2.7, 74 bytes

I'm sure this could be golfed some more, but this is what I came up with (Python's string multiplication feature sure comes in handy):

a,b=input();f="-"*5*a;d=")===)"*a;print'\n'.join([f,d,"(===("*a,d,''])*b+f

Try it here!

Ungolfed with explanation:

a,b = input()                                       # Take input from the user as a tuple
f = "-"*5 * a                                       # f is the delimiter between bacons
d = ")===)" * a                                     # 2nd and 4th lines of bacon
print '\n'.join([f, d, "(===("*a, d, ''])*b + f     # Join everything together!
share|improve this answer

TI-Basic, 80 bytes

This one was actually quite genius :)

":→Str0:Input :For(M,0,4Y:For(N,1,X:")===)
If not(fPart(M/4:"-----
If .5=fPart(M/4:"(===(
Str0+Ans→Str0:End:Ans+":→Str0:End
share|improve this answer
    
Also, for anyone wondering, in TI-Basic the colon and newline are interchangeable, and using Input with no arguments gets input by default into X and Y. – Timtech yesterday

Mathematica, 74 bytes

Array[b["-----",b=")===)","(===("][[#~Mod~4]]&,{4#2+1,#}]~Riffle~"\n"<>""&

Unnamed function taking two positive integer arguments and returning a string-with-newlines. A standard Mathematica approach: build a 2d array of strings using a (mod 4) chooser to cycle the strings in the vertical direction, then collapse them to a single string.

share|improve this answer

Batch, 168 bytes

@set s=
@for /l %%i in (1,1,%1)do @call set s=%%s%%-___-
@set t=%s:_==%
@echo %s:_=-%
@for /l %%i in (1,1,%2)do @echo %t:-=)%&echo %t:-=(%&echo %t:-=)%&echo %s:_=-%

Rather unfortunately I can't write @echo %s:==-% otherwise that would eliminate the necessity for the second variable.

share|improve this answer
    
Save 2 bytes by concatenating lines 1,2 and 3,4 with &... – Magoo 15 hours ago

05AB1E, 25 bytes

Code:

…)==û×Ь'(:s)¬g'-×=¸«»²F=

Explanation:

…)==û                         # Push the string ")===)"
     ×                        # String multiply by the first input
      Ð                       # Triplicate the string
       ¬                      # Take the first character, which is a ')' and push
        '(:                   # Replace by '('
           s                  # Swap the top two elements
            )                 # Wrap everything into an array
             ¬g               # Get the length of the first element in the array
               '-×            # And repeat the character '-' that many times
                  =           # Print it without popping
                   ¸«         # Append the string of dashes to the array
                     »        # Join by newlines
                      ²F      # Second input times do...
                        =     #   Print the top of the stack without popping

Uses the CP-1252 encoding. Try it online!

share|improve this answer

05AB1E, 32 30 bytes

Saved 2 bytes thanks to Adnan.

'-5×…)==ûÐ')'(:s)vy¹×})I.D¬)˜»

Try it online!

Explanation

'-5×                            # push "-----" 
    …)==ûÐ                      # push 3 copies of ")===)"
          ')'(:s                # replace ")" with "(" in the 2nd copy
                )               # wrap in list
                 vy¹×})         # repeat each list entry input-1 times
                       I.D      # repeat list input-2 times
                          ¬     # push the first element of the list ("-----")
                           )˜   # wrap in list and flatten
                             »  # join by newline
share|improve this answer
    
You can replace ")===)" by …)==û :) – Adnan 22 hours ago
    
@Adnan: Ah of course! Thanks! – Emigna 12 hours ago

C, 91 89 bytes

i;f(w,h){w=w*5+1;for(i=0;i<w*4*h+w;++i)putchar(i%w<w-1?i/w%4?i%w%5%4?61:40+i/w%2:45:10);}
share|improve this answer

Python 2, 70 bytes

def f(w,h):n=4*h+1;exec"n-=1;print'-)()-===-===-===-)()'[n%4::4]*w;"*n

Thanks to @xnor for saving 4 bytes!

Try it online!

share|improve this answer
    
It looks like the lines don't alternate directions in your test case. – xnor 22 hours ago
    
It looks like a [_::3] would be shorter. – xnor 21 hours ago
    
Oh shoot, something went wrong there. Deleting until I can fix it. – Dennis 21 hours ago
    
That fixed my code without adding any bytes. Thanks! Let's see if I manage to deduplicate those lines again. – Dennis 21 hours ago
    
There's 2**n%5%3 which cycles [1, 2, 1, 0, 1, 2, 1, 0, ...]. Might that be useful? – xnor 19 hours ago

JavaScript, 132 129 121 bytes

-8 bytes thanks to @user2428118

(x,y)=>{a=b=["-----",")===)","(===(",")===)","-----"];for(i=0;++i<y;){b=[...b,...a.slice(1)]}return b.map(v=>v.repeat(x)).join(`
`)}

(x,y)=>eval('a=b=["-----",")===)","(===(",")===)","-----"];for(i=0;++i<y;)b=[...b,...a.slice(1)];b.map(v=>v.repeat(x)).join`\n`')

(x,y)=>eval('a=b=[c="-----",d=")===)","(===(",d,c];for(i=0;++i<y;)b=[...b,...a.slice(1)];b.map(v=>v.repeat(x)).join`\n`')

This can quite probably be golfed more. If you have a suggestion, please leave it in the comments.

share|improve this answer
    
beat me by 1 byte.. :( – devRicher yesterday
    
@devRicher But I haven't exactly golfed mine yet. :) – ericw31415 yesterday
    
@devRicher Actually I accidentally miscounted the newline. Whoops. Edit: I've beat you now. – ericw31415 yesterday
1  
@ericw31415 (x,y)=>eval('a=b=[d="-----",c=")===)","(===(",c,d];for(i=0;+‌​+i<y;)b=[...b,...a.s‌​lice(1)].map(v=>v.re‌​peat(x)).join_BACKTI‌​CK NEWLINE BACKTICK_;') – user2428118 yesterday
    
@user2428118 This doesn't work? I can use your array simplification though. :) – ericw31415 23 mins ago

Lua, 132 bytes

a="-----"b=")===)"c="(===("w,z=io.read(),io.read()function g(f)return f:rep(w).."\n"end print((g(a)..g(b)..g(c)..g(b)):rep(z)..g(a))

Long, literal string attempt. Try it here.

share|improve this answer

JavaScript (ES6), 78

(x,y,r=s=>`${s}`.repeat(x)+`
`,a=r`)===)`,c=r`-----`)=>c+r(a+r`(===(`+a+c,x=y)

Test

F=
(x,y,r=s=>`${s}`.repeat(x)+`
`,a=r`)===)`,c=r`-----`)=>c+r(a+r`(===(`+a+c,x=y)

function update() {
  var x=+X.value,y=+Y.value
  O.textContent=F(x,y)
}

update()
X<input type=number id=X value=1 min=1 oninput='update()'>
Y<input type=number id=Y value=1 min=1 oninput='update()'>
<pre id=O></pre>

share|improve this answer

Jelly, 26 bytes

4“\ḊƭVṠ’bị“-=()”s5ẋ€ḷẋµ1ịṭ

This is a dyadic link (function) that returns a 2D array.

Try it online!

How it works

4“\ḊƭVṠ’bị“-=()”s5ẋ€ḷẋµ1ịṭ  Main link. Left argument: w. Right argument: h

4                           Set the return value to 4.
 “\ḊƭVṠ’b                   Yield 366323084456 and convert it to base 4.
                            This yields [1,1,1,1,1,0,2,2,2,0,3,2,2,2,3,0,2,2,2,0].
         ị“-=()”            Index into that string, using modular 1-based indexing.
                s5          Split the result into chunks of length 5.
                  ẋ€ḷ       Repeat the characters of each chunk w times.
                     ẋ      Repeat the array of chunks h times.
                      µ     Begin a new, monadic chain. Argument: M (bacon matrix)
                       1ị   Retrieve the first line.
                         ṭ  Tack; append it to M.
share|improve this answer

Python 2, 59 bytes

w,h=input()
for a in'-()('*h+'-':print(a+3*'=-'[a>')']+a)*w

Generates each line as a+b*3+a from the initial character a and the center character b (which is calculated from a). The a's cycle through '-()(', whereas b is '-' when a is '-', and '=' otherwise.


67 bytes:

w,h=input()
for a,b in['--']+zip(')()-','===-')*h:print(a+b*3+a)*w

Generates each line from its outer character a and center character b as a+b*3+a, then prints w copies of this. These cycle via a zip.

share|improve this answer

GameMaker Language, 160 139 148 bytes 133 bytes

x=argument0 y=argument1*4for(m=0;m<=y;m++){for(n=0;n<x;n++){a=")===)"if !m mod 4a="-----"else if n mod 2a="(===("r+=a}r+="#"}return r
share|improve this answer
    
Also, before you suggest it, GML requires each part of the for loop to have a statement inside :( – Timtech yesterday

C, 159 158 153 bytes

p(s,n){printf(s,--n?p(s,n):0);}i,j;b(n,m){p("-----",n);for(j=3;j--;){p("\n",1);for(i=n;i--;)p(j%2?"(===(":")===)",1);}p("\n",1);--m?b(n,m):p("-----",n);}

Call with:

int main()
{
    b(2,3);
}
share|improve this answer
    
Nice to see another answer in C, but it can be done quite a bit shorter, see my answer :) – orlp yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.