24
\$\begingroup\$

Your task is to create a program or function that takes, as input, a natural number (n) between 1 and 25 (inclusive) and prints an isometric representation of a slide and ladder with n number of rungs.

Ladder and slide specifications

The ladder is always oriented on the left and the slide on the right. We're viewing it from the ladder side, so part of the slide is obscured at the top three levels. The rungs are represented by four dashes (----) and the sides of the ladder and slide by slashes (/ and \). The following is a diagram to represent the patterns of spaces needed for a slide with five rungs.

Slide              Blank space count
    /----/\        1234/----/\ 
   /----/  \       123/----/12\ 
  /----/    \      12/----/1234\ 
 /----/ \    \     1/----/1\1234\ 
/----/   \    \    /----/123\1234\

Examples

>>1
/----/\

>>3
  /----/\ 
 /----/  \ 
/----/    \ 

>>4
   /----/\ 
  /----/  \ 
 /----/    \ 
/----/ \    \ 

>>10
         /----/\ 
        /----/  \ 
       /----/    \ 
      /----/ \    \ 
     /----/   \    \ 
    /----/     \    \ 
   /----/       \    \ 
  /----/         \    \ 
 /----/           \    \ 
/----/             \    \ 

This is code-golf, so the answer with the lowest byte count wins.

Note: trailing white space is acceptable in the output, as long as it doesn't exceed the length of the line.

\$\endgroup\$
8
  • 14
    \$\begingroup\$ You would break your legs at the end of that slide! \$\endgroup\$
    – Fatalize
    Commented Jul 20, 2016 at 12:06
  • 13
    \$\begingroup\$ @Fatalize that'd be... Fatal. \$\endgroup\$
    – Ven
    Commented Jul 20, 2016 at 12:07
  • 7
    \$\begingroup\$ @Fatalize bonus points for you if you draw a pool at the bottom ;) \$\endgroup\$ Commented Jul 20, 2016 at 12:08
  • 8
    \$\begingroup\$ @atlasologist A pool of blood? \$\endgroup\$
    – Neil
    Commented Jul 20, 2016 at 19:09
  • \$\begingroup\$ Apologies if this is a dumb question, but a couple of the answers below (Python, JavaScript) define a function that returns a string rather than printing it. Is that kosher? \$\endgroup\$
    – Jordan
    Commented Jul 20, 2016 at 20:11

15 Answers 15

17
\$\begingroup\$

Retina, 81 72 52 bytes

Byte count assumes ISO 8859-1 encoding.

.+
$* /\    \
^.

+` /(.+)$
$&¶$%`/  $1
/.{5}
/----/

Try it online!

Explanation

The program consists of four stages, all of which are regex substitutions (with a couple of Retina-specific features). I'll use input 5 as an example for the explanation.

Stage 1

.+
$* /\    \

This turns the input n into n spaces followed by /\ \ which will become the top of the ladder/slide:

     /\    \

For now, we'll just show the slide in full and represent the ladder only by its left-hand /.

Stage 2

^.

Unfortunately, n spaces are one more than we need, so we remove the first character again. We've now got:

    /\    \

Stage 3

+` /(.+)$
$&¶$%`/  $1

Time to expand the complete structure. Knowing where the top is, is sufficient to build the entire thing, because we can simply extend it one line at a time, moving the ladder and the slide apart by two spaces.

The + tells Retina to repeat this stage in a loop until the output stops changing (in this case, because the regex stops matching). As for the regex itself, we simply match the / on the last line and everything after it and we also match one space in front of it, which means this can no longer match once / has reached the first column.

Here's what we replace this with:

 $&    The match itself. We don't want to remove the line we already have.
 ¶     A linefeed, because we want to append a new line.
 $%`   This is a very recent addition to Retina: it's like the normal $` but
      is bounded by linefeeds. That means this inserts everything in front
      of the match which is on the same line. In particular this one space
      less than the indentation of the matched line, hence we are shifting
      the / one column left.
 /     A literal /, representing the left edge of the ladder.
>  <   Two spaces, so that we can shift the slide one column right.
 $1    Capturing group 1 which contains the slide and its separation from
      the ladder.

So at each iteration, this adds one line to the string, until we end up with this:

    /\    \
   /  \    \
  /    \    \
 /      \    \
/        \    \

Stage 4

/.{5}
/----/

All that's left is getting the ladder right. That's really simple, we just match the / and the next 5 characters and insert the correct ladder representation, thereby overriding the slide or spaces that are already there:

    /----/\
   /----/  \
  /----/    \
 /----/ \    \
/----/   \    \
\$\endgroup\$
9
\$\begingroup\$

V, 38, 37, 36, 34, 33, 32, 31, 30 29 bytes

Àé r\2é/4é-òhYpX$2P^ò3GEòjlr\

