17
\$\begingroup\$

You host the Olympic Games, and need to build a fantastic pool for the sake of the occasion, but the supervisors often change their mind regarding the dimensions, and need a quick way to rebuild it with the requested size!


Given two integers, L and x, your task is to build a swimming pool of length L and x lanes.

How is a pool built?

  • It contains an inner square, whose horizontal walls are made of L consecutive dashes(-) , and whose vertical walls are made out of 3x - 1 bars (|). Additionally, 4 + signs lie in each corner. Let's have an example (L = 10, x = 2):

    +----------+
    |          |
    |          |
    |          |
    |          |
    |          |
    +----------+
    
  • Each lane has a width of 2 vertical units. The inner square is filled with x-1 lane separators, consisting of L horizontally consecutive : symbols. After putting the lane separators, our pool should look like this:

    +----------+
    |          |
    |          |
    |::::::::::|
    |          |
    |          |
    +----------+
    
  • A pool also contains a padding (an outer square), whose horizontal walls are (L+4) -s and whose vertical walls are (3x + 1) |s, that surrounds the inner square:

    +--------------+
    | +----------+ |
    | |          | |
    | |          | |
    | |::::::::::| |
    | |          | |
    | |          | |
    | +----------+ |
    +--------------+
    

And that's our olympic-sized** swimming pool!


Specs:

  • For design and functionality purposes, you are guaranteed that 100 ≥ L ≥ 10 and 15 ≥ x ≥ 2.

  • The output must be exactly as shown. Outputting a "vertically-built"* pool is disallowed.

  • Trailing and leading spaces are allowed.

  • You may take input and provide output through any standard method.

  • Default Loopholes apply.


Examples / Test cases:

L = 20, x = 3

+------------------------+
| +--------------------+ |
| |                    | |
| |                    | |
| |::::::::::::::::::::| |
| |                    | |
| |                    | |
| |::::::::::::::::::::| |
| |                    | |
| |                    | |
| +--------------------+ |
+------------------------+



L = 50, x = 5:

+------------------------------------------------------+
| +--------------------------------------------------+ |
| |                                                  | |
| |                                                  | |
| |::::::::::::::::::::::::::::::::::::::::::::::::::| |
| |                                                  | |
| |                                                  | |
| |::::::::::::::::::::::::::::::::::::::::::::::::::| |
| |                                                  | |
| |                                                  | |
| |::::::::::::::::::::::::::::::::::::::::::::::::::| |
| |                                                  | |
| |                                                  | |
| |::::::::::::::::::::::::::::::::::::::::::::::::::| |
| |                                                  | |
| |                                                  | |
| +--------------------------------------------------+ |
+------------------------------------------------------+


L = 10, x =15

+--------------+
| +----------+ |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| |::::::::::| |
| |          | |
| |          | |
| +----------+ |
+--------------+

This is , so the shortest code in bytes wins!

*The water could flow out if it is built vertically :P

**Yes, I am aware that the more the lanes are and the shorter the pool is, the less the drawing looks like a pool!

\$\endgroup\$
  • \$\begingroup\$ Sandbox, for those who can view deleted posts. \$\endgroup\$ – Mr. Xcoder Jun 20 '17 at 14:30
  • \$\begingroup\$ What should happen if x>=L?? \$\endgroup\$ – CraigR8806 Jun 20 '17 at 17:42
  • \$\begingroup\$ @CraigR8806 The same algorithm. Just with more lanes than the length of the pool. I have a feeling that you have mistread the question... \$\endgroup\$ – Mr. Xcoder Jun 20 '17 at 17:45
  • \$\begingroup\$ What about the case where L=10 and x=15? Would there not be more lanes than could be placed in the pool? I could be misunderstanding the intent \$\endgroup\$ – CraigR8806 Jun 20 '17 at 17:57
  • \$\begingroup\$ @CraigR8806 The length has nothing to do with the lanes! You can test it yourself with one of the existing answers \$\endgroup\$ – Mr. Xcoder Jun 20 '17 at 17:59

