Print a Lego piece

48

7

This challenge is a simple one. Given two inputs, describing the height and width of a Lego piece, you have print an ASCII art representation of it.

Here is how the Lego pieces are supposed to look:

(4, 2)

___________
| o o o o |
| o o o o |
-----------

(8, 2)

___________________
| o o o o o o o o |
| o o o o o o o o |
-------------------

(4, 4)

___________
| o o o o |
| o o o o |
| o o o o |
| o o o o |
-----------

(3, 2)

_________
| o o o |
| o o o |
---------

(1, 1)

o

If you can't tell from the test-cases, the top and bottom are width*2+3 underscores and dashes, and each row has pipes for the sides, o's for the little things, and everything is separated by spaces.

The only exception for this is (1, 1), which is just a single o.

You will never get 0 for any of the dimensions.

This is , so shortest code in bytes wins!

var QUESTION_ID=84050,OVERRIDE_USER=31343;function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"http://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

Maltysen

Posted 2016-06-30T00:27:43.247

Reputation: 25 023

2Is it possible that the width or the height will be greater than 10? What range should we support? – James – 2016-06-30T02:37:43.823

29The special case is a real bummer. – Conor O'Brien – 2016-06-30T03:03:17.260

@DrGreenEggsandIronMan definitely could be greater than 10. I guess whatever integer range your language supports (bearing in mind the standard loophole about abusing this). – Maltysen – 2016-06-30T03:28:39.820

Do (2, 1) and (1, 2) look similar to the bigger ones? Can we have an extra space at the end of each line? Ending newline required or optional? – aditsu quit because SE is EVIL – 2016-06-30T05:37:12.393

@aditsu yeah, they do, (1, 1) is the only exception. Trailing whitespace is not allowed, except for a single trailing newline. – Maltysen – 2016-06-30T05:47:05.027

47In the next few years, I want to see another "Print a Lego piece" challenge that requires writing the code to tell a 3D printer to produce a Lego. – Kevin – 2016-06-30T08:47:06.873

- will be better as " – None – 2016-06-30T13:30:26.917

@Kevin such a thing can already be done - see (e.g.) http://www.thingiverse.com/thing:231651 for an example of exactly that.

– Nathaniel – 2016-06-30T13:39:53.970

@Kevin i was hoping for something like this when i clicked on this hot network question – Gigala – 2016-06-30T15:03:50.160

8Wait, "whatever integer range your language supports"? That ain't how LEGO works. The bricks are only available in a handful of very specific dimensions. Even if you add in plates, you only get a couple more. Any script that does not discard input such as (1,7) or (5,3) is complete garbage. – RegDwight – 2016-07-01T14:43:55.657

3Why doesn't the single piece (1,1) have sides? There is a real Lego piece with a single nipple on top of a cube. – tcrosley – 2016-07-02T00:00:58.190

@tcrosley i know, I just wanted a special case, and there also exist (1,1) lego pieces without sides – Maltysen – 2016-07-02T00:03:32.517

Really (re ones with no sides). I don't think the sets our grand-kids play with have any like that, but there's aren't the mega sets with hundreds of pieces. – tcrosley – 2016-07-02T00:10:08.127

1@RegDwight you are only seeing the top of it. It could be bound underneath. – Conor O'Brien – 2016-07-03T16:26:15.960

1

@tcrosley this kinda thing: http://s.ecrater.com/stores/196820/5355aaa76f14f_196820b.jpg

– Maltysen – 2016-07-03T21:07:27.993

http://www.thingiverse.com/search/page:1?q=lego – Sparr – 2016-10-08T16:15:11.057

a top down 1x1 lego view – FantaC – 2018-01-12T00:01:12.687

Answers

53

Befunge, 165 227 bytes

