29
1
Challenge:
Input:
You take two inputs:
- A string only containing printable ASCII (excluding spaces, tabs or new-lines)
- A printable ASCII character
Output:
The first line will contain the string-input. Every i
-modulo-3 first occurrence of this character will move in a South-East direction; every i
-modulo-3 second occurrence will move in a South direction; and every i
-modulo-3 third occurrence will move in a South-West direction. You'll continue until the characters are about to be at their initial starting position again (which means it will wrap around from one side to the other if necessary), and then you'll print the last line with the string-input again to finish it. (Note that all test cases will end up at their initial input after at most length(input)
rows, including the row containing the trailing input. It can be sooner though, as seen in this first test case below, with a length of 14, but ending after 9.)
This may all be pretty vague, so here an example:
Test case 1:
String-input: "This_is_a_test"
Character-input: 's'
Output:
This_is_a_test
s s s
ss s
s s
sss
sss
s s
ss s
s s s
This_is_a_test
Here is the same test case with the colored paths of the three s
:
where the first 's'
follows the green path in a South-East direction; the second 's'
follows the yellow path in a South direction; and the third 's'
follows the light blue path in a South-West direction. (If there would be a fourth 's'
it would go in a South-East direction again, which can be seen in some of the other test cases below.)
Challenge rules:
- The inputs will only contain printable ASCII (excluding spaces, tabs and new-lines)
- I/O formats are flexible. Can be a new-line delimited string, character matrix, etc. Your call.
- It is possible that the given character isn't present in the string, in which case you are allowed to either output the input-string once or twice (i.e.
"test", 'a'
can have either of these as possible output:"test\ntest"
/"test"
). - Leading spaces are mandatory; trailing spaces are optional. One or multiple leading/trailing new-lines are allowed.
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 / more examples:
Test case 2:
String-input: "abcabcabcabcabc"
Character-input: 'b'
Output:
abcabcabcabcabc
b b b b b
bbb bb
b b
bbb bb
b b b b b
b b b b
b b b b b
bb b bb
b b b
bb bbb
b b bbb
b b b b
b bb bb
b b bb b
abcabcabcabcabc
Here is the same test case with the colored paths of the five a
:
Test case 3:
String-input: "only_two_paths?"
Character-input: 'o'
Output:
only_two_paths?
o o
o o
o o
o o
o o
oo
o
oo
o o
o o
o o
o o
o o
o o
only_two_paths?
Here is the same test case with the colored paths of the two o
:
Test case 4:
String-input: "lollollollollol"
Character input: 'l'
Output:
lollollollollol
lll ll ll
ll ll ll
l ll ll ll ll
lll l ll l ll
llllll ll ll
l l ll ll
ll lll ll
l l l lll ll l
ll l ll l l
l l l l llll l
ll lll lll
l l l ll
ll lll lllll
l l l ll l ll
lollollollollol
Here is the same test case with the colored paths of the ten l
:
Test case 5:
String-input: "AbCdEeDcBaAbCdEeDcBa_CCCCC"
Character input: 'C'
Output:
AbCdEeDcBaAbCdEeDcBa_CCCCC
C C C C C
C C C C CCC
C C C C C C C
C C C C C C C
C C C C C C C
C C C C C C C
C C C C C C C
C C C CC C C
C C CC C C
C C CC C C
C C CC C
CC CC C C
CC CC C C
C C CC C C
C C CC C C C
C C C C C C
C C CC C C C
C C C C C C C
C C C C C C C
C C C C C CC
C C C C C C
C C C C CCC
C C C CCCC
C C C C
C C CCCCC
AbCdEeDcBaAbCdEeDcBa_CCCCC
Here is the same test case with the colored paths of the seven C
:
Test case 6:
String-input: "XyX"
Character input: 'X'
Output:
XyX
XX
X
XyX
Here is the same test case with the colored paths of the two X
:
Test case 7:
String-input: "aaaa"
Character input: 'a'
Output:
aaaa
aa
aaa
aaa
aaaa
Here is the same test case with the colored paths of the four a
:
So when the string contains a multiple of 3 of the character, the output might have fewer rows than the length of the string? – Neil – 2018-03-07T10:53:47.320
@Neil I'm not sure if it's a multiple of 3, haven't investigated it that much. All I know is that at most
length(input)
everything matches up again, but it can be sooner as the first test case proves. But it seems you're indeed right about the multiple of 3 part (although I'm not 100% sure). – Kevin Cruijssen – 2018-03-07T11:05:40.453@Neil any string with the character appearing only 3 times in a row will print 3 lines however long it is – Asone Tuhid – 2018-03-07T12:24:34.620
Are trailing new lines allowed between 2 printed lines? – Asone Tuhid – 2018-03-07T13:27:50.710
@AsoneTuhid Sorry but no. I don't care about any amount of trailing or leading new-lines before or after the entire expected output, but I won't allow in between lines. The diagonal lines wouldn't be correct anymore as well.. – Kevin Cruijssen – 2018-03-07T14:32:19.800
I would agree, was just asking to make sure. – Asone Tuhid – 2018-03-07T14:57:58.287