25
\$\begingroup\$

Given a positive integer < 100 (from 1 to 99, including 1 and 99), output that many lockers.

A locker is defined as the following:

+----+
|    |
|    |
|    |
| nn |
+----+

where nn is the locker number, in base 10. If there is 1-digit number, it is expressed with a 0 in front of it. For example, locker number 2 displays the number 02.

Lockers can be stacked, but only up to 2 high:

+----+
|    |
|    |
|    |
| on |
+----+
|    |
|    |
|    |
| en |
+----+

on denotes an odd number, en an even number. Lockers can also be put next to each other.

+----+----+
|    |    |
|    |    |
|    |    |
| 01 | 03 |
+----+----+----+
|    |    |    |
|    |    |    |
|    |    |    |
| 02 | 04 | 05 |
+----+----+----+

Notice that locker number 5 is an odd-numbered locker that is on the bottom. This is because when you have odd-numbered input, the last locker should be placed on the floor (because a hovering locker costs too much). The above example therefore is the expected output for n=5. n=0 should return an nothing.

Rules: Standard methods of input/output. Input in any convenient format, output as a string. Standard loopholes apply.

Test cases:

Input
Output
---------------------
1






+----+
|    |
|    |
|    |
| 01 |
+----+
--------------------- (newlines optional in case 1)
4
+----+----+
|    |    |
|    |    |
|    |    |
| 01 | 03 |
+----+----+
|    |    |
|    |    |
|    |    |
| 02 | 04 |
+----+----+
---------------------
5
+----+----+
|    |    |
|    |    |
|    |    |
| 01 | 03 |
+----+----+----+
|    |    |    |
|    |    |    |
|    |    |    |
| 02 | 04 | 05 |
+----+----+----+
---------------------
16
+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |
| 01 | 03 | 05 | 07 | 09 | 11 | 13 | 15 |
+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |
| 02 | 04 | 06 | 08 | 10 | 12 | 14 | 16 |
+----+----+----+----+----+----+----+----+

This is , so shortest code wins!

\$\endgroup\$
  • \$\begingroup\$ Related \$\endgroup\$ – Shaggy Apr 27 '17 at 14:10
  • 15
    \$\begingroup\$ It's time to tell the builders to put the odd lockers on the bottom. \$\endgroup\$ – mbomb007 Apr 27 '17 at 14:11
  • 3
    \$\begingroup\$ Do the prepending newlines of the case 1 have to be outputted? \$\endgroup\$ – dzaima Apr 27 '17 at 14:30
  • \$\begingroup\$ @ComradeSparklePony didn't impact me at all :). Sorry about the harsh tone used, just trying to help. \$\endgroup\$ – Magic Octopus Urn Apr 27 '17 at 18:48
7
\$\begingroup\$

Pyth, 66 bytes

