20
\$\begingroup\$

Your work is to recreate this piece of art:

                        _____
                       |
                       |
                       |
                       |
                  _____| 
                 |
                 |
                 |
                 |
            _____|
           |
           |
           |
           |
      _____|
     |
     |
     |
     |
_____|

The answer must recreate this and print this as a result. All languages allowed, no direct printing of art, ofc, some level of manipulation is required. The answer with the least bytes wins.

Closes on Thursday 6:30 AM UTC or so.

The original thing was shown to me by my friend who did this with Java, he refused to show me the source code and now I'll dazzle him with the brilliance of other languages possibly. :D

You cannot use any alternative character (makes it easier?).


Current leader board

  1. Pyth - 28 bytes - isaacg
  2. CJam - 30 bytes - Runer112
  3. CJam - 32 bytes - Martin Büttner

Highest votes: C - 73 bytes - Paul R


isaacg takes the crown for passing the Staircase Challenge with Pyth. Watch out for more challenges like these on PPCG!

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Programming Puzzles & Code Golf Stack Exchange! All challenges here require an objective winning criterion, to indisputably decide which solution should win. This looks like a code-golf question, i.e. shortest code wins, but I'll let you edit it in yourself in case you want to make it something different. Thanks! \$\endgroup\$ – Doorknob Mar 2 '15 at 15:08
  • 1
    \$\begingroup\$ Ah, sorry, I missed that. I've edited the proper tag into your question, then. \$\endgroup\$ – Doorknob Mar 2 '15 at 15:11
  • 13
    \$\begingroup\$ Do we have to print that 1 weird trailing space on the 6th line ? \$\endgroup\$ – Optimizer Mar 2 '15 at 15:45
  • 4
    \$\begingroup\$ More generally, is trailing space allowed? Can I pad this to a rectangle of the width of the first line? \$\endgroup\$ – Martin Ender Mar 2 '15 at 15:56
  • 8
    \$\begingroup\$ Can we have a trailing newline? \$\endgroup\$ – TheNumberOne Mar 2 '15 at 16:26

21 Answers 21

4
\$\begingroup\$

Pyth, 29 28

V21++**6/-20N5d*5?d%N5\_<\|N

Try it here.

A pretty straightforward solution, with the "append five spaces or five underscores" trick from @xnor's solution, but with the loop from 0 to 20, not 20 to 0.

\$\endgroup\$
  • 1
    \$\begingroup\$ I dub you thee, Sir isaacg for passing the Staircase Challenge. \$\endgroup\$ – therewillbecoffee Mar 6 '15 at 11:24
  • \$\begingroup\$ ...and now you can enter the staircase \$\endgroup\$ – Anthony Pham Aug 18 '15 at 20:42
22
\$\begingroup\$

C, 86 80 76 75 73 bytes

