# Draw my downslashes

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
\
/
\
/
\
/


# V, 14 bytes

\ÓçÜ/j>G
ç¯/<G


Try it online!

# Perl, 34 38 + 1 bytes

to handle the two cases

s,(/)|.,$"x($1?$c&&--$c:$c++).$&.$/,ge  to be run with -p option s,(/)|.,$"x($1?--$c:$c++).$&.$/,ge  EDIT: following comment doesn't work when first character is / s,(/)|.,$"x($1?$c--:++$c).$&.$/,ge  however output will be shifted one character on the right if first char is \ • Doesn't work for /\\/\\/. – Neil Sep 14 '17 at 9:59 • With the updated question your original 34 solution is now perfectly valid – Ton Hospel Feb 8 '18 at 19:35 VBA (Excel), 181 bytes Sub q() a = Cells(1, 1) For x = 1 To Len(a) c = Mid(a, x, 1) If c = "\" Then: Debug.Print b & c: b = b + " " If c = "/" Then: b = Left(b, Len(b) - 1): Debug.Print b & c Next End Sub  • You can golf this down significantly without changing your algorithm by taking advantage of the autoformatting nature of Excel VBA and by use of [...] notation: I got it down to 128 Bytes Sub q For x=1To[Len(A1)] c=Mid([A1],x,1) If c="\"Then Debug.?b;c:b=b+" " If c="/"Then b=Left(b,Len(b)-1):Debug.?b;c Next End Sub – Taylor Scott Sep 15 '17 at 17:50 • Thank you for golfing my script. I learned something from this and will apply in the future. :) Didn't know I can use that to fetch data directly to the cells. Thanks again :) – remoel Sep 18 '17 at 4:42 # Pyth, 24 21 bytes Port from Rod's answer. VQ=-Z<N\<+*ZdN=+Z>N\<  Try it online! • Invalid, prints [extra leading space on the first line if the string starts with \. BTW where is your code? – Mr. Xcoder Sep 14 '17 at 12:59 • @Mr.Xcoder fixed – Felipe Nardi Batista Sep 14 '17 at 13:05 # SOGL V0.12, 16 13 bytes {└=-:↕PFoF┘=+  Try it Here! - expects input on the stack, so for ease-of-use , is added # 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 \ \ \ / / \ \ / \ / / \ \ / / /  # 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. # 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! # brainfuck, 151 bytes >->++++[>++++++++<-]>>+[[-]>[-]>,[<+<+>>-]<<[>++++>-[<->-----]+<[<<<<+[>+<-]>[<+>>.<-]>>>>-]>[<<<<<[>+<-]>[<+>>.<-]->>>>->]<<<.>>>++++++++++.[-]>]<<<<]  Try it online! # J, 2827 22 bytes ({."0~+/\-0&<)_1^'\'=]  Try it online! Thanks to Bubbler for catching a bug and for -6 bytes! • I think first column is truncated? Fixing it and using _1^ gives 27 bytes. – Bubbler Sep 7 '20 at 6:41 • Thanks. The _1 trick is more elegant anyway. – Jonah Sep 7 '20 at 6:44 • I got it down to 22 bytes. – Bubbler Sep 7 '20 at 6:57 • There is golfed... and then there is Bubbler-ed! – Jonah Sep 7 '20 at 7:08 # C (gcc), 9671 70 bytes Awesome -25 solution by ceilingcat And another -1 by ceilingcat. ASCII bit hacks ftw c;p;main(i){for(;read(0,&c,1);printf("%*c\n",i,p=c))c-p?:c&1?--i:++i;}  Try it online! Ungolfed c; /* character */ p; /* previous character */ main(i /* indent */) { for (; read(0, &c, 1); printf("%*c\n", i, p = c)) c - p ? : c & 1 ? --i : ++i; }  Explanation read takes 1 byte from the file descriptor 0 (stdin) and stores it in c. c - p checks if c and p are different. c & 1 checks if c is a forward slash. So the entire conditional increments the indent if the character and the previous character are both backslashes, and decrements the indent if they're both forward slashes. Then we print c with i leading spaces. We also assign p = c for the next iteration. • @ceilingcat Thanks for the great solutions :) – willmcpherson2 Sep 7 '20 at 5:31 # PowerShell, 47 bytes switch($args){'\'{' '*$i+++$_}'/'{' '*--$i+$_}}