14 Answers 14

13
\$\begingroup\$

Charcoal, 32 bytes

NθNη↓Eθ×η:  B⁺θ²⁺׳η¹↖←B⁺θ⁶⁺׳η³

Try it online!

-4 thanks to Neil.

AST:

Program
├N: Input Number
│└θ: Identifier θ
├N: Input Number
│└η: Identifier η
├Print
│├↓: Down
│└E: Map
│ ├θ: Identifier θ
│ └×: Product
│  ├η: Identifier η
│  └':  ': String ':  '
├B: Box
│├⁺: Sum
││├θ: Identifier θ
││└2: Number 2
│└⁺: Sum
│ ├×: Product
│ │├3: Number 3
│ │└η: Identifier η
│ └1: Number 1
├Move
│└↖: Up Left
├Move
│└←: Left
└B: Box
 ├⁺: Sum
 │├θ: Identifier θ
 │└6: Number 6
 └⁺: Sum
  ├×: Product
  │├3: Number 3
  │└η: Identifier η
  └3: Number 3
\$\endgroup\$
  • \$\begingroup\$ wow Charcoal has got this challenge down :) \$\endgroup\$ – Downgoat Jun 20 '17 at 16:51
  • \$\begingroup\$ You stayed true to your name - you have me outgolfed. \$\endgroup\$ – notjagan Jun 20 '17 at 18:24
  • 2
    \$\begingroup\$ As usual, explanation request. \$\endgroup\$ – CalculatorFeline Jun 20 '17 at 22:04
  • \$\begingroup\$ @CalculatorFeline Yeah, I don't have time to do all those explanations while I'm seeking something to outgolf...also you can check the AST using the -a command-line argument. \$\endgroup\$ – Erik the Outgolfer Jun 21 '17 at 8:58
  • \$\begingroup\$ why does -a raise error >_< \$\endgroup\$ – Erik the Outgolfer Jun 21 '17 at 9:03
12
\$\begingroup\$

Charcoal, 40 39 37 bytes

NθNηB⁺θ⁶⁺׳η³↘→B⁺θ²⁺׳η¹→F⁻η¹«M³↓P×:θ

Try it online!

I know Neil already has a Charcoal answer of around the same length, but I took a bit of a different approach so I figured I may as well also post mine.

Explanation

NθNηB⁺θ⁶⁺׳η³↘→B⁺θ²⁺׳η¹→F⁻η¹«M³↓P×:θ
NθNη                                         take the two inputs as numbers in θ and η
      B⁺θ⁶⁺×³η³                               draw a rectangle θ + 6 by 3η + 3
                                               (characters default to |, - and +)
                ↘→                             move the cursor down one and right two
                   B⁺θ²⁺×³η¹                  draw a rectangle θ + 2 by 3η + 1
                             F⁻η¹«            for ι (unused) from 0 up until η - 1:
                                   M³↓            move the cursor down by 3
                                       P×:θ       print θ :s without moving the cursor
                                               [implicit end of for]
\$\endgroup\$
  • 1
    \$\begingroup\$ Explanation please. \$\endgroup\$ – CalculatorFeline Jun 20 '17 at 22:04
  • \$\begingroup\$ @CalculatorFeline Added. \$\endgroup\$ – notjagan Jun 20 '17 at 22:16
  • 1
    \$\begingroup\$ Huh, so the third parameter to is optional? Nice, that saves me two bytes! \$\endgroup\$ – Neil Jun 21 '17 at 10:08
11
\$\begingroup\$

Charcoal, 40 38 36 31 bytes

A⁺²NθA⁺¹×³NηUOθη:¶¶Bθη↖←B⁺⁴θ⁺²η

Try it online! Link is to verbose version of code. Explanation:

A⁺²Nθ       Assign(Plus(2, InputNumber()), q);

Charcoal's drawing primitives use the overall character count including +s, however the input is just the number of -s, so we need to add 2 to obtain the width of the inner wall.

A⁺¹×³Nη     Assign(Plus(1, Times(3, InputNumber())), h);