c;main(i){for(i=21;i--;c='|')printf("%*s%c\n",i/5*6+5,i%5?"":"_____",c);}
\$\endgroup\$
  • 5
    \$\begingroup\$ There always has to be that guy who will post a C solution. Upvote for you. \$\endgroup\$ – therewillbecoffee Mar 2 '15 at 17:56
  • 1
    \$\begingroup\$ You could get it even shorter by changing the loop to for(i=25;i--;) \$\endgroup\$ – Felix Bytow Mar 2 '15 at 18:02
  • 1
    \$\begingroup\$ @FelixBytow It should be i=26. Besides this, the ' ' could be changed to 32 for an extra character. My solution is 2 characters longer after these optimizations :( \$\endgroup\$ – Allbeert Mar 2 '15 at 18:37
  • 2
    \$\begingroup\$ You seem to have an extra step. there are 21 rows in the required output, not 26. Some more optimizations: main(i){for(i=21;i--;)printf("%*s%c\n",i/5*6+5,i%5?"":"_____",i<20?'|':0);} 1. Simplify the formula for length 2. As @Allbeert says, you can use the ascii code for ' ' but why stop at ASCII 32 when ASCII 0 will do. Also it runs fine for me with "" instead of " " \$\endgroup\$ – Level River St Mar 2 '15 at 18:54
  • 1
    \$\begingroup\$ How about this to save two bytes in '|' printing? c;main(i){for(i=21;i--;c='|')printf("%*s%c\n",i/5*6+5,i%5?"":"_____",c);} \$\endgroup\$ – Runer112 Mar 3 '15 at 13:56
11
\$\begingroup\$

Java, 198 158 156 146 bytes

This can probably be shortened a lot. As usual, suggestions are welcome.

void a(){String a="",b="_____",c=b;for(int i=-1;i<20;b=i%5<1?b.replace(c,"     "):i%5>3?b+" "+c:b)a=b+(++i>19?"":"|")+"\n"+a;System.out.print(a);}

Indented (kinda):

void a(){
    String a="",b="_____",c=b;
    for(int i=-1;i<20;b=i%5<1?b.replace(c,"     "):i%5>3?b+" "+c:b)
        a=b+(++i>19?"":"|")+"\n"+a;
    System.out.print(a);
}

Thanks Martin Büttner, Rainbolt, and Geobits.

\$\endgroup\$
  • 1
    \$\begingroup\$ Honestly, since you did it with Java, I'm impressed. \$\endgroup\$ – therewillbecoffee Mar 2 '15 at 16:57
9
\$\begingroup\$

Brainfuck (1065 Bytes)

It's not pretty, it's not short...but i'll optimize later on!

++++[->++++++++<]>........................
[->+++<]>-.....>++++++++++.[->+++<]>++....
...................-[->++++<]>.>++++++++++
.[->+++<]>++.......................-[->+++
+<]>.>++++++++++.[->+++<]>++..............
.........-[->++++<]>.>++++++++++.[->+++<]>
++.......................-[->++++<]>.>++++
++++++.[->+++<]>++..................[->+++
<]>-.....[->++++<]>.>++++++++++.[->+++<]>+
+.................-[->++++<]>.>++++++++++.
[->+++<]>++.................-[->++++<]>.>+
+++++++++.[->+++<]>++.................-[->
++++<]>.>++++++++++.[->+++<]>++...........
......-[->++++<]>.>++++++++++.[->+++<]>++.
...........[->+++<]>-.....[->++++<]>.>++++
++++++.[->+++<]>++...........-[->++++<]>.>
++++++++++.[->+++<]>++...........-[->++++<
]>.>++++++++++.[->+++<]>++...........-[->+
+++<]>.>++++++++++.[->+++<]>++...........-
[->++++<]>.>++++++++++.[->+++<]>++......[-
>+++<]>-.....[->++++<]>.>++++++++++.[->+++
<]>++.....-[->++++<]>.>++++++++++.[->+++<]
>++.....-[->++++<]>.>++++++++++.[->+++<]>+
+.....-[->++++<]>.>++++++++++.[->+++<]>++.
....-[->++++<]>.>++++++++++.[--->++<]>+++.
....[->++++<]>.
\$\endgroup\$
8
\$\begingroup\$

CJam, 36 30 bytes

Try it online.

L{{S5*\+}/S'|5*+]'_5*+}5*1>zN*

My initial 36-byte solution generated the result in the output orientation. Despite my attempts to squeeze more bytes out of the algorithm, I couldn't. Then I saw Martin's brilliant strategy of generating columns instead of rows and transposing the result. I realized that was probably a better approach, so I set off to create a transposition-based solution.

However, my approach to implementing that strategy varies quite a bit. Instead of generating full columns, I use an iterative solution that indents any already-generated "steps" and adds adds a new step at each iteration. So the first iteration of the main loop generates this:

 |||||
_
_
_
_
_

The second iteration of the main loop indents the existing step and adds a new one after it:

      |||||
     _
     _
     _
     _
     _
 |||||
_
_
_
_
_

And the full five iterations of the main loop generate this:

                     |||||
                    _
                    _
                    _
                    _
                    _
                |||||
               _
               _
               _
               _
               _
           |||||
          _
          _
          _
          _
          _
      |||||
     _
     _
     _
     _
     _
 |||||
_
_
_
_
_

After this, all that needs to be done is eliminate the first line, which would otherwise become the unwanted riser for the bottom step, and transpose.

\$\endgroup\$
7
\$\begingroup\$

Python 2, 80 77 74 bytes

n=24;exec"print' '*n+'_'*5+'|'*(n<24)+('\\n'+~-n*' '+'|')*4*(n>0);n-=6;"*5

Got rid of the double exec and fit everything into the one print!

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

Clip, 46

{:24S:5'_m[z{*4,:+5*6zS"|
":*6zS:5'_"|
"`}vR4`

Explanation

{               .- Put everything in a list -.
 :24S           .- 24 spaces                -.
 :5'_           .- 5 underscores            -.
 m[z            .- Map...                   -.
    {           .- A list                   -.
     *4         .- 4 of the following       -.
       ,        .- Append                   -.
        :+5*6zS .- 5 + 6 * the iteration of spaces  -.
        "|      .- A pipe and newline       -.
"
     :*6zS      .- 6 * the iteration of spaces      -.
     :5'_       .- 5 underscores            -.
     "|         .- A pipe and newline       -.
"
    `           .- End list (per iteration  -.
   }vR4         .- The mapping is onto {3,2,1,0}    -.
\$\endgroup\$
  • 1
    \$\begingroup\$ Clip doesn't even have a Wikipedia page. I mean, what.... \$\endgroup\$ – therewillbecoffee Mar 2 '15 at 17:56
  • 4
    \$\begingroup\$ @therewillbecoffee Many languages on this site don't have a Wikipedia page. That's the fun ;) \$\endgroup\$ – Ypnypn Mar 2 '15 at 18:07
  • \$\begingroup\$ @Ypnypn Did you design it? It looks really interesting! Although I'd be interested in a true quine. ;) (That one on the examples page is a bit cheaty.) \$\endgroup\$ – Martin Ender Mar 2 '15 at 18:11
6
\$\begingroup\$

CJam, 36 32 bytes

{5*S*'_+a5*~_W<S+5'|*+}5/;]W%zN*

Test it here.

I also tried using an explicit formula, but it's longer in CJam... maybe it helps someone else:

21,29,ff{_2$5/)6*(=@@6/5*=_++" |_|"=}W%N*

Explanation

I found that the staircase can be built much more easily if you a) transpose the grid and b) reverse the lines:

_
_
_
_
_
 |||||
     _
     _
     _
     _
     _
      |||||
          _
          _
          _
          _
          _
           |||||
               _
               _
               _
               _
               _
                |||||
                    _
                    _
                    _
                    _
                    _

So first I'm building that, then reverse, then transpose.

{                     }5/        "For i in [0 .. 4].";
 5*S*'_+                         "Get a string of 5*i spaces and append _.";
        a5*~                     "Get five such lines.";
            _W<S+                "Duplicate the last, remove the _, add a space.";
                 5'|*+           "Add 5 copies of |.";
                         ;       "The above creates a row too many, so discard the last one.";
                          ]W%zN* "Wrap everything in an array, reverse, transpose, riffle
                                  with newlines.";
\$\endgroup\$
  • \$\begingroup\$ Okay, someone's did it with 30 bytes. \$\endgroup\$ – therewillbecoffee Mar 3 '15 at 10:26
6
\$\begingroup\$

Python 2, 59

n=21
exec"n-=1;print n/5*6*' '+' _'[n%5<1]*5+'|'*(n<20);"*n

The 21 lines are indexed by n in [20,19,...,1,0]. First prints 6 spaces for each "step" we're up (minus 1), computed as n/5*6. Then, prints five spaces, except these are instead underscores for multiples of five. Finally, prints a vertical line, except for the top line n=20.

\$\endgroup\$
  • \$\begingroup\$ Nice and straightforward. I like it! \$\endgroup\$ – Sp3000 Mar 3 '15 at 2:37
6
\$\begingroup\$

JavaScript, 115 107 96 94 89 87 83 bytes

This is too long to win, but it's the first time I've come up with an answer on PCG.SE, and I'm kind of proud to have made something postable.

With some helpful syntactical advice I shortened the code significantly - even below the scrollbar threshold!

for(s='',y=22;--y;s+='\n')for(x=0;++x<29;)s+=6*~(~-y/5)-~x?4+5*~(x/6)+y?' ':'_':'|'
\$\endgroup\$
  • \$\begingroup\$ Nice answer, several things you could do to shorten it are take out the alert. If you run it in the console it works just fine without it. Also, the semicolon inside the last curly brace isn't necessary. You can save 1 byte by using (y/5-.2) instead of ((y-1)/5) \$\endgroup\$ – qw3n Mar 3 '15 at 16:33
  • \$\begingroup\$ @qw3n thanks for those suggestions; I wasn't sure if the alert was required by the rules. \$\endgroup\$ – vvye Mar 3 '15 at 19:31
  • \$\begingroup\$ You could also move the s+='\n' after the y-- and get rid of the curly braces so it looks like for(s='',y=21;y>0;y--,s+='\n'). I also initialized the s inside the for loop so your code is all one statement \$\endgroup\$ – qw3n Mar 3 '15 at 19:39
  • 1
    \$\begingroup\$ I thought that was my last one but this should be it for me for(s='',y=22;--y;s+='\n')for(x=0;++x<29;)s+=6*~~(y/5-.2)+5-x?5*~~(x/6)+1-y?' ':'_':'|' if you flip your ternary expression you can test for number - x which is 0 if both terms are equal saving you 2 more bytes. \$\endgroup\$ – qw3n Mar 3 '15 at 21:04
  • 1
    \$\begingroup\$ Tilde games: ~n == -n-1, -~n == n+1, ~-n == n-1, for(s='',y=22;--y;s+='\n')for(x=0;++x<29;)s+=6*~(~-y/5)-~x?4+5*~(x/6)+y?' ':'_':'|' is 83 \$\endgroup\$ – edc65 Mar 4 '15 at 9:37
6
\$\begingroup\$

ECMAScript 6, 142 138 129 91 bytes

Special thanks to @edc65 for really reworking this.

a=o='',[for(x of!0+o)(o=(a+'     |\n').repeat(4)+a+'_____|\n'+o,a+='      ')],a+'_____\n'+o

out.value=(a=o='',[for(x of!0+o)(o=(a+'     |\n').repeat(4)+a+'_____|\n'+o,a+='      ')],a+'_____\n'+o)
textarea{
  width:300px;
  height:400px;
}
<textarea id="out";></textarea>

The logic of the original version check @edc65 comment for how it morphed.

((f,l,p)=>                  //variables
f(24)+l+p[1]+               //add the non pattern line
[0,1,2,3].map(b=>f(18-6*b)) //add the right number of spaces in front of the 4 steps
.map((a,b)=>f(4,a+f(5)+p)   //the four repeating lines of the step 
+a+l)                       //the landing line
.join(p)+p)                 //put it all together
((n,s=' ')=>s.repeat(n)     //create an variable array of some character
,'_____','|\n')             //string literals
\$\endgroup\$
  • 1
    \$\begingroup\$ You can safely remove the new constructor before Array to save a few bytes. \$\endgroup\$ – NinjaBearMonkey Mar 3 '15 at 0:50
  • \$\begingroup\$ @hsl thanks for some reason I thought the new was necessary. \$\endgroup\$ – qw3n Mar 3 '15 at 2:59
  • 1
    \$\begingroup\$ Array(n).join(s) is so ES5! did you try repeat \$\endgroup\$ – edc65 Mar 3 '15 at 7:31
  • \$\begingroup\$ [1,2,3,4].map((a,b) and using just b => [0,1,2,3].map(b (-4) \$\endgroup\$ – edc65 Mar 3 '15 at 7:36
  • \$\begingroup\$ I run Firefox and it works, pretty nicely! \$\endgroup\$ – therewillbecoffee Mar 3 '15 at 10:23
5
\$\begingroup\$

MATLAB, 68 bytes

I have the strong feeling MATLAB should be able to do better, but I can't think of a way.

p(1:5,6)='|';p(1,1:5)=95;w=blkdiag(p,p,p,p);w(21,25:29)=95;flipud(w)

Creates the stairs upside-down and flips it. My thaumometer broke because of all the magic constants around.

'|' is intentionally left as-is (instead of ascii codepoint) to initialize p and w as a char array.

\$\endgroup\$
  • 1
    \$\begingroup\$ '|' = 124 anyway, so that doesn't cost any extra chars. \$\endgroup\$ – Peter Cordes Mar 3 '15 at 6:42
5
\$\begingroup\$

Ruby, 48

25.times{|i|puts" "*(4-i/6)*5+(i%6==0??_*5:?|)}

Old approach, 68

4.times{|i|(?_*5+"
|"*5).each_line{|l|puts" "*(4-i)*5+l}}
puts"_"*5
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! A couple of Ruby golfing tips: 1. there's some unnecessary whitespace in there. 2. Single character strings like '_' can be written as ?_. 3. Newlines can be embedded directly in strings (so you can actually do "<linebreakhere>|"). 4. The parentheses around that aren't necessary. The final puts can be replaced with $><< (which allows you to get rid of the space, even after using ?_). Keep it up! :) \$\endgroup\$ – Martin Ender Mar 3 '15 at 13:16
  • \$\begingroup\$ Yeah, I just got rid of some whitespace. Thanks! I didn't know about the single character strings. \$\endgroup\$ – psycotica0 Mar 3 '15 at 13:19
  • \$\begingroup\$ You can probably also replace (1..4).map by 4.times and then use 4-i instead of 5-i. \$\endgroup\$ – Martin Ender Mar 3 '15 at 13:26
  • \$\begingroup\$ Ooh, good call. Done. \$\endgroup\$ – psycotica0 Mar 3 '15 at 13:29
4
\$\begingroup\$

Julia, 83 bytes

for n=24:-6:0 print(" "^n*"_"^5*"|"^(n<24)*"\n"*(" "^(n>0?n-1:0)*"|\n"^(n>0))^4)end

In Julia, string concatenation is performed using the * operator and string repetition is performed using ^.

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

><>, 108 104 100 bytes

cc+::?v~'_____'o\/' 'o  \
?:o'|'\' 'o1-30.o\v!?:-1<}:{oav!?:<4;!
-20.12^?(+cc:ooo/ \~1-'!|'o1. \~ao6

A simple ><> solution, using the same strategy as my Python answer. The main difference is that ><> doesn't have string multiplication (or even strings), so all of that is done with loops.

Explanation

cc+                  Push 24 (call this "n")

[outer loop]
[loop 1, print n spaces]

:                    Copy n (call this "i")
:?                   If i is not zero...
' 'o1-30.                Print space, decrement i and go to start of loop 1

~'_____'ooooo        Pop i and print five underscores
:cc+(?               If n < 24...
'|'o                     Print a pipe
21.                  Otherwise skip pipe printing

[loop 2: print vertical parts of stairs]

?!;                  If n is zero, halt
4                    Push 4 (call this "j")
?!                   If j is zero...
~ao6-20.                 Pop j, print a newline, minus 6 from n and go to start of outer loop
ao                   Print a newline
}:{                  Copy n (call this "k")

[loop 3: print n-1 spaces]

1-                   Decrement k
:?!                  If k is zero...
    ~1-'!|'o1.           Pop k, decrement j, print a pipe and go to start of loop 2
' 'o                 Otherwise print a space and go to start of loop 3
\$\endgroup\$
  • \$\begingroup\$ I feel like I will always upvote a ><> answer. \$\endgroup\$ – krs013 Mar 4 '15 at 17:14
3
\$\begingroup\$

Groovy, 98 71 bytes

// old: (25..5).each{i->println((1..(i-(i-1)%5)).collect{' '}.join()+(i%5?'|':('_____'+(i==25?'':'|'))))}

(25..5).each{i->println(' '*(i-(i-1)%5)+(i%5?'|':'_'*5+(i%25?'|':'')))}

(25..5).each { i ->
    println(
        ' '*(i - (i - 1) % 5) + (
            i % 5 ? 
            '|' : 
            '_'*5 + (i % 25 ? '|' : '')
        )
    )
}    

I'm pretty sure, that it can be reduced somehow :) shortened by @Score_Under

\$\endgroup\$
  • 1
    \$\begingroup\$ I have a few bytes to shave: Replace the collect/join with a multiply: ' '*(i-(i-1)%5), remove brackets from around ('_____'+(i==25?'':'|')), replace the '_____' with '_'*5, and if you flip the last conditional you can use % as an unorthodox inequality operator: (i%25?'|':''). This should get you down to 71. \$\endgroup\$ – Score_Under Mar 5 '15 at 3:42
  • \$\begingroup\$ @Score_Under nice, thanks, I didn't know about multiplying strings :D \$\endgroup\$ – Kamil Mikolajczyk Mar 5 '15 at 10:13
2
\$\begingroup\$

Perl, 50

#!perl -l
print$"x6x(-$_/5),($_%5?$":_)x5,"|"x$|++for-20..0

Try me.

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

T-SQL, 276 bytes

declare @x int declare @y int declare @w varchar(30) declare @l char(5) set @l='_____' set @x=23 while @x > 4 begin set @w=replicate(' ',@x) set @y=0 if @x=23 print @w+' '+@l else print @w+' '+@l+'|' while @y < 4 begin set @y=1+@y print @w+'|' end set @x=@x-6 end print @l+'|'
\$\endgroup\$
2
\$\begingroup\$

Visual FoxPro 9.0, 261 bytes

n = Number of Steps

total of 175 characters, but had to output to file to display correctly - so minus 43 chars for file operations = 132 chars.

n=10
c=CHR(13)
f="st.t"
ERAS (f)    
FOR i=n TO 1 STEP -1
    p=(i-1)*6
    =STRTO(PADL("_____",p+5)+IIF(i<n,"|","")+c+IIF(i>1,REPLI(PADL("|"+c,p+1),4),""),f,.t.)
    ?PADL("_____",p+5)+IIF(i<n,"|","")+c+IIF(i>1,REPLI(PADL("|"+c,p+1),4),"")
ENDFOR
MODI COMM (f)

Note to answerer: Byte count is for the absolute working source code, and the byte counter says that it's 261 bytes, so it is.

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

Bash (+tac from coreutils): 110 bytes

This can be pasted directly into the terminal.

(n=;set {1..4};for i do echo "${n}_____|";n+='     ';for i do echo "$n|";done;n+=\ ;done;echo "${n}_____")|tac
\$\endgroup\$
  • \$\begingroup\$ Without |tac, it doesn't quite work. And I ran this on Git Bash, so I should kill myself. \$\endgroup\$ – therewillbecoffee Mar 5 '15 at 4:42
1
\$\begingroup\$

x86 machine code, 48 bytes

B1 05 B4 0E 80 E9 01 B0 5F B5 05 80 ED 01 CD 10 80 FD 00 75 F6 B5 05 80 ED 01 B0 0A CD 10 B0 08 CD 10 B0 7C CD 10 80 FD 00 75 EC 80 F9 00 75 D4

Assembly code equivalent:

mov cl, 5
mov ah, 0Eh
main:
    sub cl, 1

print_step:
    mov al, '_'
    mov ch, 5

.loop:
    sub ch, 1

    int 10h

    cmp ch, 0
    jne .loop

print_stoop:
    mov ch, 5

.loop:
    sub ch, 1

    mov al, 0Ah;    ascii newline
    int 10h

    mov al, 8;      ascii backspace
    int 10h

    mov al, '|'
    int 10h

    cmp ch, 0
    jne .loop

cmp cl, 0
jne main

Output:

_____
    |
    |
    |
    |
    |_____
         |
         |
         |
         |
         |_____
              |
              |
              |
              |
              |_____
                   |
                   |
                   |
                   |
                   |_____
                        |
                        |
                        |
                        |
                        |

Sorry about the output being different; I hope it is acceptable.

This was run in DOSBOX

\$\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.