Try it online!

# PowerShell, 71 69 bytes

-2 bytes thanks to mazzy

$z=92$args|%{$i+=($x=2*($_-eq92)-1)*($z-ne$_) ' '*$i+($z=$_)
$i+=$x}


Try it online!

Takes input by splatting. This also saves us two bytes because we can just treat it as an int instead of writing '\' which we do twice.

• nice. ($x=2*($_-eq92)-1) ? – mazzy Sep 9 '20 at 6:51

# Zsh, 43 bytes

for c (fold -1)echo ${(l/#c%2?w--:++w/)c}  Try it online! Explanation: • fold -1: line-wrap the input to a width of 1 (i.e., insert a line-break between every character) • for c (): for each character $c:
• ${c}: take the character, • (l//): left-padded with spaces to the width of: • #c: the character code of $c
• %2: modulo 2 (this is 1 for / and 0 for \)
• ?: if 1 then:
• w--: decrement $w (and return the original value) • :: else: • ++w: increment $w (and return the mutated value)
• note that $w starts at 0 implicitly • echo: print; we can't use the 2 bytes shorter <<< since its argument is evaluated in a sub-shell so the mutation of $w has no effect

# Pyth, 20 bytes

jm?qd\\+*;~hZd+*;=tZ


Test suite

The other two existing Pyth answers use different approaches (and an older version of the language).

##### Explanation:
jm?qd\\+*;~hZd+*;=tZ   | Full program
jm?qd\\+*;~hZd+*;=tZdQ | with implicit variables added
-----------------------+------------------------------------------------
m                   Q | For each character d in the input:,
?qd\\                |  if d is '\',
*;             |   create a string of n spaces, where n is
~hZ          |    Z, but then store Z + 1 in Z (Z begins as 0)
+     d         |   add d to the end of the string
?                    |  otherwise,
+*;   d  |   do the same, but with (below) spaces
=tZ   |    Z - 1, stored in Z
j                      | Join the results by newline.

• The test suite's input is missing a trailing ". – Jonathan Allan May 30 at 22:37
• @JonathanAllan thanks for the heads up – hakr14 May 31 at 0:03

# Python 2, 8676 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. >_>

# 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!

### c64 basic v2, 7170 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.

# 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.

# SmileBASIC, 65 bytes

INPUT S$WHILE""<S$C$=SHIFT(S$)DEC N,@L>C$?" "*N;C$INC N,@H<C$WEND  # 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> # 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))
(                       )     / 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


# 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


# Pip, 28 bytes

FlaIlQ'\PsX++v.lE{PsXv.l--v}


With a post decrement operator, this could be way shorter.

Try it online!

# Rockstar, 110 bytes

listen to S
X's0
Y's-1
while S at X
let C be S at X
let Y be+C is "\"
say " "*Y+C
let Y be-C is "/"
let X be+1


Try it here (Code will need to be pasted in)

# Excel, 125 bytes

=LET(q,SEQUENCE(LEN(A1)),x,MID(A1,q,1),REPT(" ",MMULT(IF(TRANSPOSE(q)>q,,1),IF(q=1,,(INDEX(x,q-1)="\")-(INDEX(x,q)="/"))))&x)


• =LET(q,SEQUENCE(LEN(A1)), q = vertical array [1..LEN(A1)]
• x,MID(A1,q,1), x = The characters in A1 in a vertical array
• IF(q=1,,(INDEX(x,q-1)="\")-(INDEX(x,q)="/")) the change in the number of spaces from the prior line; +1 if the prior line was "", -1 if the current line is "/"
• IF(TRANSPOSE(q)>q,,1) a lower triangular matrix of 1s
• MMULT(~,~) matrix multiply the change array and the lower triangular matrix to get the number of spaces for each line
• REPT(" ",~)&x) for each line output the number of spaces & characters