Holy wars have been fought over spaces vs. tabs. (And of course spaces, being
objectively superior, won.) —Alex A.
Some people still refuse to accept that
which is clearly supreme. You've just received
a file using the incorrect, bad, and inferior form
of whitespace, and now the contents of the file are
tainted and ruined.
You decide you might as well show the person who sent the file to you just how
wrong they are—violently.
Description
As the title suggests, your challenge is to take a file that contains one or
more tabs:
this is an evil tab onoes
and ruthlessly shatter them into pieces:
this is an evil tab
o
n
o
e
s
Note that the Stack Exchange software turns literal tabs into four spaces
(because it's right), so tabs within this post will be displayed as four
spaces. The input to your program, however, will contain actual tabs.
Challenge
The solution should take a single string as input, which may contain printable
ASCII, newlines, and tabs. There will always be at least a single tab in the
input.
The output should be the same string, with the following rules applied:
Start the cursor at coordinates (0,0) and with a direction of right. The
coordinates are (column,row), zero-indexed, and the direction is which way
you should move the cursor after printing a character.
For each character in the string:
If it's a newline, move to coordinates (0,n), where n is the number of
newlines in the string so far (including this one), and reset the
direction to right.
If it's a tab, output two spaces, rotate the cursor direction 90 degrees
clockwise, and output two more spaces, effectively "breaking" the tab in
half. Here's a visual example, where a tab is represented as ---> and
spaces as ·:
foo--->bar--->baz
becomes
foo···
·
b
a
r
·
·
zab··
Otherwise, simply output the character at the cursor and move the cursor
one step in the current direction.
Since you are reading the string from start to end, it is possible that you
will have to write "on top" of existing characters—this is okay. For example,
the input
foo--->bar
spaces are superior
should result in an output of
foo
b
spaces are superior
r
You may choose whether "broken tabs" should overwrite other characters—the original intention was that they do not, but the spec was ambiguous, so this is your decision.
Furthermore, after applying these rules, you may also
The input will never contain trailing spaces; it will also never contain
leading or trailing newlines. You may also always assume that you will never
need to write to a column or a row less than 0 (i.e. off the screen).
Test case
Tabs in this test case are represented as ---> because otherwise SE gobbles
them up.
Input:
Test case. Here's a tab--->there's a tab--->everywhere a tab--->tab--->this is some more text
blah
blah
blah blah blah blah blah blah--->blaah--->blaah--->blah--->blaaaaah--->blah--->blah--->blah--->blah--->blah
Output:
Test case. Here's a tab
blah
blah t
blah blah blah blah blah blah
blaablah
r b
e l b
h 'h a l
a sa a a
l l h h
this is some mobe tbxt
haalhalb
b a
a b
t
bat a erehwyreve
Fancy animation:
Rules
- This is code-golf, so the shortest code in bytes will win!
if !dandif d>2\$\endgroup\$!dis not valid syntax. @sanchises Thanks for thed>2tip, though. \$\endgroup\$d==0->d<1\$\endgroup\$