60
\$\begingroup\$

As a programmer, you've probably heard of forward slashes and backward slashes. But have you heard of downslashes? That's when you take a bunch of slashes, connect their ends and draw them going down.

For today's challenge, you must write a program or function that takes a string consisting purely of slashes, and outputs all of those slashes drawn downwards in a line connecting them. This will be a lot more clear if you see an example. Given the string \\\//\/\\, you should output:

\
 \
  \
  /
 /
 \
 /
 \
  \

Here are some clarifications:

  • There must be one slash per line.

  • The first line will have 0 leading spaces.

  • For each pair of slashes:

    • If they are different from each other, they will be drawn in the same column. For example, \/ will give:

      \
      /
      
    • If they are the same character, the lower one is in the direction pointed to, that is moving to the right for a backslash, and moving to the left for a forward slash. So \\// will give

      \
       \
       /
      /
      
  • Each line may have extra trailing whitespace as long as this doesn't change the visual appearance of the output. Up to one trailing and leading newline is also acceptable. Extra leading spaces are not permitted!

In order to keep this simpler, you can assume that the string will never contain too many forward slashes. In other words, no prefix of the input will contain more forward slashes than backslashes, so an input like \\//// or // will never be given. This also means that every input will start with a backslash.

If your input is taken as a string literal, you may escape the backslashes if this is necessary. You will also never need to handle an input that is empty, or contains characters other than a slash.

You may output by any reasonable format.

As usual, this is a challenge, so try to make the shortest solution possible, even if you pick a language where this is rather difficult. Bonus points for explaining any interesting techniques you used to take bytes off!

Examples

#Input
\\\\\\\\\\\

#Output
\
 \
  \
   \
    \
     \
      \
       \
        \
         \
          \

#Input
\\\//\\/\//\\///

#Output
\
 \
  \
  /
 /
 \
  \
  /
  \
  /
 /
 \
  \
  /
 /
/


#Input
\/\/\/

#Output
\
/
\
/
\
/
\$\endgroup\$

44 Answers 44

1
\$\begingroup\$

QBasic, 79 bytes

DO
c$=INPUT$(1)
IF"/">c$THEN END
c=2*ASC(c$)-139
i=i+c+p
?SPC(i\90);c$
p=c
LOOP

This program prints the slashes as you type them. To quit, press Enter (or anything earlier in the ASCII table than /).

Downslashes program in action

Explanation

With the variables i and p implicitly initialized to 0, we enter an infinite loop. INPUT$(1) reads one character from keyboard input. If this character is less than forward slash, END the program. Otherwise, we calculate c such that an input of / gives c=-45 and \ gives c=45. We then add c and p (the previous value) to i (the indent).

If p and c are the same, they add together to change i by either -90 or +90. If they are different, they cancel out and do not change i. We then print i\90 spaces followed by the character. (We use integer division because the value of i is actually going to be 45, 135, etc., since p is 0 on the first iteration instead of -45.)

Finally, set p=c for the next iteration, and loop.

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

Implicit, 25 bytes

~.(´?`/ò](0ß´);@ß1?`\ó~.ö

Try it online!

Ungolfed/explanation:

~.         read input, increment
(          do..while top of stack truthy
 ´         decrement

 ?`/{      if input equal to /
   ]´[      decrement memory
 }

 ](0       pull memory, do..while truthy
  @32       print a space
  ´         decrement
 );        pop

 @@10      print input, print a newline

 ?`\{      if input equal to \
   ].[      increment memory
 }

 ~.        read input, increment
)          loop
&          exit (avoid implicit output)

Note that ß is equal to @32, ò is equal to ]´[, ó is equal to ].[, and ö can be used in place of )& when at the end of a program.

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

Perl, 33 bytes

Includes +1 for the n option

perl -nE 's%.%say$"x(l^$&?--$a:$a++).$&%eg' <<< '\\\//\/\\'

Where it not for the fact that -F counts as +3 this would be 32:

echo -n '\\\//\/\\' | perl -F -E 'say$"x(l^$_?--$a:$a++).$_ for@F'
\$\endgroup\$
1
\$\begingroup\$

Python 3, 78 bytes

a,x='',0
for i in input():x+=((1*i!='/')-(1*i=='/'))*(a==i);print(' '*x+i);a=i

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 2, 86 76 75 bytes

-1 byte thanks to Neil.

p,i='',0
for c in input():i+={'\\\\':1,'//':-1}.get(p+c,0);print' '*i+c;p=c

Try it online!

Fairly lame approach, I will try to golf the mapping at least. NVM I got rekt by another answer. >_>

\$\endgroup\$
0
\$\begingroup\$

Rust, 165 bytes

fn f(s:&str){let(_,a)=s.chars().fold((0,"".to_string()),|(i,r),c|(if c<'<'{i-1}else{i+1},format!("{}{}{}\n",r," ".repeat(if c<'<'{i-1}else{i}),c)));println!("{}",a)}

Try it online!

It's an adaptation of Rod's answer in Python 2.

\$\endgroup\$
0
\$\begingroup\$

V, 14 bytes

\/j>G
ç¯/<G

Try it online!

\$\endgroup\$
0
\$\begingroup\$

c64 basic v2, 71 70 61 bytes

1rEs:x=1024:fOy=1tolE(s):a=aS(mI(s,y,1)):pOx,a:x=x-165+a*2:nE

Input can be given as