&::&*:1` v
v+3*2:\/\_"o",@
v       _$ v<
>"_",1-:^  2
v:,,"| ",*5<
v         _v
>" o",,1-:^$
>*v>\     #\^
5 #|:-1,"|"<
^2$<
v1, *95<
- >2*3+^
>:    #^_@

Not as much whitespace as before, but there are still gaps. The principle is the same as in the previous solution, but the layout is different. This time, to check if both numbers are 1, I'm just taking their product and seeing if the result is greater than 1.


Old solution (227 bytes)

v           v       <
&   >>\:2*3+>"_",1-:|
>&:1`|v ,,"| ",*52:$<   :\<
    #\v         <
     :>" o",,1-:|
     1          >"|",$\1-:|
    \`            @       $
    ^_"o",@>:!    |       2
           ^-1,*95<+3*2,*5<

It might be possible to golf it more. Just look at all that whitespace!

Here's my poor attempt at an explanation in MSPaint picture form: How to Befunge Code flows in the direction of the arrow.

user55852

Posted 2016-06-30T00:27:43.247

Reputation:

2oh. my. god. Blows my mind – lukas.pukenis – 2016-07-01T10:38:11.893

25

V, 43, 40, 38 36 bytes

One of the longest V answers I've ever written...

Àio ddÀPñóo î½o
u2Pí.«/| °|
Vr-HVr_

Try it online!

Since this contains unicode and unprintable characters, here is a reversible hexdump:

0000000: c069 6f20 1b64 64c0 50f1 f36f 20ee bd6f  .io .dd.P..o ..o
0000010: 0d0a 7532 50ed 2eab 2f7c 20b0 7c0d 0a56  ..u2P.../| .|..V
0000020: 722d 4856 725f                           r-HVr_

This challenge is about manipulating text, so perfect for V! On the other hand, V is terrible at conditionals and math, so the differing output for (1, 1) really screwed it up... :(

Explanation:

À                   "Arg1 times:
 io <esc>           "Insert 'o '
         dd         "Delete this line, and
           À        "Arg2 times:
            P       "Paste it

Now we have 'Height' lines of o's with spaces between them.

ñ                   "Wrap all of the next lines in a macro. This makes it so that if any 
                    "Search fails, execution will stop (to handle for the [1, 1] case)
 ó                  "Search and replace
  o î½o             "'o'+space+0 or 1 newlines+another 'o'

u                   "Undo this last search/replace
 2P                 "Paste twice
   í                "Search and replace on every line
    .«/| °|         "A compressed regex. This surrounds every non-empty line with bars.

Vr-                 "Replace the current (last) line with '-'
   H                "Move to line one
    Vr_             "Replace this line with '_'

Non-competing version (31 bytes):

Try it online!

This version uses several features that are newer then this challenge to be 5 bytes shorter!

Second explanation:

ddÀP

which is "Delete line, and paste it n times" is replaced with ÀÄ which is "Repeat this line n times". (-2 bytes)

óo î½o
u

which was "Replace the first match of this regex; Undo" was replaced with

/o î½o

Which is just "Search for a match of this regex" (-1 byte)

And lastly, Ò is just a simple synonym for Vr, which both "Replace every character on this line with 'x'". (-2 bytes)

James

Posted 2016-06-30T00:27:43.247

Reputation: 54 537

how come it seems broken at the bottom with this http://v.tryitonline.net/#code=w4BpbyAbZGTDgFDDscOzbyDDrsK9bwp1MlDDrS7Cqy98IMKwfApWci1IVnJf&input=&args=NTA+OTk5

– metersk – 2016-07-01T17:44:13.143

2

@meepl I really have no idea. It works on 50x959 but if you increase the width or height it stops working. I'm guessing it's most likely a restriction intentionally placed on the website to prevent extremely large programs from being ran.

– James – 2016-07-01T17:50:47.253

1TIO limits the output to 100 KB, mainly to prevent the frontend from crashing your browser. – Dennis – 2016-07-27T01:26:23.430

22

32 16-bit little-endian x86 machine code, 57 54 51 bytes

3 bytes less thanks to @ninjalj.

Heavily rewrote the code and have managed to shave off another 3 bytes

In hex

FCBA6F208D48FFE20492AAEB2389D941D1E14151B05FF3AAEB0BB87C20AB89D992F3AB92AAB00AAA4E7DEF59B02DF3AA91AAC3

Input: BX=width, SI=height, DI points to the buffer that receives result as a NULL-terminated string with lines separated by "\n"

Disassembly:

fc            cld
ba 6f 20      mov    dx,0x206f      ;Storing ' o' in DX for later use
8d 48 ff      lea    cx,[bx+si-0x1] ;CX=width+height-1
e2 04         loop   _main0         ;--CX & brahch if not zero
92            xchg   dx,ax          ;(1,1) case, swap DX & AX
aa            stosb                 ;AL == 'o', CX == 0
eb 23         jmp    _end
_main0:
89 d9         mov    cx,bx
41            inc    cx
d1 e1         shl    cx,1
41            inc    cx           ;Calculate (width+1)*2+1
51            push   cx           ;and save it for future use
b0 5f         mov    al,0x5f      ;'_'
f3 aa         rep    stosb        ;Output the whole line of them
eb 0b         jmp    _loopstart   ;Jump into the loop
_loop:
b8 7c 20      mov    ax,0x207c    ;' |'
ab            stosw               ;Output it once (left bar + space)
89 d9         mov    cx,bx        ;Copy width
92            xchg   dx,ax        ;AX == ' o'
f3 ab         rep    stosw        ;Output it CX times
92            xchg   dx,ax        ;Swap values back, AL == '|'
aa            stosb               ;Output only the right bar
_loopstart:
b0 0a         mov    al,0x0a      ;Newline. Can be replaced with mov ax,0x0a0d for windows newline
aa            stosb               ;convention (at the cost of 1 byte), with stosb replaced with stosw
4e            dec    si           ;Height--
7d ef         jge    _loop        ;Continue if si >= 0 (this accounts for the dummy first pass)
59            pop    cx
b0 2d         mov    al,0x2d      ;'-'
f3 aa         rep    stosb        ;Output bottom line
_end:
91            xchg   cx,ax        ;CX == 0, so swap to get zero in AL
aa            stosb               ;NULL-terminate output
c3            retn

meden

Posted 2016-06-30T00:27:43.247

Reputation: 711

Would be shorter as 16-bit: -3 bytes for 3 66h prefixes, +1 byte for "\r\n" line termination. – ninjalj – 2016-07-04T17:54:59.367

You should put spaces between the crossed-out numbers and the current numbers in your byte count, for readability. – Value Ink – 2016-07-08T20:34:01.690

20

Python 2, 75 73 72 bytes

lambda x,y:(x*'__'+'___\n'+('| '+'o '*x+'|\n')*y+'-'*(x*2+3),'o')[x<2>y]

Returns a string, with a conditional to handle the 1,1 block.

Thanks to Lynn and Chepner for two bytes

atlasologist

Posted 2016-06-30T00:27:43.247

Reputation: 2 945

lambda x,y:('_'*x*2+'___\n'+ etc. saves a byte. – Lynn – 2016-06-30T01:31:13.050

1Knock off another byte with x*'__' instead of 2*x*'_'. – chepner – 2016-07-01T14:24:29.120

Just join this Comunity, sorry for asking. How can i see it run? i paste it in the terminal and just prints <function <lambda> at 0x......>. How can i test this? – Miguel – 2016-07-02T10:29:42.270

1@Miguel assign it to a variable. It'll return the value of the function: f=lambda x:x+1; print(f(9)) – atlasologist – 2016-07-02T11:23:39.290

One more question if not to complicated to answer. How can you trace the bits so precisely? – Miguel – 2016-07-02T11:30:22.573

@Miguel, 1 character = 1 byte. – TheChetan – 2016-07-04T17:41:54.640

13

CJam, 34

'_q~'o*"||"\*S*f*f+'-f+zN*_,H='o@?

Try it online

Explanation:

'_        push a '_' character
q~        read and evaluate the input (height and width)
'o*       repeat the 'o' character <width> times
"||"\*    join the "||" string by the string of o's (putting them in between)
S*        join with spaces (inserting a space between every 2 characters)
f*        repeat each character <height> times, making it a separate string
f+        prepend '_' to each string
'-f+      append '-' to each string
z         transpose the array of strings
N*        join with newlines; lego piece is ready, special case to follow
_,        duplicate the string and get its length
H=        compare with H=17
'o        push 'o' for the true case
@         bring the lego piece to the top for the false case
?         if the length was 17, use 'o' else use the lego piece

aditsu quit because SE is EVIL

Posted 2016-06-30T00:27:43.247

Reputation: 22 326

11

Ruby, 59 56 bytes

Anonymous function, returns a multiline string. Try it online!

-3 bytes thanks to borrowing a trick from @El'endiaStarman

->w,h{w*h<2??o:?_*(x=2*w+3)+$/+(?|+' o'*w+" |
")*h+?-*x}

Value Ink

Posted 2016-06-30T00:27:43.247

Reputation: 10 608

11

Java, 318 312 297 294 260 258 bytes

Saved 15 bytes thanks to cliffroot!

interface a{static void main(String[]A){int b=Byte.valueOf(A[0]),B=Byte.valueOf(A[1]),C=3+b*2;String c="";if(b<2&B<2)c="o";else{for(;C-->0;)c+="_";for(;B-->0;){c+="\n|";for(C=b;C-->0;)c+=" o";c+=" |";}c+="\n";for(C=3+b*2;C-->0;)c+="-";}System.out.print(c);}}

It works with command line arguments.

Ungolfed In a human-readable form:

interface a {
    static void main(String[] A) {
        int b = Byte.valueOf(A[0]),
            B = Byte.valueOf(A[1]),
            C = 3 + b*2;
        String c = "";
        if (b < 2 & B < 2)
            c = "o";
        else {
            for (; C-- > 0;)
                c += "_";
            for (; B-- > 0;) {
                c += "\n|";
                for (C = b; C-- >0;)
                    c += " o";
                c += " |";
            }
            c += "\n";
            for(C = 3 + b*2; C-- >0;)
                c += "-";
        }
        System.out.print(c);
    }
}

Yes, it's still difficult to understand what's going on even when the program is ungolfed. So here goes a step-by-step explanation:

static void main(String[] A)

The first two command line arguments -which we'll use to get dimensions- can be used in the program as A[0] and A[1] (respectively).

int b = Byte.valueOf(A[0]),
    B = Byte.valueOf(A[1]),
    C = 3 + b*2;
String c = "";

b is the number of columns, B is the number of rows and C is a variable dedicated for use in for loops.

c is the Lego piece. We'll append rows to it and then print it at the end.

if (b < 2 & B < 2)
    c = "o";
else {

If the piece to be printed is 1x1, then both b (number of columns) and B (number of rows) should be smaller than 2. So we simply set c to a single o and then skip to the statement that System.out.prints the piece if that's the case.

for (; C-- > 0; C)
    c += "_";

Here, we append (integerValueOfA[0] * 2) + 3 underscores to c. This is the topmost row above all holes.

for (; B > 0; B--) {
    c += "\n|";
    for(C = b; C-- > 0;)
        c+=" o";
    c += " |";
}

This is the loop where we construct the piece one row at a time. What's going on inside is impossible to explain without examples. Let's say that the piece is 4x4:

Before entering the loop, c looks like this:
___________

After the first iteration (\n denotes a line feed):
___________\n
| o o o o |

After the second iteration:
___________\n
| o o o o |\n
| o o o o |

After the third iteration:
___________\n
| o o o o |\n
| o o o o |\n
| o o o o |

.

c += "\n";
for (C = 3 + b*2; C-- > 0;)
    c += "-";

Here, we append (integerValueOfA[0] * 2) + 3 hyphens to the piece. This is the row at the very bottom, below all holes.

The 4x4 piece I used for explaining the for loop where the piece is actually constructed now looks like this:

___________\n
| o o o o |\n
| o o o o |\n
| o o o o |\n
| o o o o |\n
-----------
System.out.print(c);

And finally, we print the piece!

user8397947

Posted 2016-06-30T00:27:43.247

Reputation: 1 242

Probably Revision 3 made this the longest post I've ever made on Stack Exchange.

– user8397947 – 2016-06-30T03:57:24.457

2

You can move C variable from for loops int b=Byte.valueOf(A[0]),B=Byte.valueOf(A[1]),C. In all your for loops it also seems like you can use C-->0; checks, makes it 298, http://pastebin.com/uj42JueL

– cliffroot – 2016-06-30T07:51:26.403

1

some creative usage of for loops for few bytes saved – http://pastebin.com/dhNCpi6n

– cliffroot – 2016-06-30T08:23:39.293

1if you convert your arguments to bytes first, then your check is size of brick is 1x1 will be if(b==1&B==1) which allows you to save over 20 bytes – user902383 – 2016-07-01T10:34:42.393

also for the case 1x1 instead doing this System.out.print('o');return;, you could set c='o' and placed logic for different bricks in else block. then having single print statement and no return allow you to save some additional bytes – user902383 – 2016-07-01T10:44:19.110

What is the \r nonsense? I don't think this produces the required output... – R.. GitHub STOP HELPING ICE – 2016-07-03T19:30:24.020

@R.. \r is a carriage return and \n is a linefeed. Because a carriage return followed by a linefeed is interpreted as a single newline on pretty much all modern computers thanks to Microsoft, I decided to make the code shorter by not sticking to one type of line ending.

– user8397947 – 2016-07-03T19:37:44.003

@dorukayhan: Even if we do accept that claim (which I don't; \r is junk before the newline in my book and in all text editors I use), a lone \r, which you have separating the last 2 "lines", is certainly not a newline. – R.. GitHub STOP HELPING ICE – 2016-07-03T19:54:29.433

@R.. I just fixed it.

– user8397947 – 2016-07-03T23:59:07.890

Turns out I don't need \rs! – user8397947 – 2016-07-12T18:06:56.360

I know it's been almost a year, but you can still golf quite a few things for -20 bytes: (238 bytes) interface a{static void main(String[]A){int b=new Byte(A[0]),B=new Byte(A[1]),C=3+b*2,D=C;String c="o";if(b>1|B>1){for(c="";C-->0;c+="_");for(;B-->0;c+=" |")for(c+="\n|",C=b;C-->0;c+=" o");for(c+="\n";D-->0;c+="-");}System.out.print(c);}} Summary of what I've changed: Byte.valueOf(..) to new Byte(..); Removed else; I've put everything inside the for-loops (both at the front and back) to limit brackets and semicolons; Removed the second C=3+b*2 by adding a ,D=C after the first. – Kevin Cruijssen – 2017-05-04T13:23:58.187

9

Minkolang 0.15, 58 57 56 bytes

Yes, that's right. I golfed off one two stinkin' little bytes...

nn$d*1-5&"o"O.rd2*3+$z1$([" o"]" ||"2Rlkr$Dlz["-_"0G]$O.

Try it here!

Explanation

nn                Take two numbers from input (stack is now [w h])

                  C special case C
  $d              Duplicate stack
    *1-           Multiply and subtract 1
       5&         Jump 5 spaces if truthy
         "o"O.    Top of stack was 1*1-1=0, so output "o" and stop.

                     C precalculates width of top and bottom lines for later use C
         r           Reverse stack (now [h w])
          d          Duplicate top of stack
           2*3+      Multiply top of stack by 2 and add 3
               $z    Pop this and store in register (z = 2*w+3)

                                       C generates the row C
                 1$(                   Open while loop prepopulated with top of stack
                    [" o"]             w times, push "o "
                          " ||"        Push "|| "
                               2R      Rotate twice to the right
                                 l     Push newline (10)
                                  k    Break out of while loop

                                           C duplicates the row h times C
                                   r       Reverse stack
                                    $D     Duplicate whole stack h times
                                      l    Push newline

                     C this is for the top and bottom lines C
        z[           Open for loop that repeats z times
          "-_"       Push "_-"
              0G     Relocate top of stack to bottom of stack
                ]    Close for loop

                 $O.    Output whole stack as characters and stop.

Okay, that's two significant rewrites of the explanation for two bytes saved. I don't think I can or will golf anything more out of this one. :P

El'endia Starman

Posted 2016-06-30T00:27:43.247

Reputation: 14 504

3The first byte is the hardest step... the beginning of a 1000 bytes begins with a single bit... – Conor O'Brien – 2016-06-30T01:37:23.987

8

brainfuck, 391 bytes

I know this can be golfed down more, but at this point I'm just glad it works. I will continue to work to golf it down.

+++++[-<+++[-<+++<++++++<+++++++<++<++++++++>>>>>]<<+<+<<+<++>>>>>>>]<<<<+<++<->>>>>>,<,>>++++++[<--------<-------->>-]<<->>+<<[>>-<<[>>>+<<<-]]>>>[<<<+>>>-]<[<->>>+<<<[>>>-<<<[>>>>+<<<<-]]>>>>[<<<<+>>>>-]<[<<<<<<<.[-].]<<<+>-]<<+>[->++>+<<]>+++[-<<<<.>>>>]>[-<<+>>]<<<<<<<<<.>>>>>>><[->[->+>+<<]<<<<<<.>>>>>>>[<<<<<<.>.>>>>>-]>[-<<+>>]<<<<<<<.<.<.>>>>>>]>[->++>+<<]>+++[-<<<.>>>]>[-<<+>>]<<

Input needs to be given as just two digits. As in, to do (8, 2) you would just enter 82.

Try it online!

Breakdown:

First put the necessary characters into the tape: (newline)| o_-

+++++[-<+++[-<+++<++++++<+++++++<++<++++++++>>>>>]<<+<+<<+<++>>>>>>>]<<<<+<++<->>>>>

Then collect the input into two cells and subtract 48 from each (to get the numeric value and not the numeral character)

>,<,>>++++++[<--------<-------->>-]<<

Next, check for the special case of (1, 1) (Note that just this check accounts for 109 bytes of the code). As if ifs weren't hard enough to do in brainfuck, we have a nested if:

->>+<<[>>-<<[>>>+<<<-]]>>>[<<<+>>>-]<[<->>>+<<<[>>>-<<<[>>>>+<<<<-]]>>>>[<<<<+>>>>-]<[<<<<<<<.[-].]<<<+>-]<<+

The following is the structure to check if a cell x is zero or nonzero:

temp0[-]+
temp1[-]
x[
 code1
 temp0-
 x[temp1+x-]
]
temp1[x+temp1-]
temp0[
 code2
temp0-]

However in a nested if, we need to have 4 temporary cells.

Now we get to the actual printing of characters:

Print the top bar and a newline:

>[->++>+<<]>+++[-<<<<.>>>>]>[-<<+>>]<<<<<<<<<.>>>>>>>

Print a |, a row of o's, another | and a newline a number of times equal to the height:

<[->[->+>+<<]<<<<<<.>>>>>>>[<<<<<<.>.>>>>>-]>[-<<+>>]<<<<<<<.<.<.>>>>>>] 

And print the bottom bar (no newline needed here):

>[->++>+<<]>+++[-<<<.>>>]>[-<<+>>]<<

Business Cat

Posted 2016-06-30T00:27:43.247

Reputation: 8 927

7

Jolf, 36 bytes

?w*jJρΡ,a+3ώj+2J'-"-+"d*'_lH"  ' o'o
?w*jJ                             'o   return "o" if j * J - 1
       ,a                              make a box
         +3ώj                           of width +3ώj = 3+2*j
             +2J                        of height +2J = 2+J
                '-                      specifying corners as "-"
      Ρ           "-+"                 replacing the first run of "-"s
                      d*'_lH            with a run of "_"s of equal length
     ρ                      "  '       replacing all "  "
                               ' o      with " o"

Jolf, 24 bytes, noncompeting

Well, I made a better box builtin.

?w*jJ,AhώjJ"_-'_-'|' o'o

Jolf, 38 37 bytes

?w*jJΆ+*'_γ+3ώjS*JΆ'|*j" o' |
"*'-γ'o

Simple stuff, really. Saved a byte by noting that (math zeta, or the stand deviance) is only 0 when both arguments are 1, and is falsey otherwise (for our case).

Conor O'Brien

Posted 2016-06-30T00:27:43.247

Reputation: 36 228

7

Retina, 52 bytes

Byte count assumes ISO 8859-1 encoding. Note that the sixth line is supposed to contain a single space.

\G1?
$_¶
1* 



.+
|$&|
\G.
_
r`.\G
-
s`^.{17}$|1
o

Try it online!

Input is in unary, using 1 as the unary digit, space as a separator and height followed by width.

Explanation

All stages in this program are bare substitutions, occasionally with a regular regex modifier (no repetition or loops, no other types of stages). It doesn't even use Retina-specific substitution features, apart from the usual alias for linefeeds.

\G1?
$_¶

The purpose of this is to "multiply" the two inputs. Our goal is to create h+2 rows with w 1s each (h+2 so that we can turn the top and bottom into _ and - later). The \G anchor requires the match to start where the last one left off. That is, if we ever fail to match a character in the string, further characters won't match either. We use this to match only the 1s in h, but not in w because the regex doesn't allow the space that separates them to be matched. However, we also make the 1 optional, so that we get an additional empty match at the end of h. That's h+1 matches. Each of those is replaced with the entire input ($_) followed by a linefeed. w itself remains untouched which gives us the h+2nd copy. Say the input was 11 1111, then we've now got:

11 1111
11 1111
11 1111
 1111

That's pretty good. We've got some extra stuff, but the h+2 copies of w are there.

1* 

Note that there's a space at the end of the first line. This removes those prefixes from the lines so that we only have the ws afterwards.



Ah well, that doesn't really work with SE's formatting... the first line is empty and the second line is supposed to contain a single space. This inserts spaces into every possible position, i.e. at the beginning and end of each line and between every pair of 1s:

 1 1 1 1 
 1 1 1 1 
 1 1 1 1 
 1 1 1 1 

We will turn these into os later

.+
|$&|

This simply wraps every line in a pair of |:

| 1 1 1 1 |
| 1 1 1 1 |
| 1 1 1 1 |
| 1 1 1 1 |

Now we take care of the top and bottom:

\G.
_

Time for \G to shine again. This matches each character on the first line and turns it into a _.

r`.\G
-

