26
\$\begingroup\$

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
\
|\
|*\
|  \
|** \
|   *\
|**   \
|   ** \
|**    *\
|-  **   \
|#-    ** \
| #-**    *\
|# #-  **   \
| # #-    ** \
\# # #-**    *\
 \# # #-  **   \
  \# # #-    **|\
   \# # #-**   |#\
    \# # #-  **| #\
     \# # #-   |# #\
      \# # #-**| # #\
       \# # #- |# # #\
        \# # #-| # # #\
         \# # #-# # # #\
          \# # # # # # #\
           \# # # # # # #\
            \# # # # # # #\
             \# # # # # # #\
              \# # # # # # #\
               \# # # # # # #\
                \# # # # # # #\
                 \# # # # # # #\
                  \# # # # # # #\
                   \# # # # # # #\
                    \# # # # # # #\
                     \# # # # # # #\
                      \# # # # # # #\
                       \# # # # # # #\
                        \# # # # # # #\
                         \# # # # # # #\
                          \# # # # # # #\
                           \# # # # # # #\
                            \# # # # # # #\
                             \# # # # # # |
                              \# # # # # #|
                               \# # # # # |
                                \# # # # #|
                                 \# # # # |
                                  \# # # #|
                                   \# # # |
                                    \# # #|
                                     \# # |
                                      \# #|
                                       \# |
                                        \#|
                                         \|
                                          \
\$\endgroup\$
  • 4
    \$\begingroup\$ Expecting @LuisMendo to come here and blow this away within the next day or so. \$\endgroup\$ – Magic Octopus Urn May 11 '17 at 19:44
  • \$\begingroup\$ Crap... Forgot @ doesn't link those who haven't responded... Someone kick my butt using MATL please so I can delete the comments. \$\endgroup\$ – Magic Octopus Urn May 11 '17 at 20:37
  • 2
    \$\begingroup\$ Anyone animate this \$\endgroup\$ – Matthew Roh May 12 '17 at 5:07
  • 1
    \$\begingroup\$ @SIGSEGV Done, though maybe not what you had in mind. \$\endgroup\$ – Brian McCutchon May 12 '17 at 6:12
  • 1
    \$\begingroup\$ @carusocomputing Did someone call me? 31 bytes, though \$\endgroup\$ – Luis Mendo May 12 '17 at 10:24
11
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ The 1-indexing is OK. \$\endgroup\$ – Artyer May 11 '17 at 18:44
  • 1
    \$\begingroup\$ ú is the same as ð×ì :) \$\endgroup\$ – Adnan May 11 '17 at 19:03
  • 1
    \$\begingroup\$ @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? \$\endgroup\$ – Magic Octopus Urn May 11 '17 at 19:21
  • 2
    \$\begingroup\$ @Adnan NO WONDER I CAN NEVER REPLY TO HIM. Sorry about butchering your name Jonathan Allan. \$\endgroup\$ – Magic Octopus Urn May 11 '17 at 19:42
  • 2
    \$\begingroup\$ Really not a problem :) \$\endgroup\$ – Jonathan Allan May 11 '17 at 20:07
9
\$\begingroup\$

Pyth, 35 bytes

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

Try it online!

\$\endgroup\$
8
\$\begingroup\$

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!

\$\endgroup\$
5
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Why is it always Jelly that does this? \$\endgroup\$ – Gryphon - Reinstate Monica Jun 6 '17 at 16:02
  • \$\begingroup\$ 05AB1E clearly won this round! \$\endgroup\$ – Jonathan Allan Jun 6 '17 at 18:54
  • \$\begingroup\$ OK, why is it always either Jelly or 05AB1E that does this? (Rhetorical question, I know it's because they're golfing languages) \$\endgroup\$ – Gryphon - Reinstate Monica Jun 6 '17 at 18:56
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Wow, what was the expensive part? The replacing the - with the [\]? \$\endgroup\$ – Magic Octopus Urn May 12 '17 at 14:00
  • \$\begingroup\$ @carusocomputing Yes, I think that's the most expensive part \$\endgroup\$ – Luis Mendo May 12 '17 at 14:05
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ It looks like the only test case yours passes is the one that doesnt change. \$\endgroup\$ – Marie May 12 '17 at 20:35
  • \$\begingroup\$ Yes, this doesn't work properly for most test cases. (The first one was what I tried) \$\endgroup\$ – Artyer May 12 '17 at 21:03
  • 2
    \$\begingroup\$ @Artyer Sorry, that was subtle; I didn't spot the off-by-one error. \$\endgroup\$ – Neil May 13 '17 at 1:02
2
\$\begingroup\$

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.