0dA"\\/\/\\/\//"

If the string is longer as 25, the program will overwrite its own memory. And we won't do that, because basic programs overwriting themselves with POKEs contradict the OO paradigm.

\$\endgroup\$
0
\$\begingroup\$

Excel VBA, 74 72 Bytes

Anonymous VBE immediate window function that takes input from cell [A1] and outputs to the VBE immediate window

For i=1To[Len(A1)]:c=Mid([A1],i,1):s=s+(c="/"):?Spc(s)c:s=s-(c="\"):Next

Example I/O

[A1]="\\\//\\/\//\\///"
For i=1To[Len(A1)]:c=Mid([A1],i,1):s=s+(c="/"):?Space(s)c:s=s-(c="\"):Next
\
 \
  \
  /
 /
 \
  \
  /
  \
  /
 /
 \
  \
  /
 /
/
\$\endgroup\$
0
\$\begingroup\$

GolfScript - 117 bytes

1/{'/'=}%1:s;[{s}{..(\;!?.0<{;,0:s;}{.@\>}if}while]0:M;1:i;{,{i*}%:a{M+' '*i 0<{'/'}{"\\"}if +"\n"+}/a)M+:M;;i~):i;}/

Ungolfed version

1/{'/'=}%
1:s;
[{s}
{
  ..(\;!?.0<{;,0:s;}{.@\>}if
}
while]
0:M;
1:i;
{
  ,{i*}%:a{M+' '*i 0<{'/'}{"\\"}if +"\n"+}/
  a)M+:M;;
  i~):i;
}/

At first glance it looked easy... it took me 2.5 hours. Probably there is room for improvement, perhaps tomorrow I'll do some optimization.

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 65 bytes

INPUT S$WHILE""<S$C$=SHIFT(S$)DEC N,@L>C$?" "*N;C$INC N,@H<C$WEND
\$\endgroup\$
0
\$\begingroup\$

JavaScript, 49 48 bytes

Takes input as an array of individual characters and outputs an array of lines.

a=>a.map(c=>" ".repeat(x+=d=c>'/',x-=!d)+c,x=-1)

Saved a byte borrowing a trick from the other JS solution.


Try it

f=
a=>a.map(c=>" ".repeat(x+=d=c>'/',x-=!d)+c,x=-1)
o.innerText=[String.raw`\\\\\\\\\\\\`,String.raw`\\\//\\/\//\\///`,String.raw`\/\/\/`].map(s=>f([...s]).join`\n`).join`\n\n`
oninput=_=>o.innerText=f([...i.value]).join`\n`
<input id=i><pre id=o>

\$\endgroup\$
0
\$\begingroup\$

K4, 31 bytes

Solution:

-1(-1+\a-a*~~':a:-1 1"/"=x)$$x:

Examples:

q)k)-1(-1+\a-a*~~':a:-1 1"/"=x)$$x:"\\\\\\//\\\\/\\//\\\\///"
\
 \
  \
  /
 /
 \
  \
  /
  \
  /
 /
 \
  \
  /
 /
/

Works if you start with a / too:

q)k)-1(-1+\a-a*~~':a:-1 1"/"=x)$$x:"/\\\\/\\\\/"
/
\
 \
 /
 \
  \
  /

Explanation:

Generate list of negative numbers to (left) pad each of the input characters. Struggled to get a formula that worked, will look at the other solutions to see if there is a more simple method:

q)k)"/"=x
0001100101100111b
q)k)-1 1"/"=x
-1 -1 -1 1 1 -1 -1 1 -1 1 1 -1 -1 1 1 1
q)k)~~':a:-1 1"/"=x
1001010111010100b
q)k)a*~~':a:-1 1"/"=x
-1 0 0 1 0 -1 0 1 -1 1 0 -1 0 1 0 0
q)k)a-a*~~':a:-1 1"/"=x
0 -1 -1 0 1 0 -1 0 0 0 1 0 -1 0 1 1
q)k)+\a-a*~~':a:-1 1"/"=x
0 -1 -2 -2 -1 -1 -2 -2 -2 -2 -1 -1 -2 -2 -1 0
q)k)-1+\a-a*~~':a:-1 1"/"=x
-1 -2 -3 -3 -2 -2 -3 -3 -3 -3 -2 -2 -3 -3 -2 -1

Full breakdown:

-1(-1+\a-a*~~':a:-1 1"/"=x)$$x: / the solution
                             x: / save input as variable x
                            $   / string (converts to char lists))
                           $    / pad
  (                       )     / do this together
                     "/"=x      / boolean list of x equal to /
                 -1 1           / index into this list
               a:               / save as variable a
           ~~':                 / differ (not-not-each-previous)
         a*                     / multiply by a
       a-                       / subtract from a
     +\                         / sums
   -1                           / (start summing from -1)
-1                              / print to stdout
\$\endgroup\$
0
\$\begingroup\$

Japt -R, 13 bytes

¬®ùZ>'/?°V:V´

Try it online!

Unpacked & How it works

Uq mZ{ZùZ>'/?++V:V--

Input: U = string of / and \, V = 0 (default)

Uq                    Split into chars
   mZ{                Map...
        Z>'/?           If the char is rightward (backslash)...
             ++V          Pre-increment V
                :V--      Otherwise, post-decrement V
      Zù                Left-pad the char with spaces to length V

-R                    Join with newline before printing
\$\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.