Skip to main content
Down to 84 bytes
Source Link

Lua, 8888 84 bytes

Improved version (-4 bytes thanks to QuertyKeyboard)

s=""g=s.gsub g(...,".",function(c)s=g(g(g(s,"\\"," "),"/?$",c)," /","/")print(s)end)

Try it online!

Original version (88 bytes)

Another attempt in Lua, this time with a completely different approach using string manipulation instead of a counter variable.

  
s=""for c in(...):gmatch"."do s=s:gsub("\\"," "):gsub("/?$",c):gsub(" /","/")print(s)end

Try it online!

Ungolfed:

s = ""
for c in string.gmatch((...), ".") do --for each character in the input
  --s contains the output from the previous iteration
  s = s:gsub("\\\\", " ") --Replace backslash with space -> indent by 1
  s = s:gsub("/?$", c) --Remove any / at the end of the string and append c to the string
  s = s:gsub(" /", "/") --Remove a single space in front of any / -> un-indent by 1
  print(s)
end

There's one interesting thing in the code: (...):gmatch"."
This uses some quirks in the Lua parser. When Lua encounters a piece of code in the form func "string", it'll convert this to func("string"). This is so that one can write print "string" to print a constant string and it only works with a single string literal after the function. Anything else will give a syntax error. However, this syntactic sugar also works with function calls in the middle of an expression, and more surprising, it works just fine together with the : method call syntactic sugar. So in the end, Lua will interpret the code like this:

(...):gmatch"."
-> (...):gmatch(".")
-> string.gmatch((...), ".")

If anyone can think of a way to remove one of the three gsub calls, please tell me.If anyone can think of a way to remove one of the three gsub calls, please tell me.

Lua, 88 bytes

Another attempt in Lua, this time with a completely different approach using string manipulation instead of a counter variable.

 
s=""for c in(...):gmatch"."do s=s:gsub("\\"," "):gsub("/?$",c):gsub(" /","/")print(s)end

Try it online!

Ungolfed:

s = ""
for c in string.gmatch((...), ".") do --for each character in the input
  --s contains the output from the previous iteration
  s = s:gsub("\\\\", " ") --Replace backslash with space -> indent by 1
  s = s:gsub("/?$", c) --Remove any / at the end of the string and append c to the string
  s = s:gsub(" /", "/") --Remove a single space in front of any / -> un-indent by 1
  print(s)
end

There's one interesting thing in the code: (...):gmatch"."
This uses some quirks in the Lua parser. When Lua encounters a piece of code in the form func "string", it'll convert this to func("string"). This is so that one can write print "string" to print a constant string and it only works with a single string literal after the function. Anything else will give a syntax error. However, this syntactic sugar also works with function calls in the middle of an expression, and more surprising, it works just fine together with the : method call syntactic sugar. So in the end, Lua will interpret the code like this:

(...):gmatch"."
-> (...):gmatch(".")
-> string.gmatch((...), ".")

If anyone can think of a way to remove one of the three gsub calls, please tell me.

Lua, 88 84 bytes

Improved version (-4 bytes thanks to QuertyKeyboard)

s=""g=s.gsub g(...,".",function(c)s=g(g(g(s,"\\"," "),"/?$",c)," /","/")print(s)end)

Try it online!

Original version (88 bytes)

Another attempt in Lua, this time with a completely different approach using string manipulation instead of a counter variable.

 
s=""for c in(...):gmatch"."do s=s:gsub("\\"," "):gsub("/?$",c):gsub(" /","/")print(s)end

Ungolfed:

s = ""
for c in string.gmatch((...), ".") do --for each character in the input
  --s contains the output from the previous iteration
  s = s:gsub("\\\\", " ") --Replace backslash with space -> indent by 1
  s = s:gsub("/?$", c) --Remove any / at the end of the string and append c to the string
  s = s:gsub(" /", "/") --Remove a single space in front of any / -> un-indent by 1
  print(s)
end

There's one interesting thing in the code: (...):gmatch"."
This uses some quirks in the Lua parser. When Lua encounters a piece of code in the form func "string", it'll convert this to func("string"). This is so that one can write print "string" to print a constant string and it only works with a single string literal after the function. Anything else will give a syntax error. However, this syntactic sugar also works with function calls in the middle of an expression, and more surprising, it works just fine together with the : method call syntactic sugar. So in the end, Lua will interpret the code like this:

(...):gmatch"."
-> (...):gmatch(".")
-> string.gmatch((...), ".")

If anyone can think of a way to remove one of the three gsub calls, please tell me.

Source Link

Lua, 88 bytes

Another attempt in Lua, this time with a completely different approach using string manipulation instead of a counter variable.

s=""for c in(...):gmatch"."do s=s:gsub("\\"," "):gsub("/?$",c):gsub(" /","/")print(s)end

Try it online!

Ungolfed:

s = ""
for c in string.gmatch((...), ".") do --for each character in the input
  --s contains the output from the previous iteration
  s = s:gsub("\\\\", " ") --Replace backslash with space -> indent by 1
  s = s:gsub("/?$", c) --Remove any / at the end of the string and append c to the string
  s = s:gsub(" /", "/") --Remove a single space in front of any / -> un-indent by 1
  print(s)
end

There's one interesting thing in the code: (...):gmatch"."
This uses some quirks in the Lua parser. When Lua encounters a piece of code in the form func "string", it'll convert this to func("string"). This is so that one can write print "string" to print a constant string and it only works with a single string literal after the function. Anything else will give a syntax error. However, this syntactic sugar also works with function calls in the middle of an expression, and more surprising, it works just fine together with the : method call syntactic sugar. So in the end, Lua will interpret the code like this:

(...):gmatch"."
-> (...):gmatch(".")
-> string.gmatch((...), ".")

If anyone can think of a way to remove one of the three gsub calls, please tell me.