A.[Y2m+.rKXJr6j" | "++km.[\02`kdkjkUT;" -|+"+*3]KJ_.T_McSQ2js[GHhH

Test suite.

\$\endgroup\$
6
\$\begingroup\$

Python 2, 201 191 185 175 171 166 164 163 bytes

n=input()
for j in 0,1:c=n/2+n%2*j;m='+----'*c+'+\n';print['\n',m+('|    '*c+'|\n')*3+''.join('| %02d '%-~i for i in range(j,n-n%2,2)+n%2*j*[~-n])+'|\n'+m*j][c>0],

Try it Online!

\$\endgroup\$
  • \$\begingroup\$ This currently fails on the first test case. \$\endgroup\$ – Comrade SparklePony Apr 27 '17 at 15:01
  • \$\begingroup\$ @ComradeSparklePony Fixed \$\endgroup\$ – math junkie Apr 27 '17 at 15:21
4
\$\begingroup\$

PHP, 191 Bytes

for(;a&$k="01112344453"[$i++];print"$l\n")for($l="",$b="+||"[$k%3],$n=0;$n++<$a=$argn;)$l.=$i<6&$n%2&$n!=$a|$i>5&($n%2<1|$n==$a)?($l?"":"$b").["----+","    |",sprintf(" %02d |",$n)][$k%3]:"";

Try it online!

PHP, 235 Bytes

for(;$i++<$a=$argn;)$r[$i==$a|1-$i&1][]=($p=str_pad)($i,2,0,0);for(;$j<6;)$l[]=($a<2&$j<3?"":[$p("+",$c=($j<3?floor:ceil)($a/2)*5+1,"----+"),$p("|",$c,"    |"),"| ".join(" | ",$r[$j/3])." |"])[$j++%3]."\n";echo strtr("01112344453",$l);

Case 1 with optional newlines

Try it online!

Expanded

for(;$i++<$a=$argn;)
  $r[$i==$a|1-$i&1][]=($p=str_pad)($i,2,0,0); # make an 2D array 0:odd values 1:even values and last value  
for(;$j<6;) # create 6 strings for each different line
  $l[]=($a<2&$j<3 # if last value =1 and line number under 3 
    ?"" # make an empty string empty [] as alternative
    :[$p("+",$c=($j<3 # else make the 0 or 3 line and store the count for next line
      ?floor # if line number =0 count=floor ($a/2)  multiply 5 and add 1
      :ceil)($a/2)*5+1,"----+") # else count= ceil($a/2) multiply 5 and add 1
    ,$p("|",$c,"    |") # make lines 1 and 4
    ,"| ".join(" | ",$r[$j/3])." |"])[$j++%3]."\n"; #make lines 2 odd values and 5 even values and last value
echo strtr("01112344453",$l); # Output after replace the digits with the 6 strings

PHP, 300 Bytes

for(;$i++<$a=$argn;)$r[$i==$a||!($i%2)][]=($p=str_pad)($i,2,0,0);echo strtr("01112344453",($a>1?[$p("+",$c=($a/2^0)*5+1,"----+")."\n",$p("|",$c,"    |")."\n","| ".join(" | ",$r[0])." |\n"]:["","",""])+[3=>$p("+",$c=ceil($a/2)*5+1,"----+")."\n",$p("|",$c,"    |")."\n","| ".join(" | ",$r[1])." |\n"]);

replace ["","",""] with ["\n","\n","\n"] if you want newlines for case 1

Try it online!

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

Ruby, 256 239 201 191 183 bytes

n=gets.to_i;a=n/2;z=a+n%2;t=a*2;q="+----+";r=->x{q*x+?\n+("|    |"*x+?\n)*3+"| n |"*x+?\n};u=r[a]+r[z]+q*z;n.times{|i|j=2*i+1;u[?n]="%02d"%(i<a ?j:i>=t ?j-t:j-t+1)};puts u.squeeze'+|'

This is awfully long. I'll work on golfing it more.

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

C (gcc), 426 335 300 294 282 252 249 246 244 237 bytes

This really needs to be golfed down

#define L puts("")
#define P(s)printf(&s[i>1]
#define F(x)for(i=0;i++<x;)P(
#define O(x,y)F(x)"+----+"));for(j=0;L,j++<3;)F(x)"|    |"));j=y;F(x)"| %02d |")
e,i,j;f(n){e=n/2+n%2;O(n/2,-1),j+=2);L;O(e,0),j+=i^e?2:2-n%2);L;F(e)"+----+"));}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Suggest -~n/2 instead of n/2+n%2 \$\endgroup\$ – ceilingcat Nov 29 '18 at 2:41
1
\$\begingroup\$

Batch, 305 bytes

@echo off
set/a"n=%1&-2
if %1 gtr 1 call:l %n% 1
call:l %1 2
echo %s: =-%
exit/b
:l
set s=+
set "t=|
for /l %%i in (%2,2,%n%)do call:c %%i
if %1 gtr %n% call:c %1
for %%s in ("%s: =-%" "%s:+=|%" "%s:+=|%" "%s:+=|%" "%t%")do echo %%~s
exit/b
:c
set s=%s%    +
set i=0%1
set "t=%t% %i:~-2% |

+----+ and | | are both similar to + + in that they can be generated via a single substitution, and it turns out to be slightly shorter than generating them separately (the extra quoting needed for |s doesn't help).

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

Pyth - 97 74 86 80 75 bytes

V3K+:?&NtQ2 1Q2?NQYIlK+*+\+*\-4lK\+IqN2BFTU4+sm?qT3.F"| {:02d} "d+\|*\ 4K\|

Try it here

\$\endgroup\$
  • 1
    \$\begingroup\$ Sorry, but this currently fails on input of 1. \$\endgroup\$ – Comrade SparklePony Apr 27 '17 at 19:06
  • \$\begingroup\$ @ComradeSparklePony Fixed it. Thanks. \$\endgroup\$ – Maria Apr 27 '17 at 19:32
1
\$\begingroup\$

JavaScript ES6, 224 bytes

n=>(r=(s,k)=>s.repeat(k),s="",[0,1].map(i=>(c=(n/2+n%2*i)|0,c&&(s+="+"+r(l="----+",c)+`
|`+r(r("    |",c)+`
|`,3),[...Array(c).keys()].map(j=>s+=` ${(h=2*j+(i+!(i&j>c-2&n%2)))>9?h:"0"+h} |`),s+=`
`+(i?`+${r(l,c)}
`:"")))),s)

Used some ideas from math junkie's Python answer

Test Snippet

f=
n=>(r=(s,k)=>s.repeat(k),s="",[0,1].map(i=>(c=(n/2+n%2*i)|0,c&&(s+="+"+r(l="----+",c)+`
|`+r(r("    |",c)+`
|`,3),[...Array(c).keys()].map(j=>s+=` ${(h=2*j+(i+!(i&j>c-2&n%2)))>9?h:"0"+h} |`),s+=`
`+(i?`+${r(l,c)}
`:"")))),s)

O.innerHTML=f(I.value);
<input id="I" value="5" type="number" min="0" max="99" oninput="O.innerHTML=f(this.value)">
<pre id="O"></pre>

Cleaned up

n => {
    r=(s,k)=>s.repeat(k);
    s="";
    [0,1].map(i => {
        c = (n/2 + n%2 * i)|0;
        if (c) {
            s += "+" + r(l="----+", c) + "\n|" + r(r("    |",c) + "\n|", 3);
            [...Array(c).keys()].map(j => {
                s += ` ${(h = 2*j + (i + !(i & j>c-2 & n%2))) > 9 ? h:"0"+h} |`;
            });
            s += "\n" + (i ? `+${r(l,c)}\n` : "");
        }
    });
    return s;
};
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 37 bytes

NθF…·¹θ«F⁵¿﹪ι²¿⁼ιθ→↗↓B⁶±⁶↗→→0P←⮌Iι←←↙

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

Nθ

Input the number of lockers into q.

F…·¹θ«

Loop over the lockers from 1 to q inclusive.

F⁵¿﹪ι²¿⁼ιθ→↗↓

Calculate the direction to the next locker, and repeat that 5 times (golfier than using jump movements).

B⁶±⁶

Draw the locker, starting at the bottom left corner. (The bottom right corner also takes 4 bytes while the top right corner takes 5. The top left corner only takes 3 bytes but then the locker number would take longer to draw.)

↗→→0

Draw the leading zero of the locker number, if necessary.

P←⮌Iι

Draw the locker number reversed and right-to-left, effectively right justifying it.

←←↙

Move back to the bottom left corner ready to calculate the direction to the next locker.

Edit: Later versions of Charcoal support this 32-byte solution:

F⪪…·¹N²«F⮌ι«↗→→0P←⮌Iκ↖↖↖↑UR⁶»M⁵χ

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

F⪪…·¹N²«

Take the numbers from 1 to the input number inclusive pairwise. (If the input number is odd the last array will only have one element.)

F⮌ι«

Loop over each pair in reverse order.

↗→→0

Draw the leading zero of the locker number, if necessary.

P←⮌Iι

Draw the locker number reversed and right-to-left, effectively right justifying it.

↖↖↖↑UR⁶

Move to the top left of the locker and draw it. This is also the bottom left of the next locker, so we're ready to draw the second locker of the pair if applicable.

»M⁵χ

Move to the next pair of lockers. (This should be before the inner loop for a saving of 1 byte but Charcoal generates incorrect output for an input of 1 for some reason.)

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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