Make a flag wave

Take a flag, like this one:

-----------------------------
|             |             |
|             |             |
|=============+=============|
|             |             |
|             |             |
-----------------------------


And a number input: the "wave length"

Say the wave length was 5. Then, every 5 characters along the line from the beginning, replace the next character a - with \ and shift all the characters after it one row down. Repeat this until the end. You end up with:

-----\
|     -----\
|           -----\
|=====        |   -----\
|     ======  |         -----
|           ==+===          |
-----\        |   ======    |
-----\  |         ====|
-----\          |
-----\    |
-----


If you end up not being able to make a full wave length along at the end, the flag is finished. Just remain flat until the end.

You can assume that all the lines are of the same length and that the top and bottom lines are composed entirely of - (0x2D) and the rest of the characters are in !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~, or is a space (). (So it's ascii value is x, where 31 < x < 127) The flag will be at least 3 tall and 3 wide. Wave length will be a non-negative integer (>= 0). (You can also have wave length 1 be the smallest wavelength, so the above example would have wave length 6. This is OK.) Your i/o of flag can be as arrays of lines or a string with multiple lines. The output flag may have trailing spaces, but can only have leading spaces so long as it is the same on every flag. Trailing space on an input flag may be deleted. Scoring This is code-golf, so the shortest solution, in bytes, wins. Test cases --- abc --- 2 --\ abc --\ ----- | + | ----- 10 ----- | + | ----- --------------------------------------- |&&&&&& &&&&&&| |&&&&&& &&&&&&| |&&&&&& &&&&&&| |&&&&&& .\^/. &&&&&&| |&&&&&& . | | . &&&&&&| |&&&&&& |\| |/| &&&&&&| |&&&&&& .--' '--. &&&&&&| |&&&&&& \ / &&&&&&| |&&&&&& > < &&&&&&| |&&&&&& '~|/~~|~~\|~' &&&&&&| |&&&&&& | &&&&&&| |&&&&&& &&&&&&| |&&&&&& &&&&&&| --------------------------------------- 12 ------------\ |&&&&&& ------------\ |&&&&&& ------------\ |&&&&&& &&&&&&| |&&&&&& &&&&&&| |&&&&&& .\^/. &&&&&&| |&&&&&& . | | . &&&&&&| |&&&&&& . |\| |/| &&&&&&| |&&&&&& --' '-- &&&&&&| |&&&&&& \ /. &&&&&&| |&&&&&& > < &&&&&&| |&&&&&& '~|/~~|~~\|~' &&&&&&| |&&&&&& | &&&&&&| |&&&&&& &&&&&&| ------------\ &&&&&&| ------------\ &&&&&&| ------------\ ----------------------- |-._-._ :| |: _.-'_.-| | -._:| |:_.-' | |--------' '-'-------| |------_.-. .-._------| | _.-'_.:| |:._-._ | |-'_.-' :| |: -._-| ----------------------- 4 ----\ |-._----\ | -._ :----\ |-----._:| |: ----\ |--------| |:_.-'_--- | _.--_.-' '-'_.-' .-| |-'_.-'_.:. .-.----- | ----\-' :| |:._------| ----\| |: _-._--| ----\ -._ | ----\-| --- --------------- --------------- --------------- --------------- --------------- 5 -----\ -----------\ --------------- --------------- -----\--------- -----\--- --- ------------------------------------------- |* * * * * |##########################| | * * * * *| | |* * * * * |##########################| | * * * * *| | |* * * * * |##########################| | * * * * *| | |* * * * * |##########################| |--------------- | |#########################################| | | |#########################################| | | |#########################################| ------------------------------------------- 0 \ |\ |*\ | \ |** \ | *\ |** \ | ** \ |** *\ |- ** \ |#- ** \ | #-** *\ |# #- ** \ | # #- ** \ \# # #-** *\ \# # #- ** \ \# # #- **|\ \# # #-** |#\ \# # #- **| #\ \# # #- |# #\ \# # #-**| # #\ \# # #- |# # #\ \# # #-| # # #\ \# # #-# # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # #\ \# # # # # # | \# # # # # #| \# # # # # | \# # # # #| \# # # # | \# # # #| \# # # | \# # #| \# # | \# #| \# | \#| \| \  • Expecting @LuisMendo to come here and blow this away within the next day or so. May 11, 2017 at 19:44 • Crap... Forgot @ doesn't link those who haven't responded... Someone kick my butt using MATL please so I can delete the comments. May 11, 2017 at 20:37 • Anyone animate this May 12, 2017 at 5:07 • @SIGSEGV Done, though maybe not what you had in mind. May 12, 2017 at 6:12 • @carusocomputing Did someone call me? 31 bytes, though May 12, 2017 at 10:24 7 Answers 05AB1E, 252326 25 bytes -2 thanks to Adnan -1 thanks to Emigna +3 thanks to Jonathan Allan (thanks for spending the time to catch that invalidity!) øvyN>²Öi¦¨'\.ø}N²÷ú}).Bø»  Try it online! This is 1-indexed instead of 0, +2 bytes if that isn't okay. ø # Transpose. vy } # For each column... N>²Öi¦¨'\.ø} # Replace outside dashes with slants. N²÷ # Current index / input #2. ú # Prepend that many spaces to current column. ).Bø # Join, pad, transpose back. » # Print with newlines.  Emigna/Adnan/Anyone - There HAS to be a better solution to replace those slants, but I'm stumped. • The 1-indexing is OK. May 11, 2017 at 18:44 • ú is the same as ð×ì :) May 11, 2017 at 19:03 • @Artyer not at all! +3 thanks to him for pointing out my answer wasn't valid, I like it when people actually care enough to read an answer long enough to notice it isn't correct. Did I reword it enough to sound less sarcastic :P? May 11, 2017 at 19:21 • @Adnan NO WONDER I CAN NEVER REPLY TO HIM. Sorry about butchering your name Jonathan Allan. May 11, 2017 at 19:42 • Really not a problem :) May 11, 2017 at 20:07 Pyth, 35 bytes j.ts.e+L*k;?qQtlb+PbjPteb*2\\bcC.zh  Try it online! Python 2, 129 bytes lambda f,n,j=''.join:map(j,zip(*[i/n*' '+'-\\'[i%n>n-2]+j(s[1:-1])+'-\\'[i%n>n-2]+len(f[0])/n*' 'for i,s in enumerate(zip(*f))]))  Try it online! Jelly, 29 bytes s”\⁹¦€Fð€0,1¦Zsð⁶ẋ;Ð€µ@"J;/z⁶  Input and output flags are lists of lines. Wave length is 1-indexed. Always produces one line of spaces. (all of the above is explicitly allowed in the posted question) Try it online! (footer to make IO look pretty - takes and receives flags as multiline text) How? A pretty similar method to carusocomputing's 05ab1e answer, which I have not managed to golf down more. s”\⁹¦€Fð€0,1¦Zsð⁶ẋ;Ð€µ@"J;/z⁶ - Main link: list of lists f, number p ¦ - apply to indexes (of f) 0,1 - ... 0 paired with 1 (i.e. last & first -> bottom & top) ð€ - the previous chain with p as right argument for €ach: s - split into chunks of length p ¦€ - apply to indexes for €ach ⁹ - ... link's right argument, p ”\ - the character '\' (a replacement - if the index is out of bounds this has no effect - although this might change in the future.) Z - transpose the edited flag s - split into chunks of length p J - range of length = [1,2,...,nChunks] ð µ@" - zip with reversed arguments (call those i): ⁶ - literal space ẋ - repeated i times ;Ð€ - concatenate mapped across the chunks ;/ - undo the split (reduce with concatenation) z⁶ - transpose with a filler of space characters  • Why is it always Jelly that does this? Jun 6, 2017 at 16:02 • 05AB1E clearly won this round! Jun 6, 2017 at 18:54 • OK, why is it always either Jelly or 05AB1E that does this? (Rhetorical question, I know it's because they're golfing languages) Jun 6, 2017 at 18:56 MATL, 31 bytes x"X@q1GQ&\Z"@b1G=?'\'5L(]h]Xhc!  Try it online! Or verify all test cases: 1, 2, 3, 4, 5, 6, 7. • Wow, what was the expensive part? The replacing the - with the [\]? May 12, 2017 at 14:00 • @carusocomputing Yes, I think that's the most expensive part May 12, 2017 at 14:05 JavaScript (ES6), 175 bytes f=(s,l,p=++l,t=s.replace(/^-*|-*$/g,s=>s.replace(/-/g,(c,i)=>++i%l?c:\\)))=>t.search
<p?t:(f(s,l,p+l,t)+
+ .repeat(p)).replace(eval(/(^|(.*)\\n)(.{${p}})/g),$3$2) ; test=(s,l)=>document.write(<pre>${s}
${l}${f(s,l)}</pre>);
test(---
abc
---,2);
test(-----
| + |
-----,10);
test(---------------------------------------
|&&&&&&                         &&&&&&|
|&&&&&&                         &&&&&&|
|&&&&&&                         &&&&&&|
|&&&&&&          .\\^/.          &&&&&&|
|&&&&&&        . |   | .        &&&&&&|
|&&&&&&        |\\|   |/|        &&&&&&|
|&&&&&&     .--'       '--.     &&&&&&|
|&&&&&&      \\           /      &&&&&&|
|&&&&&&       >         <       &&&&&&|
|&&&&&&      '~|/~~|~~\\|~'      &&&&&&|
|&&&&&&            |            &&&&&&|
|&&&&&&                         &&&&&&|
|&&&&&&                         &&&&&&|
---------------------------------------,12);
test(-----------------------
|-._\-._ :| |: _.-'_.-|
|   \-._\:| |:\_.-'   |
|-------\-' '-'-------|
|------_.-. .-._------|
|  _.-'_.:| |:._\-._  |
|-'_.-'  :| |:  \-._\-|
-----------------------,4);
test(---------------
---------------
---------------
---------------
---------------,5);
test(-------------------------------------------
|*  *  *  *  * |##########################|
| *  *  *  *  *|                          |
|*  *  *  *  * |##########################|
| *  *  *  *  *|                          |
|*  *  *  *  * |##########################|
| *  *  *  *  *|                          |
|*  *  *  *  * |##########################|
|---------------                          |
|#########################################|
|                                         |
|#########################################|
|                                         |
|#########################################|
-------------------------------------------,0);