Same thing, but due to the r modifier (right-to-left mode), this matches the characters on the last line and turns them into -. So now we've got:

___________
| 1 1 1 1 |
| 1 1 1 1 |
-----------

There's only two things left to do now: turn those 1 into os, and if the input was 1 1 then turn the entire thing into o instead. We can handle both of those with a single stage:

s`^.{17}$|1
o

s is regular singleline mode (i.e. it makes . match linefeeds). If the input was 1 1 the result will have the minimum size of 17 characters so we can match it with ^.{17}$ and replace it with o. Otherwise, if that fails, we'll just match all the 1s and replace those with o instead.

Martin Ender

Posted 2016-06-30T00:27:43.247

Reputation: 184 808

6

JavaScript (ES6), 89 86 bytes

(x,y,g=c=>c[r=`repeat`](x*2+3))=>x*y-1?g(`_`)+`
`+`| ${`o `[r](x)}|
`[r](y)+g(`-`):`o`

Edit: Saved 3 bytes thanks to @Shaggy.

Neil

Posted 2016-06-30T00:27:43.247

Reputation: 95 035

Save 3 bytes by aliasing repeat. – Shaggy – 2017-05-02T22:25:21.277

6

05AB1E, 33 bytes

Code:

*i'oë¹·3+©'_׶²F'|„ o¹×„ |¶}®'-×J

Explanation:

*i'o                               # If both input equal 1, push "o"
    ë                              # Else, do...
     ¹·3+                          # Push input_1 × 2 + 3
         ©                         # Copy this number to the register
          '_×                      # Multiply by "_"
             ¶                     # Push a newline character
              ²F           }       # Do the following input_2 times:
                '|                 # Push "|"
                  „ o              # Push " o"
                     ¹×            # Multiply this by input_1
                       „ |         # Push " |"
                          ¶        # Push a newline character
                            ®      # Retrieve the value from the register
                             '-×   # Multiply by "-"
                                J  # Join everything and implicitly print.

Uses the CP-1252 encoding. Try it online!.

Adnan

Posted 2016-06-30T00:27:43.247

Reputation: 41 965

5

Python 2, 71 bytes

lambda x,y:('o',x*'__'+'___\n'+'| %s|\n'%('o '*x)*y+'-'*(x*2+3))[x+y>2]

Lulhum

Posted 2016-06-30T00:27:43.247

Reputation: 381

1Welcome to PPCG! Nice first post! – Rɪᴋᴇʀ – 2016-07-05T14:35:03.117

5

Befunge, 144 Bytes

I would have prefered to comment to this post, but I don't have the reputation yet, so I'm putting an answer of my own, which works a similar way, but is slightly more compact

&::&*:1`v
v3*2:\/\_"o",@
>+:  v   >52*," |",, v
>,1-:vLEG O MAKERv::\<
^"_" _$\:|<v "o "_v
v52:+3*2$<,>,,1-:^$
>*,v <    ^"|":-1\<
v-1_@,
>:"-"^

