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

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

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

# Implicit, 25 bytes

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

Try it online!

Ungolfed/explanation:

(          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
}

)          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 at 6:41
• Thanks. The _1 trick is more elegant anyway. – Jonah Sep 7 at 6:44
• I got it down to 22 bytes. – Bubbler Sep 7 at 6:57
• There is golfed... and then there is Bubbler-ed! – Jonah Sep 7 at 7:08

# C (gcc), 9671 70 bytes

Awesome -25 solution by ceilingcat

And another -1 by ceilingcat. ASCII bit hacks ftw

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 at 5:31

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$_)

# 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:"\\\\\\//\\\\/\\//\\\\///"
\
\
\
/
/
\
\
/
\
/
/
\
\
/
/
/

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

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