Try it online!

I might catch up with Osabie. One byte shorter than Osabie. \o/ Tied with 2sable! One byte shorter!

In other news, this is definitely the longest strikethrough header I've ever made.

Explanation:

Àé              "Insert 'arg1' spaces
   r\           "Turn the last one into a '\'
     2é/        "Insert 2 '/'
        4é-     "Insert 4 '-'

ò        ò      "Recursivly:
 h              "  Move one to the left
  Yp            "  Duplicate this line
    X           "  Delete one space from the left
     $2P        "  Paste two spaces at the end of this line
        ^       "  Move back to the beginning of this line.

This will run until an error occurs, which thanks to the "move left" command ('h'), will be 'arg1' times.

Now we just need to add the inner leg

3GE             "Move to the second slash of line 3
   ò    ò       "Recursively: (The second 'ò' is implicit)
    jl          "  Move down and to the right
      r\        "  And replace the character under the cursor with a '\'

Non-competing version (28 bytes)

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

Pyth, 39 35 bytes

VQ++*dt-QN"/----/">+*+ddN"\    \\"5

Explanation:

VQ                                  # Interate over 0 -> Q-1 (Q is the input)
  +                                 # Concatenate the 2 halfs of the slide
   +                                # Concatenate the whitespace block and the ladder
    *d                              # Multiply d (whitespace) by this number \/
      t-QN                          # Calculate the amount of spaces before : input - step of the iterarion -1
          "/----/"                  # Ladder
                  >               5 # Remove the first 5 chars  from the string generated in the following lines 
                   +                # Concatenate the whitespace block and the slide
                    *+ddN           # Multiply "+dd" (2 whitespace) by the step of the iterarion to generate the space between the ladder and the slide
                         "\    \\"  # Slide

Test here

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

2sable, 40 36 32 30 bytes

Hmmm, V is coming awfully close...

F¹N-<ð×…/--«ðð«N×…\  ««5F¦}¶

Uses the CP-1252 encoding. Try a 05AB1E compatible version!.

\$\endgroup\$
3
  • \$\begingroup\$ And the battle rages on: codegolf.stackexchange.com/a/85985/31716 :P \$\endgroup\$
    – DJMcMayhem
    Commented Jul 20, 2016 at 18:28
  • \$\begingroup\$ @DrGreenEggsandIronMan Hmmm!!?!?! \$\endgroup\$
    – Adnan
    Commented Jul 20, 2016 at 19:22
  • \$\begingroup\$ There's one more off, and one more once I get home. Also, I'm not switching languages. :P \$\endgroup\$
    – DJMcMayhem
    Commented Jul 20, 2016 at 19:35
7
\$\begingroup\$

PowerShell v2+, 99 90 82 bytes