you can test the code here

Maliafo

Posted 2016-06-30T00:27:43.247

Reputation: 361

4

Reng v.4, 82 bytes, noncompeting

I pushed a bug fix that fixes functions being overwritten by themselves (please don't ask; my stuff is haunted)

i#wi#hhw+2e1+ø ~*x}o:{"-"ö<
"_"{:o}w2*3+#xx*2ø
"o"o~
ö"|"o"o"{Wo:o}w*"| "ooh1-?^#h

Takes input as space-joined numbers, like 4 2. Try it here!

Conor O'Brien

Posted 2016-06-30T00:27:43.247

Reputation: 36 228

8I pushed a bug fix that fixes functions being overwritten by themselves ... Well, that's an interesting bug to have – MKII – 2016-07-01T07:45:31.517

4

Bash, 186,163,156, 148,131, 130 Bytes

 ## Arg1 - Lego width
 ## Arg2 - Lego height 
function print_lego() { 
(($1+$2>2))&&{
printf _%.0s `seq -1 $1`
echo
for((i=$2;i--;)){ 
 printf \|
 for((j=$1;j--;)){
  printf o
 }
 echo \| 
}
printf =%.0s `seq -1 $1`
echo 
}||echo o
}

Note: If you really need the lego to have hyphens for the last line, then change the last printf to

printf -- -%.0s `seq -1 $1`

and add two bytes.

user53101

Posted 2016-06-30T00:27:43.247

Reputation:

2Wouldn't this be quite a bit shorter if it wasn't wrapped in a function? Also, I'm not an expert in bash but it looks like it's got some extra whitespace. – James – 2016-06-30T04:26:48.177

It would be ~170 as a one-liner: (($x+$y==2))&&echo o||{ printf _%.0s $(seq -1 $x);echo;for((i=0;i<$y;i++));do printf \|;for((j=0;j<$x;j++));do printf o;done;echo \|;done;printf =%.0s $(seq -1 $x);echo;} – None – 2016-06-30T13:00:31.437

1If you use (), you don't need the keyword function to declare a function. There is an alternate for syntax using braces, e.g: for((j=$1;j--;));{ printf o;}. As shown in the previous example, you can save some characters by decrementing and testing in for's second expression. You can use backticks instead of $(cmd). – ninjalj – 2016-06-30T19:48:40.377

@ninjalj Thanks, I'm new to code golf -- that squeezes another ~17 bytes off, the one-liner is now 152: (($x+$y==2))&&echo o||{ printf _%.0s \seq -1 $x`;echo;for((i=$y;i--;)){ printf |;for((j=$x;j--;)){ printf o;};echo |;};printf =%.0s `seq -1 $x`;echo;}` – None – 2016-07-01T00:28:36.560

Dollar signs are optional in arithmetic context, so you can shave a few more bytes by changing (($something)) to ((something)) throughout. ($1 still needs the dollar sign to disambiguate it from the literal 1.) – tripleee – 2018-05-02T09:53:44.547

4

PowerShell v2+, 76 bytes

param($x,$y)if($x+$y-2){"_"*($z=$x*2+3);"|$(" o"*$x) |`n"*$y+'-'*$z;exit}"o"

Takes input, then checks an if statement. Since non-zero values are truthy in PowerShell, so long as at least one of $x and $y are not equal to 1, the if will be true.

Inside the if, there's a series of string multiplications. First, we construct our string of underscores, saving $z for later. That gets placed on the pipeline. Next we construct our string of sides and pegs (with the pegs multiplied by $x), done $y times, and concatenate that with our dashes $z times. That string is then placed on the pipeline and we exit. The pipeline is flushed and printing is implicit. Note that we get the newline between the underscores and the first row of pegs for free, since the default .ToString() separator for array output is `n (and we're outputting an array of strings).

