Make a flag wave

26

2

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
\
|\
|*\
|  \
|** \
|   *\
|**   \
|   ** \
|**    *\
|-  **   \
|#-    ** \
| #-**    *\
|# #-  **   \
| # #-    ** \
\# # #-**    *\
 \# # #-  **   \
  \# # #-    **|\
   \# # #-**   |#\
    \# # #-  **| #\
     \# # #-   |# #\
      \# # #-**| # #\
       \# # #- |# # #\
        \# # #-| # # #\
         \# # #-# # # #\
          \# # # # # # #\
           \# # # # # # #\
            \# # # # # # #\
             \# # # # # # #\
              \# # # # # # #\
               \# # # # # # #\
                \# # # # # # #\
                 \# # # # # # #\
                  \# # # # # # #\
                   \# # # # # # #\
                    \# # # # # # #\
                     \# # # # # # #\
                      \# # # # # # #\
                       \# # # # # # #\
                        \# # # # # # #\
                         \# # # # # # #\
                          \# # # # # # #\
                           \# # # # # # #\
                            \# # # # # # #\
                             \# # # # # # |
                              \# # # # # #|
                               \# # # # # |
                                \# # # # #|
                                 \# # # # |
                                  \# # # #|
                                   \# # # |
                                    \# # #|
                                     \# # |
                                      \# #|
                                       \# |
                                        \#|
                                         \|
                                          \

Artyer

Posted 2017-05-11T17:56:13.963

Reputation: 1 697

4Expecting @LuisMendo to come here and blow this away within the next day or so. – Magic Octopus Urn – 2017-05-11T19:44:37.437

Crap... Forgot @ doesn't link those who haven't responded... Someone kick my butt using MATL please so I can delete the comments. – Magic Octopus Urn – 2017-05-11T20:37:22.397

2Anyone animate this – Matthew Roh – 2017-05-12T05:07:35.307

The first testcase seems to contradict this statement of yours: 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. – user41805 – 2017-05-12T05:07:38.530

1

@SIGSEGV Done, though maybe not what you had in mind.

– Brian McCutchon – 2017-05-12T06:12:25.197

@KritixiLithos So the rest of the flag (Nothing) goes down, and that nothingness stays flat. – Artyer – 2017-05-12T08:09:49.523

1@carusocomputing Did someone call me? 31 bytes, though – Luis Mendo – 2017-05-12T10:24:24.313

Is the Canadian flag example wrong? The second shifted column contains a . that doesnt shift with the row. – Marie – 2017-05-12T20:28:10.610

@Marie It's in the column that has a \. Only the columns after the \ shift down. – Artyer – 2017-05-12T20:58:25.703

Line 9 column 27, it is directly in line with the first - of the last turn-downed section. – Marie – 2017-05-12T21:41:12.877

@Marie Oh yeah, that's a mistake. I was looking at the wrong dot. Thanks! – Artyer – 2017-05-13T09:16:10.120

No problem. I only noticed it because my solution didnt match and diffchecker highlighted that dot – Marie – 2017-05-13T18:34:04.293

Answers

11

05AB1E, 25 23 26 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.

Magic Octopus Urn

Posted 2017-05-11T17:56:13.963

Reputation: 19 422

The 1-indexing is OK. – Artyer – 2017-05-11T18:44:23.097

1ú is the same as ð×ì :) – Adnan – 2017-05-11T19:03:29.270

@Adnan that's GOTTA be new, right? No ideas on "replace last" though? I feel like 05AB1E needs replace first and replace last. – Magic Octopus Urn – 2017-05-11T19:10:26.963

@JonathanAllan fixed for +3 byte penalty. – Magic Octopus Urn – 2017-05-11T19:16:48.513

+3 thanks to Johnathan Allen sounds so passive aggressive... – Artyer – 2017-05-11T19:20:35.643

1@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? – Magic Octopus Urn – 2017-05-11T19:21:14.993

2@Adnan NO WONDER I CAN NEVER REPLY TO HIM. Sorry about butchering your name Jonathan Allan. – Magic Octopus Urn – 2017-05-11T19:42:49.450

2Really not a problem :) – Jonathan Allan – 2017-05-11T20:07:31.990

Very nice answer. Only improvement I can see is %_ => Ö. – Emigna – 2017-05-12T06:19:09.993

9

Pyth, 35 bytes

j.ts.e+L*k;?qQtlb+PbjPteb*2\\bcC.zh

Try it online!

Leaky Nun

Posted 2017-05-11T17:56:13.963

Reputation: 45 011

8

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!

ovs

Posted 2017-05-11T17:56:13.963

Reputation: 21 408

5

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

Jonathan Allan

Posted 2017-05-11T17:56:13.963

Reputation: 67 804

Why is it always Jelly that does this? – Gryphon – 2017-06-06T16:02:18.353

05AB1E clearly won this round! – Jonathan Allan – 2017-06-06T18:54:55.447

OK, why is it always either Jelly or 05AB1E that does this? (Rhetorical question, I know it's because they're golfing languages) – Gryphon – 2017-06-06T18:56:48.017

3

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.

Luis Mendo

Posted 2017-05-11T17:56:13.963

Reputation: 87 464

Wow, what was the expensive part? The replacing the - with the [\]? – Magic Octopus Urn – 2017-05-12T14:00:56.990

@carusocomputing Yes, I think that's the most expensive part – Luis Mendo – 2017-05-12T14:05:19.387

2

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 "\\"
    ;

Titus

Posted 2017-05-11T17:56:13.963

Reputation: 13 814

Doesn't work for lines in the middle: http://sandbox.onlinephpfunctions.com/code/83b3163b402e0a675bc8bffa419e1424acee2d86

– Artyer – 2017-05-12T08:48:45.113

It shouldn't turn the middle lines into \. See the test case after the British one – Artyer – 2017-05-12T08:50:15.260

It seems to be cutting off some of the bottoms of flags that increase in size a lot:

http://sandbox.onlinephpfunctions.com/code/b6b259e033d3e3d48f9aa23e71dbd6c6dbfcc0c6

– Artyer – 2017-05-12T09:35:07.613

@Artyer That should be it. – Titus – 2017-05-12T09:56:22.213

1$z&&$z!=$r-1 should be equivalent to $z%($r-1) saving 3 byte. – Christoph – 2017-05-15T11:11:15.840

2

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.

Neil

Posted 2017-05-11T17:56:13.963

Reputation: 95 035

It looks like the only test case yours passes is the one that doesnt change. – Marie – 2017-05-12T20:35:37.173

Yes, this doesn't work properly for most test cases. (The first one was what I tried) – Artyer – 2017-05-12T21:03:05.143

2@Artyer Sorry, that was subtle; I didn't spot the off-by-one error. – Neil – 2017-05-13T01:02:28.133