Calculate the height of the inner wall, again, inclusive of the bottom row, so three per lane plus one.

UOθη:¶¶     Oblong(q, h, ":\n\n");

Draw the lanes. This is simply a rectangle filled with :s vertically separated by two blank lines (the pilcrows represent newline characters).

Bθη         Box(q, h);

The Rectangle command is exactly what we need to draw the inner wall. Edit: Box allows you to omit its third parameter, saving me 2 bytes.

↖           Move(:UpLeft);
←           Move(:Left);
B⁺⁴θ⁺²η     Box(Plus(4, q), Plus(2, h));

And again to draw the outer wall, except slightly wider and taller, and centred on the inner wall.

\$\endgroup\$
  • 1
    \$\begingroup\$ Why are the links to Charcoal code at TIO never to the actual code? \$\endgroup\$ – Jonathan Allan Jun 20 '17 at 14:57
  • 1
    \$\begingroup\$ @JonathanAllan I link to the verbose code because it's more readable, plus I add the argument to generate the succinct code automatically for verification purposes. \$\endgroup\$ – Neil Jun 20 '17 at 14:59
  • \$\begingroup\$ ...but how is 219 characters more readable than 40? :p \$\endgroup\$ – Jonathan Allan Jun 20 '17 at 15:30
  • \$\begingroup\$ I could have just looked harder and noticed this tip :) \$\endgroup\$ – Jonathan Allan Jun 20 '17 at 17:04
  • 1
    \$\begingroup\$ As usual, please explain. \$\endgroup\$ – CalculatorFeline Jun 20 '17 at 17:35
8
\$\begingroup\$

T-SQL, 284 281 bytes

DECLARE @ INT,@x INT,@S VARCHAR(MAX)='+--d--+b| +d+ |b'SELECT @=L,@x=x FROM t
P:SET @S+='| |s| |b| |s| |b| |c| |b'SET @x-=1IF @x>0GOTO P
PRINT REPLACE(REPLACE(REPLACE(REPLACE(LEFT(@S,LEN(@S)-6)+'+d+ |b+--d--+','d',REPLICATE('-',@)),'b',CHAR(13)),'s',SPACE(@)),'c',REPLICATE(':',@))

Input is taken from INT columns L and x in preexisting table t, per our allowed input methods.

Basically I'm creating a long string with letters representing the repeated characters (d=dashes, s=spaces, c=colons, b=line break), then REPLACE them all at the end with the appropriate fillers.

Formatted:

DECLARE @ INT,@x INT,@S VARCHAR(MAX)='+--d--+b| +d+ |b'
SELECT @=L,@x=x FROM t
P:
    SET @S+='| |s| |b| |s| |b| |c| |b'
    SET @x-=1
IF @x>0 GOTO P
PRINT REPLACE(REPLACE(REPLACE(REPLACE( LEFT(@S,LEN(@S)-6)+'+d+ |b+--d--+'
     ,'d',REPLICATE('-',@))
     ,'b',CHAR(13))
     ,'s',SPACE(@))
     ,'c',REPLICATE(':',@))

Inside the loop I append 2 rows of blanks and 1 row of colons, then at the end I chop off that divider row and append the pool border before performing the replaces.

EDIT: Saved 3 bytes by switching @ to the most-often used variable, and swapping initialization order.

\$\endgroup\$
8
\$\begingroup\$

JavaScript (ES6), 137 124 bytes

Golfed it down a bit on my phone, more to follow.

x=>y=>`+--0--+
| +0+ |
${((a=`| |1| |
`)+a+`| |2| |
`).repeat(y-1)+a+a}| +0+ |
+--0--+`.replace(/\d/g,n=>"- :"[n].repeat(x))

Try it

f=
x=>y=>`+--0--+
| +0+ |
${((a=`| |1| |
`)+a+`| |2| |
`).repeat(y-1)+a+a}| +0+ |
+--0--+`.replace(/\d/g,n=>"- :"[n].repeat(x))
oninput=_=>o.innerText=f(+i.value)(+j.value);o.innerText=f(i.value=50)(j.value=5)
input{font-family:sans-serif;margin:0 5px 0 0;width:50px;}
<label for=i>L: </label><input id=i type=number><label for=j>x: </label><input id=j type=number><pre id=o>