If the if is false, we're in the special 1 1 case, so we just put "o" by itself on the pipeline and exit, with printing again implicit.

Examples

PS C:\Tools\Scripts\golfing> .\print-a-lego-piece.ps1 1 1
o

PS C:\Tools\Scripts\golfing> .\print-a-lego-piece.ps1 5 3
_____________
| o o o o o |
| o o o o o |
| o o o o o |
-------------

AdmBorkBork

Posted 2016-06-30T00:27:43.247

Reputation: 41 581

4

Perl 5 - 84 77 bytes

84 Bytes

sub l{($x,$y)=@_;$w=3+2*$x;warn$x*$y<2?"o":'_'x$w.$/.('| '.'o 'x$x."|\n")x$y.'-'x$w}

77 Bytes. With some help from Dom Hastings

sub l{($x,$y)=@_;$x*$y<2?o:'_'x($w=3+2*$x).('
| '.'o 'x$x."|")x$y.$/.'-'x$w}

Kaundur

Posted 2016-06-30T00:27:43.247

Reputation: 71

First I was confused as to why someone would go to the effort of using warn in a golf program, but then I realized you're using it because it's shorter than print. Nice! – pipe – 2016-07-03T13:11:43.527

Yeah, I think in Perl 6 you can take another byte off by using say instead of warn – Kaundur – 2016-07-03T18:34:53.720

1You can do that in Perl 5 too, just that it's not enabled by default. I think that you can get around that in code-golf by calling your script from the command line with -E instead of -e, enabling all the extensions. I'm new to this place so I don't know exactly where it's specified how to count the scores though. – pipe – 2016-07-03T18:46:31.947

Oh really, I didn't know that. I'm new here as well so I'm also not sure – Kaundur – 2016-07-04T06:59:50.683

I think you can shorten this to 76 bytes... If you're using a function I believe returning the string is acceptable (see the JS answer, saving you 4 bytes for warn), you don't need quotes around the "o" (you can use a bareword for another -2), if you inline the calculation of $w you should save another byte ('_'x($w=3+2*$x) vs. $w=3+2*$x; ... '_'x$w) and lastly, you can change the \n for a literal newline. Hope that helps! – Dom Hastings – 2016-07-05T09:23:59.393

Ah nice, I didn't spot the inline assignment. The literal newline is amazing – Kaundur – 2016-07-05T12:32:15.163

@Kaundur Happy to help, I think you can drop the last .$/ as well since there doesn't appear to be a trailing newline requirement... Can't think of any more savings just yet... – Dom Hastings – 2016-07-05T15:49:00.037

@DomHastings Correct again, must have been left over from some earlier version of the code – Kaundur – 2016-07-05T20:01:12.027

Cut it down to 70 including a -p: Try it online!

– Xcali – 2018-01-11T20:39:48.097

3

Batch, 172 170 bytes

@echo off
if "%*"=="1 1" echo o&exit/b
set o=
for /l %%i in (1,1,%1)do call set o=%%o%% o
echo ---%o: o=--%
for /l %%i in (1,1,%2)do echo ^|%o% ^|
echo ---%o: o=--%

Edit: Saved 2 bytes thanks to @CᴏɴᴏʀO'Bʀɪᴇɴ @EʀɪᴋᴛʜᴇGᴏʟғᴇʀ.

I can save 7 bytes if I can assume delayed expansion is enabled.

Neil

Posted 2016-06-30T00:27:43.247

Reputation: 95 035

%%o%% instead of %o%? – Erik the Outgolfer – 2016-06-30T18:47:28.897

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ %o% would be replaced with original value of o each time, so that o would only ever equal " o". %%o%% goes through as an argument to call of %o%, which then uses the current value of o. – Neil – 2016-06-30T19:36:16.683

Why don't you... just do set o=%o% o? – Erik the Outgolfer – 2016-07-01T07:53:28.180

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ %o% gets expanded before the for loop is parsed, so the loop reads for /l %i in (1,1,8) do call set o= o which is obviously pointless. – Neil – 2016-07-01T08:19:18.327

Why don't you do set o=%%o%% o then (-5)? – Erik the Outgolfer – 2016-07-01T08:25:14.990

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ That would expand to for /l %i in (1,1,8) do set o=%o% o which would set o to %o% o which still isn't right. – Neil – 2016-07-01T08:29:21.007

If o is o o o doesn't set o=%o% o set o to o o o o? – Erik the Outgolfer – 2016-07-01T08:32:38.503

@EʀɪᴋᴛʜᴇGᴏʟғᴇʀ It depends on when the statement is parsed. If the set is its own statement, then it parses as set o= o o o o. If it's inside a for loop, then it's parsed as set o= o as I mentioned above. – Neil – 2016-07-01T08:33:59.500

I think you can do for /l => for/l – Conor O'Brien – 2016-07-01T18:01:58.363

Also, if "%*"=="1 1" => if"%*=="1 1 – Conor O'Brien – 2016-07-01T18:03:26.440

@CᴏɴᴏʀO'Bʀɪᴇɴ Thanks for reminding me that for/l actually has one place where I don't need a space, but I couldn't get your other suggestion to work. – Neil – 2016-07-01T20:20:41.723

Huh. I thought it would work. – Conor O'Brien – 2016-07-01T20:21:51.777

3

C, 202 191 bytes

#define p printf
i,w,h;t(char*c){for(i=0;p(c),++i<w*2+3;);p("\n");}f(){t("_");for(i=0;i<w*h;)i%w<1?p("| o "):p("o "),i++%w>w-2&&p("|\n");t("-");}main(){scanf("%d %d",&w,&h);w*h<2?p("o"):f();}

Thanks to @Lince Assassino for saving 11 bytes!

Ungolfed:

#include <stdio.h>
#define p printf

int i, w, h;

void t(char *c)
{
    for(i=0; p(c), ++i<w*2+3;);
    p("\n");
}

void f()
{
    t("_");
    for(i=0; i<w*h;)
    {
        i%w<1 ? p("| o ") : p("o ");
        i++%w>w-2 && p("|\n");
    }
    t("-");
}

int main()
{
    scanf("%d %d", &w, &h);
    w*h<2 ? p("o") : f();
}

Marco

Posted 2016-06-30T00:27:43.247

Reputation: 581

1You can change your first line for p(char*A){printf(A);} – Lince Assassino – 2016-07-01T17:34:52.640

1Really, thank you! But it's possible to make shorter with #define p printf – Marco – 2016-07-01T18:16:21.577

3

Vim, 56 keystrokes

This seems like a text editing task, so Vim is the obvious choice! I take input as a text file with two space separated integers and output the answer into the same file. Also, I hate you for having the 1x1 special case... Anyway:

"adt l"bDro:if@a*@b==1|wq|en<cr>Di| |<esc>@aio <esc>yy@bPVr_GpVr-ZZ

and if there hadn't been for the special case, 35 keystrokes

"adt x"bDi| |<esc>@aio <esc>yy@bPVr_GpVr-ZZ

A breakdown for sane people:

"adt l"bD

Delete numbers from buffer into @a and @b (space char kept)

         ro:if@a*@b==1|wq|en<cr>

Replace space with "o" and if special case, save and quit

                                Di| |<esc>

Clear the line and write the edges of the lego block

                                          @aio <esc>

Insert @a lots of "o " to get a finished middle part

                                                    yy@bP

Yank line and make @b extra copies (one too many)

                                                         Vr_

We are at top of buffer, replace extra line with underscores

                                                            Gp

Jump to bottom of buffer, pull line that we yanked earlier

                                                              Vr-ZZ

Replace line with dashes, save and quit

algmyr

Posted 2016-06-30T00:27:43.247

Reputation: 858

3

Cinnamon Gum, 32 bytes

0000000: 6c07 d5f5 7a5d 9cdf 5ae6 52ae 4050 0c35  l...z]..Z.R.@P.5
0000010: 18d9 052f 0082 9b42 e7c8 e422 5fe4 7d9f  .../...B..."_.}.

Non-competing. Try it online. Input must be exactly in the form [width,height] with no space in between the comma and the height.

Explanation

The string decompresses to this:

l[1,1]&o;?&`p___~__~
%| ~o ~|
%---~--~

The first l stage maps [1,1] to o (the special case) and everything else to the string

`p___~__~
%| ~o ~|
%---~--~

The backtick then signals the start of a second stage; instead of outputting that string, CG chops off the backtick and executes the string. The p mode then repeats all the characters inside the tildes first parameter (width) times and then afterwards repeats the characters inside the percent signs second parameter (height) times. So for [4,2] it turns into this:

___________
%| o o o o |
%-----------

and then into:

___________
| o o o o |
| o o o o |
-----------

a spaghetto

Posted 2016-06-30T00:27:43.247

Reputation: 10 647

2

Befunge, 114 113 108 101 bytes

I know there are already a number of Befunge solutions, but I was fairly certain they could be improved upon by taking a different approach to the layout of the code. I suspect this answer can be golfed further as well, but it is already a fair bit smaller than either of the previous entries.

&::&+:2`^|,+55_"_",^
-4:,,"| "<\_|#:+1\,,"|"+55$_2#$-#$" o",#!,#:<
  ,"-"_@#:-1<
 :*2+2\-_"o",@v!:-1<

Try it online!

James Holderness

Posted 2016-06-30T00:27:43.247

Reputation: 8 298

Can you explain why the string :<| is needed? – Zacharý – 2018-05-29T18:20:34.697

@Zacharý That vertical branch on the first line never actually branches up. The top of the stack is always zero at that point, so it's a shortcut for dropping the stack top and branching down at the same time - essentially this tip.

– James Holderness – 2018-11-28T18:59:44.117

2

Pyth, 38 bytes

?t*QJEjb+sm\_K+\|+j\o*dhQ\|+mKJsm\-K\o

Test suite.

Leaky Nun

Posted 2016-06-30T00:27:43.247

Reputation: 45 011

2

Haskell, 76 bytes

1#1="o"
w#h|f<-w*2+3=f!"_"++'\n':h!('|':w!" o"++" |\n")++f!"-"
n!s=[1..n]>>s

Usage example: 3 # 2 gives you a multiline string for a 3-by-2 brick.

Ungolfed:

(#) :: Int -> Int -> String
1     #   1    = "o"
width # height = let longWidth = 2 * width + 3 in -- golfed as 'f'
                      (        longWidth `times` "_"  ++   "\n" )
  ++ height   `times` ( "|" ++     width `times` " o" ++ " |\n" )
  ++                  (        longWidth `times` "-"            )

-- | golfed as (!)
times :: Int -> [a] -> [a]
times n s = concat $ replicate n s

MarLinn

Posted 2016-06-30T00:27:43.247

Reputation: 231

At first glance that looked like it should be shorter with unlines, but it's not. – ballesta25 – 2016-07-03T21:40:37.063

2

Groovy, 107, 98, 70, 64

{x,y->t=x*2+3;x<2&&y<2?"o":'_'*t+"\n"+"|${' o'*x} |\n"*y+'-'*t}

Testing:

(2,2)
(1,1)
(8,2)
(1,4)
_______
| o o |
| o o |
-------
o
___________________
| o o o o o o o o |
| o o o o o o o o |
-------------------
_____
| o |
| o |
| o |
| o |
-----

Will Lp

Posted 2016-06-30T00:27:43.247

Reputation: 797

1

Octave, 97 95 86 bytes

@(w,h){[a=~(1:w*2+3)+95;repmat(['| ' repmat('o ',1,w) '|'],h,1);~a+45],'o'}{(w*h<2)+1}

I used the @atlasologist's method in Python to test (1, 1): (...,'o')[x<2>y]

Thanks to @Luis Mendo for saving 7 bytes: a=ones(1,w*2+3)*'_' to a=~(1:w*2+3)+95 and a./a*'-' to ~a+45

Thanks to @pajonk for saving 2 bytes: f=

Marco

Posted 2016-06-30T00:27:43.247

Reputation: 581

1This is not valid Matlab code. You should label as "Octave" only. Also, instead of a./a*'-' can you use ~~a*'-'? Or even ~a+45? – Luis Mendo – 2016-06-30T16:51:38.310

Also, probably you can leave it as an anonymous function (without f=) – pajonk – 2016-06-30T17:30:13.200

1

APL, 46 bytes

{⍵≡1 1:'o'⋄'-'⍪⍨'_'⍪'|',' ','|',⍨'o '⍴⍨1 2×⌽⍵}

The guard: ⍵≡1 1:'o' for the special case. Otherwise 'o '⍴⍨1 2×⌽⍵ builds the content. And the rest is just the boxing.

lstefano

Posted 2016-06-30T00:27:43.247

Reputation: 850

1

Retina, 112 111 bytes

Makes the piece the correct width, then adds rows. Performs a substitution at the end if the result was a 1x1. Takes input like 4,2. Byte count assumes ISO 8859-1 encoding.

\d+
$*
1$
¶___¶| |¶---
+s`1(,.*)(¶.* )(.*)
$1__$2o $3--
+sm`1(.*)(^.*$¶)(.*)
$1$2$2$3
,¶

_____¶\| o \|¶-----
o

Try it online

Fun fact:

You can give multiple inputs on separate lines and it will add them together into a single LEGO!

mbomb007

Posted 2016-06-30T00:27:43.247

Reputation: 21 944

1

C#, 198 bytes

void f(int x,int y){int l=x*2+3;Console.Write(y==x&&x==1?"o":s("_",l)+"\n"+s("|"+s(" o",x)+" |\n",y)+s("-",l));}string s(string m,int u){return string.Join("",new string[u].Select(n=>m).ToArray());}

quick and dirty

I had to write a function that multiplies strings

ungolfed (for suggestions)

public static void f(int x,int y)
{
    int l=x*2+3;
    Console.Write(y == x && x == 1 ? "o" : s("_",l)+"\n"+ s("|" + s(" o", x) + " |\n", y) + s("-",l));

}
public static string s(string m,int u)
{
    return string.Join("", new string[u].Select(n => m).ToArray());
}

downrep_nation

Posted 2016-06-30T00:27:43.247

Reputation: 1 152

I noticed your s function can be optimized to string s(string m,int u){return string.Join("",new int[u].Select(n => m));} - the .ToArray() is redundant and the string[] may as well be an int[]. But instead of string.Join you can use aggregate: string s(string m, int u){return new int[u].Aggregate("",(t,i)=>t+m);} – Oliver Hallam – 2016-06-30T19:17:43.577

1you can shave a few bytes like this...(191) void f(int x,int y){Func<char,int,string>s=(c,i)=>new string(c,i);int l=x*2+3;Console.Write((y&x)==1?"o":s('_',l)+"\n"+s('y',y).Replace("y","| "+s('x', x)+"|\n").Replace("x","o ")+s('-',l));} – Matthew Whited – 2016-06-30T22:01:36.453

1

J, 59 bytes.

' o|_='&({~)@:((4,~3,2(,~"1)2 0,"1(1 0&($~)@,+:))^:(0===]))

Mark Allen

Posted 2016-06-30T00:27:43.247

Reputation: 201

1

Common Lisp, 286 bytes

(let((columns(read))(rows(read)))(when(and(= columns 1)(= rows 1))(progn(write-char #\o)(exit)))(dotimes(i(+(* columns 2)3))(write-char #\_))(terpri)(dotimes(i rows)(format t "| ")(dotimes(i columns)(format t "o "))(format t "| ")(terpri))(dotimes(i(+(* columns 2)3))(write-char #\-)))

Byeonggon Lee

Posted 2016-06-30T00:27:43.247

Reputation: 419

this shows you have 285, not 286 bytes. (let((c(read))(r(read)))(when(and(= c 1)(= r 1))(progn(#1=write-char #\o)(exit)))(#2=dotimes #4=(i(+(* c 2)3))(#1# #\_))(terpri)(#2#(i r)#3=(format t "| ")(#2#(i c)(format t "o "))#3#(terpri))(#2# #4# (#1# #\-))) saves 73 bytes. I used c instead of columns, r instead of rows, also #n# and #n= reader macro where you reuse things. – None – 2017-02-25T10:04:35.537

This saves 103 bytes: (lambda(c r)(if(=(* c r)1)(format t"o"(exit)))(#1=dotimes #2=(i(+(* c 2)3))(format t"_"))(terpri)(#1#(i r)#3=(format t"| ")(#1#(i c)(format t"o "))#3#(terpri))(#1# #2#(format t"-"))), I think anonymous function is allowed. What I don't like is fact that for (f 1 1) output is not shown (or it's shown but program exits too quickly to see it) but it was in your solution from the beggining. – None – 2017-02-25T19:04:12.097

1

PHP 4.1, 103 bytes

This is based on my answer on https://codegolf.stackexchange.com/a/57883/14732, which was where all the heavy lifting was done.

<?$R=str_repeat;printf($W+$H<3?o:"_%'_".($Z=1+$W*2)."s_
%s-%1$'-{$Z}s-",'',$R('|'.$R(' o',$W).' |
',$H));

This answer requires that short_open_tags and register_globals are enabled (which it is enabled by default).

You can pass the values using the keys W and H, over POST, GET, SESSION and COOKIE.

This outputs warnings to STDERR, which aren't errors. According to https://codegolf.meta.stackexchange.com/a/1655/14732, this is acceptable as long as it isn't disallowed in the question.
Removing the warnings costs me 2 bytes.

Ismael Miguel

Posted 2016-06-30T00:27:43.247

Reputation: 6 797

1

PHP 7, 98 bytes

<?=($P=str_pad)("",$w=3+2*$argv[1],_).$P("",$argv[2]*++$w,$P("
| ",$w-1,"o ")."|").$P("
",$w,"-");

str_pad saves a little from str_repeat.
Juggling with preincrement on $w saves two bytes.
But version 7 is needed for assigning $r while using it at the same time.

Titus

Posted 2016-06-30T00:27:43.247

Reputation: 13 814

1

Racket 174 bytes

(λ(w h)(let((dl(λ(s)(displayln s)))(d(λ(s)(display s))))(for((x(+ 3(* w 2))))(d"_"))
(dl"")(for((x h))(d"| ")(for((y w))(d"o "))(d"|")(dl""))(for((x(+ 3(* w 2))))(d"-"))))

Ungolfed:

(define (f1 w h)
    (let ((dl (λ (s) (displayln s)))
          (d (λ (s) (display s))))
      (for ((x (+ 3 (* w 2))))
        (d "_"))
      (dl "")
      (for ((x h))
        (d "| ")
        (for ((y w))
          (d "o "))
        (d "|")
        (dl ""))
      (for ((x (+ 3 (* w 2))))
        (d "-"))))

Testing:

(f 5 2)

Output:

_____________
| o o o o o |
| o o o o o |
-------------

rnso

Posted 2016-06-30T00:27:43.247

Reputation: 1 635

0

Swift, 182 Bytes

func p(w:Int,h:Int){let i=w*2+3;var s="";if w+h<3{s="o"}else{for _ in 0..<i{s+="_"};for _ in 0..<h{s+="\n|";for _ in 0..<w{s+=" o"};s+=" |"};s+="\n";for _ in 0..<i{s+="-"}};print(s)}

If this was a closure implementation the func p(w:Int,h:Int) could be deleted and w replaced with $0 and h replaced with $1 but then it would be only the implementation not the interface so I felt that was cheating...

2nd attempt 180 Bytes

func q(w:Int,h:Int){let l=w*2+3;var s="",a="";if w+h<3{s="o"}else{for _ in 0..<l{s+="_";a+="-"};for x in 0..<h*w{s+=x%h==0 ? "\n| o":x%h==w-1 ? " o |":" o"};s+="\n";s+=a};print(s)}

Improved using ternaries. Only 2 bytes though...

3rd attempt 263 bytes

func r(w:Int,h:Int){let l=w*2+3;var s="";if w+h<3{s="o"}else{for a in 0..<(h+2)*l{if a<l{s+="_";continue};if a>=(h+2)*l-l{s+=a%l==0 ? "\n-":"-";continue};let v=a%l>0&&a%l<l-2;s+=a%l==0 ? "\n|":v&&(a%l)%2==0 ? "o":v&&(a%l)%2==1 ? " ":a%l==l-1 ? "|":" "}};print(s)}

The reason I include this is because my original thought was to decrease the amount of for loops. Whilst this is more verbose it only has the one iteration (also it took me quite a long time so here it is again in a more friendly format):

func r(w: Int, h: Int) {
    let l = w * 2 + 3
    var str = ""
    if w + h < 3 {
        str = "o"
    } else {
        for a in 0..<(h + 2) * l {
            if a < l {
                str += "_"
                continue
            }
            if a >= (h + 2) * l - l {
                str += a % l == 0 ? "\n-" : "-"
                continue
            }
            let v = a % l > 0 && a % l < l - 2
            str += a % l == 0 ? "\n|" : v && (a % l) % 2==0 ? "0" : v && (a % l) % 2 == 1 ? " " : a % l == l - 1 ? "|" : " "
        }
    }
    print(str)
}

There are a lot of ternary conditions here so I'm sure it can be optimised further.

Rob Sanders

Posted 2016-06-30T00:27:43.247

Reputation: 101

Welcome to the site! Nice first post. – programmer5000 – 2017-05-02T21:04:56.317

0

JavaScript ES6, 134 124 Bytes

function p(e,n){r='repeat';e*n-1?x="_"[r](2*e+3)+"\n"+("|"+" o"[r](e)+" |\n")[r](n)+"-"[r](2*e+3)+"\n":x="o",console.log(x)}

Lexx

Posted 2016-06-30T00:27:43.247

Reputation: 1

I can shave off 4 bytes by using [r] instead of .repeat: function p(e,n){r='repeat';1==e&&1==n?x="0":x="_"[r](2*e+3)+"\n"+("|"+" o"[r](e)+" |\n")[r](n)+"-"[r](2*e+3)+"\n",console.log(x)} – IvanSanchez – 2016-06-30T09:20:34.227

The output for 1,1 should be o, not 0. Also, I don't see any use of ES6 here, so you can change the version. – Neil – 2016-06-30T12:22:03.517

@Neil AFAIK .repeat was introduced with ES6, fixed the output – Lexx – 2016-06-30T12:39:51.053

Ah, well Firefox had it over a year before ES6. – Neil – 2016-06-30T13:37:40.693

1You can replace function p(e,n) with p=(e,n)=> for 6 bytes. – cyberbit – 2016-06-30T14:14:21.063

1You're assigning x= twice. The second assignation can be removed for an extra 2 bytes, and x can be replaced altogether with a console.log( ? : ), bringing it down to 113 bytes: p=(e,n)=>{r='repeat';console.log(e*n-1?"_"[r](2*e+3)+"\n"+("|"+" o"[r](e)+" |\n")[r](n)+"-"[r](2*e+3)+"\n":"o")} ` – IvanSanchez – 2016-06-30T15:10:30.663

1Guess what, welcome to PPCG! – Erik the Outgolfer – 2016-07-01T08:04:03.617

"JavaScript" it is.

– Peter Mortensen – 2016-07-02T09:11:39.873

Thanks a lot, learned a bit and will adjust my solution :) – Lexx – 2016-07-04T13:28:39.120

