[Question]
  [
## 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`:
[![enter image description here](https://i.stack.imgur.com/6a0Qs.png)](https://i.stack.imgur.com/6a0Qs.png)
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](/questions/tagged/code-golf "show questions tagged '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](https://codegolf.meta.stackexchange.com/questions/2419/default-for-code-golf-program-function-or-snippet/2422#2422) 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](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) 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`:
[![enter image description here](https://i.stack.imgur.com/aZg8n.png)](https://i.stack.imgur.com/aZg8n.png)
### 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`:
[![enter image description here](https://i.stack.imgur.com/6h7rz.png)](https://i.stack.imgur.com/6h7rz.png)
### 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`:
[![enter image description here](https://i.stack.imgur.com/Fq2QV.png)](https://i.stack.imgur.com/Fq2QV.png)
### 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`:
[![enter image description here](https://i.stack.imgur.com/FJBWl.png)](https://i.stack.imgur.com/FJBWl.png)
### 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`:
[![enter image description here](https://i.stack.imgur.com/Hna6q.png)](https://i.stack.imgur.com/Hna6q.png)
### 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`:
[![enter image description here](https://i.stack.imgur.com/aCIxL.png)](https://i.stack.imgur.com/aCIxL.png)
  
[Answer]
# [Perl 5](https://www.perl.org/), `-plF` ~~101~~ ~~100~~ ~~99~~ ~~98~~ ~~97~~ 96 bytes
Replace the `\0` by a literal 0 byte to get 96. Notice that the Try It Online link has 97 bytes because it doesn't seem possible to input a literal 0 character there.
```
#!/usr/bin/perl -plF
say;say$$l=~y/\0/ /runtil--$l,(s:\Q${\<>}:$$l|=v0 x((++$#$l%3*$l-$l+"@-")%@F).$&;v0:oreg^$$l)eq$_
```
The code golf perl highlighter thinks `#` start a comment. How naive üòà
[Try it online!](https://tio.run/##DcpPC4IwGIDxe59C7DW0NV2El5nhyVuHoONoeBglvLjlliT9@eitwfPcfkaNWHpvu7kKA2D9nQvBiqgYH4PrkVLATWq5OMFL7A8fHsi7nlj0TFNCYAmY7NaAQZG4oXGWNG2Ww6qaGNejul4Cz9QdpPfnW29lqJNOWbewP21crwfrqcHW02OZb1nO/g "Perl 5 ‚Äì Try It Online")
# How it works
`$l` is a counter for which line after the first we are on (it counts down though, so e.g. -3 for 3 lines below the top string).After printing the initial string once it repeatedly does the following.
Search the first string for occurrences of the target character and calculate at which offset it should appear: `(++$#$l%3*$l-$l+"@-")%@F` which is the current position plus the line number (negative) times `-1, 0, 1` (cyclic). Construct a string with that many times `\0` followed by the target character and `or` that into an accumulator `$$l` (that is a different accumulator for each `$l` and the reason `$l` counts down instead of up because `$1`, `$2` etc are read-only). Simularly `$#$l` refers to a different array each time through the loop. The result is the `$l`th line but with `\0` instead of spaces.
The target charcters in the first string are replaced by `\0` so you end up with the original string with "holes" (with `\0`) at the original positions of the target character. If you `xor` that with the accumulator the holes get filled if and only if the accumulator has the target characters in the original positions, so the result will be the original string. That is used to terminate the loop. If the loop is not finished yet print the accumulator with `\0` replaced by space.
When the loop ends the `-p` option once more prints the first string and the program is done.
The target character is picked up in a rather tricky way. The `${\<>}` converts a line read from STDIN to a reference which is then immediately dereferenced and substituted in the regex. The `\Q` prefix escapes all characters that are special in a regex (like `.` and `*`). The `\E` is implicit. The `o` modifier causes the search part to never be evaluated again but just repeated in all subsequent matches (which is good since there is nothing on STDIN anymore).
[Answer]
# [Python 2](https://docs.python.org/2/), ~~199~~ ~~193~~ 191 bytes
```
s,c=input()
print s;l=len(s);r=range;I=j=[i for i in r(l)if s[i]==c]
while 1:
 j=[(j[i]-i%3+1)%l for i in r(len(I))]
 if sorted(j)==I:print s;break
 print''.join((' '+c)[i in j]for i in r(l))
```
[Try it online!](https://tio.run/##VY9Na8MwDIbv/hUiUGyv3aDbYdBgxpb2kNMuOwRKCI7rLs6MHWyPLr8@U8o@WqGD9PDqlTSMqfPufipetzuRZdkUV0oYN3wmxskQjEsQcyusdizyPIgg3bvOS9GLvYGjD2DAOAjMcnOEuDe1EKomp85YDesNAdSxHvGtWTws13xhr4bQteS8JjAP@5D0gfVciHLzu7kNWn4QOLeU3vXeOMYo0KXi@7NJX18dwSf8gZA/htKbRzxDf2kF848/XlP21pnYYMom6ZiyFY2UZLJVl4m0ReqdHZt08s0gUxefkHqk1tvLRGqRPrfFYae36kX@V00xBwoKFFRjhVU1L8PAUtJv "Python 2 ‚Äì Try It Online")
---
If the loop can exit via exception:
# [Python 2](https://docs.python.org/2/), 187 bytes
```
s,c=input()
print s;l=len(s);r=range;I=j=[i for i in r(l)if s[i]==c]
while 1:
 j=[(j[i]-i%3+1)%l for i in r(len(I))]
 if sorted(j)==I:print s;q
 print''.join((' '+c)[i in j]for i in r(l))
```
[Try it online!](https://tio.run/##VY9Pa8MwDMXv/hQiUGyv3Vi3wyDBjC3tIadddgiUEBLXXRyMndkebT59ppT9aYUOjx9P0tMwxs7Zhyl/22xFkiRTWEmh7fAVGSeD1zZCyIwwyrLAMy98Yz9UVohe7DQcnAcN2oJnhusDhJ2uhJAVOXbaKFinBNDHesS3evG4XPOFuRrCrQXnFYF52Pmo9qznQhTp7@VPAmdJ6V3vtGWMAl1Kvjsv6KurAHzC/IT8MbTePGGE6MdUnZSE@UcC6iTVENP7n81T8t7pUGM3dVQhJisaKEmaVl420haps2as49HVQxO78IzUITXOXDZSg/SlzfdbtZGvzb@q87nQkKOhHEtU5XwMC2VDvwE "Python 2 ‚Äì Try It Online")
---
* -4 bytes thanks to Jonathan Frech
* -2 bytes thanks to Lynn
[Answer]
# [C (gcc)](https://gcc.gnu.org), 261 bytes
```
i,j,l,k;g(L,t,r,c)char**L;{L[r++][c]=k;c-=t?t-1?:0:-1;l<r||g(L,t,r,c<0?l-1:c%l);}f(S,K){char*s=S,*L[i=1+(l=strlen(s))];for(k=K;i--;)for(L[i]=calloc(j=l,1);j--;)L[i][j]=32;for(puts(s);s[++i];)s[i]-k||g(L,++j%3,0,i);for(i=1;strcmp(*L,L[i]);)puts(L[i++]);puts(s);}
```
[Try it online!](https://tio.run/##bY/NbsIwEITvPAWKhLBjWyLlxmJFLe2J3OgBKYoi4/LjxCQodlUh4NlTOy0th6z2YHu@mfVKtpeybRUtqKYl7FFCLW2oxPIgmjBM4JKkDSFZKjNegmTcxpZF8WwyYxHoeXO9/lnmk1izaCZHGsNth1Z0iS9diuErGiap4hFBmhvb6G2FDMYZ7OoGlXwJijHA/uKojEuhdS1RwTWNMBRe8@9pkfHpU@c5fVrjEsCkhKgMsHEyK3/@QkgxmtIJVbhD3VRwI@XxhMKE@hwMuPO7s1sMwz3s1qrKDo9CVQgPLoOhqx0K3g/K5K5FbrfGBnQ4NuNfTxBguGNiIx/bc5s@rq70ObdfdX4S9mBiz9V9nK71Y3tO93HPm8XH2/ZVvoj/U77w5S2LPsv6vPbauncNV14UThzc2m8)
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~197~~ 194 bytes
```
s=>c=>{(C=console.log)(s);w=0;g=[];while(~(w=s.indexOf(c,w)))g.push(w++);h=(H=_=>g.map(x=>A[x]=c,A=Array(l=s.length).fill` `)&&A.join``)();while(h!=(y=H(g=g.map((x,i)=>(l-~x-i%3)%l))))C(y);C(s)}
```
[Try it online!](https://tio.run/##LY5BasMwEEWv0hoSZogtCl2KERhvsusBQrCNIksKimQstZIpzdVdQwt/@9779/FrjHKxc2p8uKltoi2SkCS@oSMZfAxOMRc0QkSe6Y1rulx5NtYpeEKmyKy/qfIxgawzImo2f0YD@XRCbgjO1JPQ7DHOUEi0l3IlWbfULsu4gttpp7xOBtlknRteBjweW3YP1g8DAv53zCvBSmfQ9GeCUlskAa55lsYe3vHg9jJ2sCLv9p8/G5@gSsbGft/YJxVThVDFCrdf "JavaScript (Node.js) ‚Äì Try It Online")
Takes inputs in currying syntax, i.e. `f(s)(c)`.
Not a perfect one at all, but I need JS. A lot of functions in function.
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~189 176 171 156 150 146 144~~ 137 bytes
```
->s,c,r=0..k=s.size,b=[p,s]{d=0;g=s.tr c,n=' '*k;l=r=r.map{|i|l||c==s[i]?(n[z=(i+1-d%3)%k]=g[z]=c;d+=1;z):p}-b;g==s||b<<n&&redo;puts b,s}
```
[Try it online!](https://tio.run/##VY/RboIwFIbv9xTERNFZiGZ3q0ezsb3BLkgIIW1BJVQgPTULWJ@dtReb3cl/cfJ9f05adeXDdIQp2iMRRMEmjhvAGOuxIhyynmB@K2FDTxZqFQjSQhiEzw2VoEDFF9bfTG2kMQIAszo/LNtshGW93kbl/GU1b3I4ZWMOgpZr2NJx9drfI27PARrDd7t2sVBV2dH@qjHgBO/TMZt9nWssbFihK9QzEoQY5jRwnSerGRd@nOe@71o5FPq7K3qmz3hwvvO97KQf56Xv33hSflYf4p09tiJx46qJX02H1LH03/PsOMh8@PuRPzj9AA "Ruby ‚Äì Try It Online")
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 24 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
¬ªv‚Üï*Œ¥√≥‚ÄºŒì‚ñà|q‚î¨YM‚ï®|¬∑¬£‚Üïf√ü√üZo
```
[Run and debug it online](https://staxlang.xyz/#p=af76122aeba213e2db7c71c2594dd07cfa9c1266e1e15a6f&i=%22This_is_a_test%22+%22s%22%0A%22abcabcabcabcabc%22+%22b%22%0A%22only_two_paths%3F%22+%22o%22%0A%22lollollollollol%22+%22l%22%0A%22AbCdEeDcBaAbCdEeDcBa_CCCCC%22+%22C%22%0A%22XyX%22+%22X%22%0A%22aaaa%22+%22a%22&a=1&m=2)
This is the ascii representation of the same program.
```
YxP|Ic{{i3%v-x%%mb-!Czny&PWxP
```
It gets the indices of all the character, and then mutates them until they are set-wise equal to the original indices. For every change, output a string with the character at those indices.
```
Y                               Store character in y.  Now the inputs are x and y.
 xP                             Print x.
   |Ic                          Get all indices of y in x. Make a copy.
      {                   W     Loop until cancelled.
       {        m               Map index array using block.
        i3%v-                   Subtract i%3-1 from each element. i is the loop index.
             x%%                Modulo len(x)
                 b-!            Is the mutated array set-equal to the original?
                    C           If so, cancel W loop
                     zny&P      Print a string with y at all specified indices
                           xP   Finally, print x again
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 33 bytes
```
3·∏∂N·πÅ+%‚ÅπL¬§‚Äò
·πÑ·∫π¬©¬•@√á√á·π¢‚Åª¬Æ∆ä√ê¬ø·π¨·ªã;‚Å∂¬•·πñY,Y
```
[Try it online!](https://tio.run/##AWgAl/9qZWxsef//M@G4tk7huYErJeKBuUzCpOKAmArhuYThurnCqcKlQMOHw4fhuaLigbvCrsaKw5DCv@G5rOG7izvigbbCpeG5llksWf/igJ1zIMOnIOKAnFRoaXNfaXNfYV90ZXN04oCd/w "Jelly ‚Äì Try It Online")
---
Call as a function. (dyadic link)
I found [some](https://chat.stackexchange.com/transcript/message/43286603#43286603) [33-byte](https://chat.stackexchange.com/transcript/message/43286383#43286383) [alternatives](https://chat.stackexchange.com/transcript/message/43286524#43286524), but no actual improvement.
]