I/O is as a newline-delimited string. Output includes a leading newline; this can be removed at a cost of 3 bytes. I tried computing the output string directly but that took me... 176 bytes:

f=
(a,l,h=a.length)=>[...Array(h+(a[0].length-1)/++l|0)].map((_,i)=>a[0].replace(/./g,(k,j)=>((k=i-(j/l|0))&&h+~k)|-~j%l?(a[k]||'')[j]||' ':'\\'))
;
test=(s,l)=>document.write(<pre>${s}${l}
${f(s.split ,l).join }</pre>); test(--- abc ---,2); test(----- | + | -----,10); test(--------------------------------------- |&&&&&& &&&&&&| |&&&&&& &&&&&&| |&&&&&& &&&&&&| |&&&&&& .\\^/. &&&&&&| |&&&&&& . | | . &&&&&&| |&&&&&& |\\| |/| &&&&&&| |&&&&&& .--' '--. &&&&&&| |&&&&&& \\ / &&&&&&| |&&&&&& > < &&&&&&| |&&&&&& '~|/~~|~~\\|~' &&&&&&| |&&&&&& | &&&&&&| |&&&&&& &&&&&&| |&&&&&& &&&&&&| ---------------------------------------,12); test(----------------------- |-._\-._ :| |: _.-'_.-| | \-._\:| |:\_.-' | |-------\-' '-'-------| |------_.-. .-._------| | _.-'_.:| |:._\-._ | |-'_.-' :| |: \-._\-| -----------------------,4); test(--------------- --------------- --------------- --------------- ---------------,5); test(------------------------------------------- |* * * * * |##########################| | * * * * *| | |* * * * * |##########################| | * * * * *| | |* * * * * |##########################| | * * * * *| | |* * * * * |##########################| |--------------- | |#########################################| | | |#########################################| | | |#########################################| -------------------------------------------,0); I/O is as a string array. • It looks like the only test case yours passes is the one that doesnt change. May 12, 2017 at 20:35 • Yes, this doesn't work properly for most test cases. (The first one was what I tried) May 12, 2017 at 21:03 • @Artyer Sorry, that was subtle; I didn't spot the off-by-one error. – Neil May 13, 2017 at 1:02 PHP, 168 164 187 172 167 153 150 152 149 bytes for($r=count($f=file(a));$y<$r+$e/$n=$argn;$y+=print" ")for($x=0;$x+1<$e=strlen($f[0])-1;)echo("\\".$f[$z=$y-($x/$n|0)][$x++]." ")[$z%($r-1)||$x%$n];  takes flag from static file a and wave length (minimum 1) from STDIN. Run as pipe with php -nr or try it online. breakdown for($r=count($f=file(a)); # import file, count lines$y<$r+$e/$n=$argn;          # loop $y through lines$y+=print"\n")                      # 2. print newline
for($x=0;$x+1<$e=strlen($f[0])-1;)  # 1. loop $x through columns echo("\\".$f[                           # 3. create string=backslash+character+space
$z=$y-($x/$n|0)                 # 1. line no. = $y - wave offset ][$x++]." "                         # 2. pick character from line
)
[
$z%($r-1)                           # if not first or last line
||$x%$n                             # or not last position of wave
]                                       # then index 1 (character or space), else "\\"
;

• Doesn't work for lines in the middle: sandbox.onlinephpfunctions.com/code/… May 12, 2017 at 8:48
• It shouldn't turn the middle lines into . See the test case after the British one May 12, 2017 at 8:50
• It seems to be cutting off some of the bottoms of flags that increase in size a lot: sandbox.onlinephpfunctions.com/code/… May 12, 2017 at 9:35
• @Artyer That should be it. May 12, 2017 at 9:56
• $z&&$z!=$r-1 should be equivalent to $z%(\$r-1)` saving 3 byte. May 15, 2017 at 11:11