21
1
Input:
Two strings (NOTE: the order of input is important).
Output:
Both words/sentences start on lines with one empty line in between them. They 'walk' horizontally 'next to each other'. But when they have the same character at the same position, they cross each other, and then continue walking 'next to each other'.
Confusing you say? Let's give an example:
Input: Words crossing over
& Ducks quacking
:
Word quack n
s i g
Duck cross n over
Challenge rules:
- We always first go back walking a straight line after we've 'crossed over' before we can cross over again (see test case above {1} - where
ing
is equal, but after we've crossed oni
, we first have to go back walking straight (thus ignoringn
), before we can cross again ong
). - The inputs can be of different length, in which case the longer one continues walking in a straight line (see test cases 1, 2, 4 & 6).
- Both inputs can be the same (see test case 3).
- The inputs won't contain any tabs nor new-lines.
- Spaces are ignored as characters that are the same (as an edge case), in which case the next (non-space) character after that - if any - is crossing over instead (see test cases 3, 5 & 6).
- The inputs can have no adjacent (non-space) characters on the same position at all, in which case both just walk in a straight line horizontally (see test cases 2).
- Even if the first character is equal, we always start two lines apart (see test cases 3 & 6).
- Trailing spaces and a single trailing new-line are optional.
- You can assume the inputs will only contain printable ASCII characters (new-lines and tabs excluded).
- The inputs are case-sensitive, so
A
anda
aren't equal, and won't cross over (see test case 7). - Both the inputs lengths will always be at least 2.
- Input & output can be in any reasonable format. Can be a single String with new-lines; a String-array/list; printed to STDOUT; 2D array of characters; etc.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code.
- Also, please add an explanation if necessary.
Test cases:
1. Input: "Words crossing over" & "Ducks quacking"
1. Output:
Word quack n
s i g
Duck cross n over
2. Input: "bananas" & "ananas"
2. Output:
bananas
ananas
3. Input: "I see my twin!" & "I see my twin!"
3. Output:
I e y w n
s e m t i !
I e y w n
4. Input: "Is the weather nice?" & "Not really, no.."
4. Output:
Is th ally, no..
e
Not r weather nice?
5. Input: "Codegolf & Programming Puzzles" & "The golfer might solve puzzles"
5. Output:
Code o f & Programming P z l s
g l u z e
The o fer might solve p z l s
6. Input: "Can you turn the lights off?" & "Can you try to solve this?"
6. Output:
C n o urn the ve s off?
a y u t l t
C n o ry to so igh his?
7. Input: "one Ampere" & "two apples"
7. Output:
one Am les
p
two ap ere
8. Input: "Words crossing" & "Words Quacking"
8. Output:
W r s cross n
o d i g
W r s Quack n
If the strings start with 3 identical letters, this intersects the third letters, not the second ones. I am unsure if this is the correct result, I have asked the OP. – Mr. Xcoder – 2017-08-11T09:39:10.167
@Mr.Xcoder Thanks. Should be fixed now. – Adám – 2017-08-11T09:44:59.587
Ok, nice solution then. If you have time, maybe you can add an explanation :P – Mr. Xcoder – 2017-08-11T09:47:48.600
@Mr.Xcoder Yes, I always do. (Ping me if you see any unexplained answer of mine!) – Adám – 2017-08-11T09:48:28.933
That's why I am always delighted to see your answers :) - Because I understand your submissions more than I understand mine. – Mr. Xcoder – 2017-08-11T09:49:25.100
@Mr.Xcoder Thank you. I appreciate your words. – Adám – 2017-08-11T09:50:14.557
1
@Adám Yeah surely...or maybe? Might have something to do with this as well...oh and an unexplained answer! Or maybe two...? And something I don't quite get.
– Erik the Outgolfer – 2017-08-11T09:51:55.107@EriktheOutgolfer Thanks. Done. – Adám – 2017-08-11T09:53:53.020
@EriktheOutgolfer All done. Thanks. – Adám – 2017-08-11T11:10:28.353