31
3
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
add or remove as many trailing spaces as you would like.
add a maximum of a single trailing newline.
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!
When you say the cursor has to start at
(0,0)
do you mean we need to clear the console first, or do you just mean the default position of the cursor by that? – Martin Ender – 2015-09-21T06:26:58.09718I'm voting to close this question as off-topic because it is full of hatred and blasphemy. – aditsu quit because SE is EVIL – 2015-09-21T06:53:06.073
1Your animation looks so much like the ><> interpreter that I now want to see a self-modifying ><> entry. – Sanchises – 2015-09-21T08:48:30.463
1I liked the hidden message in the opening paragraph but I have to disagree. – wf4 – 2015-09-21T14:41:47.307
@MartinBüttner That just means the default position. – Doorknob – 2015-09-21T16:35:30.097
What are the requirements if the cursor is going backwards or upwards and the bit of text is longer than there is space for before the first row or column is reached. For example the string
this<tab>is a<tab>problematic<tab>string!!
would not only go into negative columns, but also negative rows. – Tom Carpenter – 2015-09-21T21:25:07.193@TomCarpenter I already addressed that at the end of the Challenge section: "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)." – Doorknob – 2015-09-21T21:26:16.227
@Doorknob I missed that bit. ;) – Tom Carpenter – 2015-09-21T21:26:35.390