0

SpecBAS - 87 bytes

1 INPUT w,h: x=w*2+3: ?IIF$(w=1 AND h=1,"o","_"*x+#13+(("| "+("o "*w)+"|"#13)*h)+"-"*x)

Uses an inline-IF to either print single "o" when width and height are 1, otherwise builds up a string. #13 is the line feed character.

Brian

Posted 2016-06-30T00:27:43.247

Reputation: 1 209

0

Scala, 90 bytes

(w:Int,h:Int)=>{val A=2*w+3;if(w*h==1)"o"else s"${"_"*A}\n${s"| ${"o "*w}|\n"*h}${"-"*A}"}

paste the above function into your Scala REPL, and then invoke the function with the width and height arguments.

Peter Perháč

Posted 2016-06-30T00:27:43.247

Reputation: 101

Hello, and welcome to PPCG! This is a great first post! There is only one small issue: we commonly use Scala, 170 bytes not Scala - 170 bytes. They both work, but out byte snippet counters look for the first, according to my research. – NoOneIsHere – 2016-07-01T23:21:22.003

@NoOneIsHere thanks. fixed. and also shortened the submission from 170 to 94 bytes. Enjoying this... :-) – Peter Perháč – 2016-07-02T09:50:14.713

@PeterPerháč You need to use Level 1 headers, so not fixed. Instead of **Scala, 94 bytes**, use #Scala, 94 bytes. – Erik the Outgolfer – 2016-07-02T13:18:07.053