param($n)1..$n|%{" "*($n-$_)+"/----/"+-join(" "*($_+$i++)+"\    \")[6..(6+$_+$i)]}

Takes input $n, starts a loop from 1 to $n with |%{...}. Each iteration, we're constructing a string. We start with the appropriate number of spaces " "*($n-$_) and the ladder "/----/".

To that, we add another string that's been sliced [...] and -joined back together. The second string is the slide, and we assume that the whole slide is always visible. It's the number of spaces before the slide would start " "*($_+$i++), followed by the slide itself "\ \". This is sliced by a range calculated out to be the part of the "spaces plus slide" that's partially hidden by the ladder.

Examples

PS C:\Tools\Scripts\golfing> .\draw-a-ladder-and-slide.ps1 7
      /----/\
     /----/  \
    /----/    \
   /----/ \    \
  /----/   \    \
 /----/     \    \
/----/       \    \

PS C:\Tools\Scripts\golfing> .\draw-a-ladder-and-slide.ps1 15
              /----/\
             /----/  \
            /----/    \
           /----/ \    \
          /----/   \    \
         /----/     \    \
        /----/       \    \
       /----/         \    \
      /----/           \    \
     /----/             \    \
    /----/               \    \
   /----/                 \    \
  /----/                   \    \
 /----/                     \    \
/----/                       \    \
\$\endgroup\$
2
  • 7
    \$\begingroup\$ +1 because the slashes in your directory line up with the slashes in the test output. :D \$\endgroup\$
    – DJMcMayhem
    Commented Jul 20, 2016 at 19:04
  • \$\begingroup\$ Your \golfing folder must be very organized ._. \$\endgroup\$ Commented Jul 20, 2016 at 19:15
6
\$\begingroup\$

Python 2 - 79 76 75 bytes

x=input()
for i in range(x):print(x-i)*' '+'/----/'+(i*'  '+'\\    \\')[5:]

Thanks to Hubert Grzeskowiak for "disqualifying" me since making my program print actually saved 3 bytes! Thanks also to Eʀɪᴋ ᴛʜᴇ Gᴏʟғᴇʀ for saving 1 more byte!

\$\endgroup\$
5
  • \$\begingroup\$ Nice! By the way, lambda was okay, I think, since strictly speaking it is a sort of function. Using lambda you can save a newline and a space :-) \$\endgroup\$
    – Hugo G
    Commented Jul 20, 2016 at 21:45
  • \$\begingroup\$ Wait, I actually tried it with lambda and couldn't get it working. It's bc of the print, I guess? \$\endgroup\$
    – Hugo G
    Commented Jul 20, 2016 at 22:08
  • \$\begingroup\$ @HubertGrzeskowiak Well it depends on challenge specifications. If the question asked for a program/function that returns a string representation of a slide, either of my answers would be valid. Since it specifies "print" as you pointed out, I would have either needed to name the lambda function and print a call to it, or do what I did above which was shorter. \$\endgroup\$ Commented Jul 20, 2016 at 22:29
  • \$\begingroup\$ The specs don't tell that the function has to be named, or be called ;-) \$\endgroup\$
    – Hugo G
    Commented Jul 20, 2016 at 23:35
  • \$\begingroup\$ Convert it to a full program: replace def f(x): with x=input(), and remove the indentation on the second line. This saves you 1 byte. \$\endgroup\$ Commented Jul 21, 2016 at 11:52
6
\$\begingroup\$

Vim, 71 keystrokes

This is such a stupid way of doing it, but it's a bit of fun. Input is given as a text file with a single line containing an integer. This is likely quite golfable, but it'll do for now (edit: switched style of control characters upon request):

A@qyyPgg<c-v><c-v>GkI <c-v><esc>G$i  <c-v><esc><esc>
Tq"qDI/----/\^[lD@"ddh<c-v>god:%s/     \\/\\    \\<cr>

<c-v>, <esc> and <cr> are all individual keystrokes; ctrl+v, escape, and carriage return (enter) respectively. For a simple to digest version with the correct literals, here is the file ladder.keys run though xxd:

00000000: 4140 7179 7950 6767 1616 476b 4920 161b  [email protected] ..
00000010: 4724 6920 2016 1b1b 5471 2271 4449 2f2d  G$i  ...Tq"qDI/-
00000020: 2d2d 2d2f 5c1b 6c44 4022 6464 6816 676f  ---/\.lD@"ddh.go
00000030: 643a 2573 2f20 2020 2020 5c5c 2f5c 5c20  d:%s/     \\/\\
00000040: 2020 205c 5c0d 0d0a                         \\...

To try it out (assuming a nix with the appropriate tools) take the above, run it through xxd -r and put in the file ladder.keys. Create a file ladder.txt with an integer in it. Then do:

vim -s ladder.keys -u NONE ladder.txt
\$\endgroup\$
3
  • \$\begingroup\$ We usually write ^V as <c-v> here. \$\endgroup\$
    – Leaky Nun
    Commented Jul 21, 2016 at 19:23
  • \$\begingroup\$ @Jordan They are one keystroke each, counting modified keys (shift, ctrl or alt with another key) as one keystroke. The keystrokes are Ctrl+v, escape and enter. \$\endgroup\$
    – algmyr
    Commented Jul 21, 2016 at 23:28
  • \$\begingroup\$ @algmyr My mistake. I misread your answer earlier. \$\endgroup\$
    – Jordan
    Commented Jul 21, 2016 at 23:32
6
\$\begingroup\$

bash, 61

for((;i<$1;)){ printf "%$[$1+i]s\    \^M%$[$1-++i]s/----/\n";}

where ^M is a literal carriage return

$ ./ladder 1
/----/\
$ ./ladder 4
   /----/\
  /----/  \
 /----/    \
/----/ \    \
$ ./ladder 10
         /----/\
        /----/  \
       /----/    \
      /----/ \    \
     /----/   \    \
    /----/     \    \
   /----/       \    \
  /----/         \    \
 /----/           \    \
/----/             \    \
\$\endgroup\$
3
  • \$\begingroup\$ I think you should have a single space between / and `\` in the 4th row, and 1 less space between slide edges. \$\endgroup\$
    – Leibrug
    Commented Jul 21, 2016 at 11:25
  • \$\begingroup\$ It should be 4 dashes, not 5. \$\endgroup\$
    – algmyr
    Commented Jul 28, 2016 at 13:26
  • \$\begingroup\$ @algmyr thank you that saves 2 bytes \$\endgroup\$
    – izabera
    Commented Jul 28, 2016 at 15:09
5
\$\begingroup\$

JavaScript (ES6), 79 bytes

f=
n=>" ".repeat(n).replace(/./g,"$'/$`$`\\    \\\n").replace(/\/...../g,"/----/")
;
<input type=number min=0 oninput=o.textContent=f(this.value)><pre id=o>

Works by taking a string of n spaces, then doing some exotic substitution to get the slide with a support, then replacing the support with a ladder.

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

Ruby, 61 bytes

->n{n.times{|i|puts"%*s\\    \\\r%*s----/"% [n+i,"",n-i,?/]}}

Ungolfed

->(num_rows) {
  num_rows.times {|row_idx|
    puts "%*s\\    \\\r%*s----/" % [ num_rows + row_idx, "", num_rows - row_idx, "/" ]
  }
}

I could save two bytes by using '%*s\ \^M%*s----/' (where ^M is a literal carriage return) for the format string, but then Ruby prints the warning "warning: encountered \r in middle of line, treated as a mere space". ¯\_(ツ)_/¯

Previous solution (64 bytes)

->n{n.times{|i|puts" "*(n+i)+"\\    \\\r"+" "*(n-i-1)+"/----/"}}
\$\endgroup\$
3
\$\begingroup\$

Batch, 194 bytes

@echo off
for /l %%i in (1,1,%1)do call:l %1 %%i
exit/b
:l
set s=\    \
for /l %%j in (1,1,%2)do call set s=  %%s%%
set s=/----/%s:~7%
for /l %%j in (%2,1,%1)do call set s= %%s%%
echo%s%

Turned out to be reasonably straightforward: indent the slide, remove the first 7 characters, indent the ladder, remove the leading space. This last bit does involve a little trickery though!

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

Java, 116 bytes

c->{for(int i=0;i<c;i++)System.out.format("%"+(5+c-i)+"s%"+(i<3?i*2+1:2*(i-2))+"s%5s\n","/----/","\\",i<3?"":"\\");};

Unfortunately, you can't [easily] duplicate strings in Java, so I end up abusing the format function.

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

Scala, 95 bytes

def l(n:Int)=for(i<- 0 to n-1){println(" "*(n-i-1)+"/----/"+("  "*i+"\\    \\").substring(5))}
\$\endgroup\$
2
\$\begingroup\$

Haskell, 81 bytes

a n=[1..n]>>" "
f n=[1..n]>>=(\i->a(n-i)++"/----/"++drop 7(a(2*i)++"\\    \\\n"))
\$\endgroup\$
1
  • \$\begingroup\$ do i<-[1..n];a(n-i)++"/----/"++drop 7(a(2*i)++"\\ \\\n") saves two bytes. \$\endgroup\$
    – lynn
    Commented Jul 21, 2016 at 22:52
0
\$\begingroup\$

eacal, noncompeting, 386 bytes

init .
define @ curry push .
define ~ curry exec .--func
alias $ strap
alias ' string
set n set m cast number arg number 0
set s empty string
label l
@ get n
set n ~ dec
@ space
@ get n
$ ~ repeat
$ ' /----/
@ space
@ get m
@ get n
@ ~ sub
@ ~ dec
@ number 2
@ ~ mul
$ ~ repeat
$ ' \
$ newline
@ get n
@ number 0
if ~ more
goto l
@ $
@ regex gm '   ( {4})(?=.$)
@ '  \$1
print ~ replace

I have officially made the most verbose language possible. I made the comment in jest and in sarcasm. Please calm down. Instructions on how to run in the github repo linked in the header.

Ungolfed

init .
set n set m cast number arg number 0
set s empty string
label loop
    push . get n
    set n exec .--func dec
    push . space
    push . get n
    strap exec .--func repeat
    strap string /----/
    push . space
    push . get m
    push . get n
    push . exec .--func sub
    push . exec .--func dec
    push . number 2
    push . exec .--func mul
    strap exec .--func repeat
    strap string \
    strap newline
    push . get n
    push . number 0
    if exec .--func more
        goto loop

push . strap
push . regex gm string   ( {4})(?=.$)
push . string  \$1
print exec .--func replace
\$\endgroup\$
6
  • 1
    \$\begingroup\$ "I have officially made the most verbose language possible." Excuse me, have you heard of AppleScript? \$\endgroup\$ Commented Jul 20, 2016 at 23:53
  • \$\begingroup\$ @VTCAKAVSMoACE Alright, let's make a deal. You answer this question in applescript. Then I'll tell you this is more verbose. \$\endgroup\$ Commented Jul 21, 2016 at 1:53
  • 4
    \$\begingroup\$ It's not verbose enough if you can ungolf it. \$\endgroup\$
    – Neil
    Commented Jul 21, 2016 at 8:30
  • \$\begingroup\$ @VTC, TimmyD, and Neil: Geez. You happy now? \$\endgroup\$ Commented Jul 21, 2016 at 19:31
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ We're obviously joking around, lol \$\endgroup\$ Commented Jul 21, 2016 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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