\$\endgroup\$
  • \$\begingroup\$ I like this one because it's interactive :D \$\endgroup\$ – xDaizu Jun 21 '17 at 16:26
  • \$\begingroup\$ @xDaizu, check out some of my other JS solutions, so ;) \$\endgroup\$ – Shaggy Jun 21 '17 at 17:33
6
\$\begingroup\$

Python 2, 124 120 117 bytes

-2 bytes thanks to Hyper Neutrino

l,x=input()
o='+--%s--+\n| +%s+ |\n'%(('-'*l,)*2)
print o+'| |%s| |\n'*(x*3-1)%((' '*l,' '*l,':'*l)*x)[:-1]+o[-2::-1]

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 122 bytes (your variable t is unnecessary) \$\endgroup\$ – HyperNeutrino Jun 20 '17 at 15:59
4
\$\begingroup\$

SOGL V0.12, 52 51 bytes

:┌* +1Ο;@*┐1ΟG∙⁴++⁰
b3*Ie4+⁰b3*He⁰32žbH∫3*2+4;e :*ž

Try it Here!
Not bad considering that 20 bytes of this is a rectangle function, which charcoal has a built-in for.

Explanation:

Rectangle function: (example: on stack 4, 2)
:                   duplicate the top of stack (X pos)      [4, 2, 2]
 ┌*                 get that many "-"es                     [4, 2, "--"]
    +               push "+"                                [4, 2, "--", "+"]
     1Ο             wrap the dashes in pluses               [4, 2, "+--+"]
       ;            get the duplicated X position           [4, "+--+", 2]
        @*          get that many spaces                    [4, "+--+", "  "]
          ┐         push "|"                                [4, "+--+", "  ", "|"]
           1Ο       wrap the spaces with "|"                [4, "+--+", "|  |"]
             G      get the Y value ontop                   ["+--+", "|  |", 4]
              ∙     get an array with that many strings     ["+--+", ["|  |", "|  |", "|  |", "|  |"]]
               ⁴    duplicate the dashes wrapped in pluses  ["+--+", ["|  |", "|  |", "|  |", "|  |"], "+--+"]
                ++  add everything to one array             [["+--+", "|  |", "|  |", "|  |", "|  |", "+--+"]]