You can use anonymous functions, i.e. {val A=2*w+3;if(w*h==1)"o"else s"${"_"*A}\n${s"| ${"o "*w}|\n"*h}${"-"*A}"} (not snippets, though). – Erik the Outgolfer – 2016-07-02T17:30:16.030

0

JavaScript ECMAScript6 (92 94 91 87 Bytes)

92 Characters

a=(w,h,r='repeat')=>w-h?"__"[r](w)+"__\n"+("|"+" o"[r](w)+" |\n")[r](h)+"--"[r](w)+"---":"o"

Everything below here is Correct

94 Characters

a=(w,h,r='repeat')=>w*h-1?"__"[r](w)+"__\n"+("|"+" o"[r](w)+" |\n")[r](h)+"--"[r](w)+"---":"o"

91 Characters

a=(w,h)=>w*h-1?`${"__"[r='repeat'](w)}__\n`+('|'+" o"[r](w)+' |\n')[r](h)+"-"[r](w*2+3):"o"

87 Characters (Smallest so Far)

a=(w,h)=>w*h-1?'__'[r='repeat'](w)+`__
`+('|'+' o'[r](w)+` |
`)[r](h)+'-'[r](w*2+3):'o'

I took what I learned from the other JavaScript Submissions and made it a lot smaller by writing mine from scratch and then working my way down, turns out you don't need any console.log or alert as by default it will push it to the console when there is an output

I am still working on it to see if I can make it smaller yet

Shubshub

Posted 2016-06-30T00:27:43.247

Reputation: 31

I think your first line of output is one character short. – James Holderness – 2018-01-11T23:32:48.420