Main function: (example input: 2, 5)
b3*                              push variable B (input 1, Y length) multiplied by 3                     [6]
   I                             increase it                                                             [7]
    e4+                          push variable E (input 2, X length) + 4                                 [7, 9]
       ⁰                         execute the rectangle function [in X: E+4, Y: b*3+1]                    [["+---------+","|         |","|         |","|         |","|         |","|         |","|         |","|         |","+---------+"]]
        b3*                      push variable B * 3                                                     [["+---------+",..,"+---------+"], 6]
           H                     decrease it                                                             [["+---------+",..,"+---------+"], 5]
            e                    push variable E                                                         [["+---------+",..,"+---------+"], 5, 5]
             ⁰                   execute the rectangle function [on X: E, Y: B*3-1]                      [["+---------+",..,"+---------+"], ["+-----+","|     |","|     |","|     |","|     |","|     |","+-----+"]]
              32ž                at coordinates [3;2] (1-indexed) in the first rectangle put in the 2nd  [["+---------+",
                                                                                                           "| +-----+ |",
                                                                                                           "| |     | |",
                                                                                                           "| |     | |",
                                                                                                           "| |     | |",
                                                                                                           "| |     | |",
                                                                                                           "| |     | |",
                                                                                                           "| +-----+ |",
                                                                                                           "+---------+"]
                 bH∫             iterate over the numbers from 1 to B-1:                                 [[...], 1]
                    3*2+           push pop()*3+2                                                        [[...], 5]
                        4;         push 4 one below the stack                                            [[...], 4, 5]
                          e        push the variable E (X length)                                        [[...], 4, 5, 5]
                            :*     get that many colons                                                  [[...], 4, 5, ":::::"]
                              ž    insert [at coordinates [4; cIter*3+2] the colons]                     
\$\endgroup\$
4
\$\begingroup\$

C# (.NET Core), 202 bytes

(L,x)=>{char p='+',n='\n',e=' ';string v="|",r="",s=p+new string('-',L+4)+p+n,q=v+e+p+new string('-',L)+p+e+v+n;r+=s+q;for(int i=0;i<3*x-1;)r+=v+e+v+new string(i++%3<2?e:':',L)+v+e+v+n;r+=q+s;return r;}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Python 2, 128 126 bytes

L,x=input()
k='+'+'-'*(L+4)+'+\n| +'+'-'*L+'+ |\n'
f=lambda k:'| |'+k*L+'| |\n'
print k+f(':').join([f(' ')*2]*x)[:-1]+k[::-1]

Try it online!

-2 bytes thanks to @Mr.Xcoder

\$\endgroup\$
  • \$\begingroup\$ 126 bytes \$\endgroup\$ – Mr. Xcoder Jun 29 '17 at 9:52
  • \$\begingroup\$ @Mr.Xcoder Right, thanks, I don't know what I was thinking with those extra brackets :P \$\endgroup\$ – HyperNeutrino Jun 29 '17 at 13:43
3
\$\begingroup\$

PHP, 153 bytes

for(;$i-2<$z=3*$argv[2]+1;$i++)echo str_pad(strrev($r=["--+","+ |","| |"][!($b=$i>1&$i<$z)?$i&&$i<$z+1?1:0:2]),$argv[1]+3,"- :"[$b?$i%3!=1?1:2:0])."$r
";

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Python 2, 97 bytes

L,x=input()
for i in'01'+'223'*~-x+'2210':r=('+|||-   -+||'+L*'-- :')[int(i)::4];print r+r[2::-1]

Try it online!


Python 2, 98 bytes

L,x=input()
s='+---| +-%s+-+---'%'|:'.join(['| '*5]*x)
while s:print s[:3]+s[3]*L+s[2::-1];s=s[4:]

Try it online!

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

Charcoal, 36 bytes

NθNηFη«M³↑P×θ:»←B⁺θ²⁺׳η¹↖←B⁺θ⁶⁺׳η³

Try it online!

This is a more Charcoal-y algorithm than my other answer.

\$\endgroup\$
  • 1
    \$\begingroup\$ Charcoal has officially taken over the whole challenge. 4/10 answers are in Charcoal... \$\endgroup\$ – Mr. Xcoder Jun 20 '17 at 18:41
  • \$\begingroup\$ @Mr.Xcoder Well, it's a language specifically designed to golf ascii-art, so that's what you get for it. ;) \$\endgroup\$ – Erik the Outgolfer Jun 20 '17 at 18:42
  • 1
    \$\begingroup\$ Explanation request. \$\endgroup\$ – CalculatorFeline Jun 20 '17 at 22:04
  • \$\begingroup\$ @Mr.Xcoder And three of them are the same length too! \$\endgroup\$ – Neil Jun 21 '17 at 10:09
2
\$\begingroup\$

C (gcc), 195 bytes

#define P printf
y;f(L,l){char s[L+1],t[L+1];memset(s,45,L);memset(t,58,L);t[L]=s[L]=0;P("+-%s-+\n|+%s+|\n",s,s);for(y=3*l;y-->1;y%3?P("||%*c||\n",L,32):P("||%s||\n",t));P("|+%s+|\n+-%s-+",s,s);}

Try it online!

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

Perl 5, 124 + 1 (-a) = 125 bytes

say$o='+'.'-'x($l=pop@F),$t="----+
",$i="| $o+ |
",$e=($d="| |").$"x$l.$d,$/,$e;say$d.':'x$l."$d
$e
$e"for 2..$_;say$i,$o,$t

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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