16
\$\begingroup\$

I forgot towel day

Sandbox link

PHP script for both examples (error, formatting the string)

Corrected php script, the input is self explanatory

PHP script with more input comment/uncomment $argv to see different results

As you may know, May 25 is well known as Towel Day, because of the many uses a towel can have.

A simple extract of the book "The Hitchhiker's Guide to the Galaxy" (personally I took this book as an "everyday guide") states:

"A towel is about the most massively useful thing an interstellar hitchhiker can have. Partly it has great practical value."

If you need more information about towels check this SE.scifi answer


The Challenge

Hopefully you will do this with a towel used as a blanket for your legs.

Write a full program or function in any valid programming language that takes two inputs

size        Integer  : #The size
message     string   : #A sentence

How to draw a towel with these values?

First using size:

Draw a towel depending on the size input, for our towel

  width = 12*size

  #the line that has the "bar" its always present with different sizes

  Width is defined as:
  from the first | to the second | has to be equal to size*12 including both ||

      <-   width-> 
  [===|          |====]  
      | 12 chars ||   1  height starts here, just after the "bar"
      | SIZE = 1 ||   2
      |          ||   3   height = 5*size
      |          ||   4
      |          ||   5  height ends here just before the first line |=======|
      |==========||   # the lines with the # 
      |==========||   # are always fixed
      """"""""""""|   # it means, every towel
       |          |   # always has this 5 rows
       """"""""""""   # no matter the size

Second, give it the message

You have to give it a message, what is a towel without a beautiful message sewed in golden thread?

Example 1

  input: size=1, message="a simple message can stay with size"

  width = 12*size

  #the line that has the "bar" it's always present with different sizes
  #no words allowed at the bar level


  [===|          |====]  
      | a simple ||   1  height starts here, just after the "bar"
      | message  ||   2
      | can stay ||   3   height = 5*size
      | with size||   4
      |          ||   5  height ends here just before the first line |=======|
      |==========||   # the lines with the # 
      |==========||   # are always fixed
      """"""""""""|   # it means, every towel
       |          |   # always has this 5 rows
       """"""""""""   # no matter the size

Example 2

input size=2 
message="Don't Panic and bring a towel to SE Programming Puzzles and CodeGolf"

The size is 2
That means 24 width and 10 heigth

    <-    24 chars width  ->
[===|                      |====]
    | Don't Panic and bring||   1
    | a towel to SE        ||   2
    | Programming Puzzles  ||   3
    | and CodeGolf         ||   4
    |                      ||   5
    |                      ||   6
    |                      ||   7
    |                      ||   8
    |                      ||   9
    |                      ||   10
    |======================||   #  The lines with the "#"
    |======================||   #  always present and
    """"""""""""""""""""""""|   #  adapted to 
     |                      |   #  the towel width
     """"""""""""""""""""""""   #  

Accepted answer criteria

  • This is codegolf so normal rules apply.

Rules

  • You are guaranteed that all the input strings will fit the size, so no input like size=1; message="This string is just tooooooooooooooooooooo long to fit the width and height of your towel".

  • The string format is up to you, if you want to center the substrings for example.

  • Word-breaks are disallowed.

Edits

I am truly sorry about any confussion, because the ansii towels i draw did not matched the parameters, added a PHP script for both examples for you to check the expected outputs.

Also thank you to all the people who voted up and considered my first challenge :D.

\$\endgroup\$
  • \$\begingroup\$ Give me a few minutes to fix it (just saw this). What happened was that i made a program on php to draw the towel but it was much simpler than the one i presented, i had a program for the examples and i forgot to modify it. \$\endgroup\$ – Francisco Hahn Jun 8 '18 at 13:47
  • \$\begingroup\$ @Lynn added a php ungolfed solution with both test cases, sorry about any confussion. \$\endgroup\$ – Francisco Hahn Jun 8 '18 at 14:45
  • \$\begingroup\$ Can we choose where the line breaks go? \$\endgroup\$ – Jakob Jun 8 '18 at 20:01
  • \$\begingroup\$ The line break should be added when the current line + next word will exceed the limit permited for your towel @Jakob as the php script i posted it decides. \$\endgroup\$ – Francisco Hahn Jun 8 '18 at 20:07
  • \$\begingroup\$ can we take the proper text width and/or height as arguments instead of computing them? \$\endgroup\$ – JoshM Jun 14 '18 at 18:03
4
\$\begingroup\$

Python 2, 223 210 209 204 bytes

def f(s,m):w=12*s-2;l='    |';print'[===|'+' '*w+'|====]';exec"j=(m+' ').rfind(' ',0,-~w);print l+'%*s||'%(w,m[:j]);m=m[j+1:];"*s*5;print(l+w*'='+'||\n')*2+' '*4+'"'*12*s+'|\n '+l+' '*w+'|\n    ','"'*12*s

Try it online!

\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 375 bytes

This is the worst submission you will get, but at least I tried xD half of bytes are spaces though

(s,_,w=12*s-2,h=5*s)=>`
[===|${j=' '.repeat(w)}|====]
${[...Array(h)].map((g,i)=>_.split` `.reduce((a,b)=>((l=a.split`,`)[l.length-1]+b).length>w-1?a+','+b:a+' '+b,'').split`,`[i]).map(a=>`    |${a?(r=a.length)<w?a+' '.repeat(w-r):a:j}||    `).join`\n`+
`
    |${'='.repeat(w)}||    `.repeat(2)}
    ${y='"'.repeat(w+2)}|    
     |${j}|    
     ${y}
`

Try it online!

\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 347 345 343 337 334 328 326 bytes

b=>d=>`
[===|${M=" "[X="repeat"](c=12*b-2)}|====]
${[...Array(5*b)].map((a,b)=>d[Y="split"]` `.reduce((a,b)=>((l=a[Y]`,`)[l[Z="length"]-1]+b)[Z]>c-1?a+","+b:a+" "+b)[Y]`,`[b]).map(a=>`    |${a?(r=a[Z])<c?a+" "[X](c-r):a:" "[X](c)}||    `).join`
`+`
    |${"="[X](c)}||    `[X](2)}
    ${y='"'[X](c+2)}|
     |${M}|
     ${y}
`

Try it online!


Explanation :

b =>                                   // lambda function taking arg 1 : size
    d =>                               // arg 2 : message
        `                              // begin string template for drawing
[===|${                                // draw first part and now open literal 
        M = ' '[X='repeat'(c=12*b-2)]  // set 3 variables, M, X , c to be used again
    }|====]                            // close and draw the next part ====]
${[...Array(5*b)]                      // open and create an array of length 5 * b =width
.map(a,b=>                             // begin map function two args : a,b
        d[Y='split']` `                // use d(message), split at whitespace, set to Y
.reduce((a,b) =>                       // reduce to single value, arg 1 : a, arg 2 : b
        ((l = a[Y]`,`)                 // declare l and then find in l
        [l[Z='length']-1]              // set Z as length
        + b)                           // add value of b
        [Z]                            // find the length 
        > c-1 ?                        // check if it's less than c - 1
        a+','+b                        // then add `${a},${b}`
        : a + ' ' + b                  // otherwise `${a} ${b}`
        )[Y]`,`                        // close and split at comma
        [b]                            // use b again
        )                              // close
        .map(a =>                      // map over that arg 1 : a
                `    |${               // four space + | and open 
        a ?                            // if a is true or a truthy value
            (r=a[Z])                   // set value of r as a's length
            < c ?                      // check if it's less than c
                a+' '[X](c-r)          // then draw a + space repeated c-r times
                : a + ' '[X](c)        // else draw a + space repeated c times
            }                          // close
            ||    `                    // add || and 4 spaces and close
        )                              // end
        .join`
`                                      // and turn to string with new line as separator 
    +                                  // add to that
    `
    |{                                 // new line , 4 spaces and | 
        '='[X](c)}                     // repeat = c times
        ||    `[X](2)}                 // and repeat that 2 times
    ${                                 // new line + 4 space
    y = '"'[X](c+2)                    // repeat " c + 2 times and set to y
    }|                                 // close and add |
    |{                                 // add | and open
        M}|                            // put M and close and add |
    {y}                                // new line , 4 spaces and variable y
`                                      // end with new line.
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 79 bytes

Nθ≔×⁵θε≔×¹²θδ←P←====[↓⁺³ε×"δ↖P↑⁺³ε←G↑²←⁻δ²↓²=↓↓↓¹×"δ↖↑⁺⁴ε====]F⪪S «×⸿›⁺ⅈLι⁻δ² ι

Try it online! Link is to verbose version of code. Explanation:

Nθ≔×⁵θε≔×¹²θδ

Calculate the size of the towel.

←P←====[↓⁺³ε×"δ↖P↑⁺³ε←G↑²←⁻δ²↓²=↓↓↓¹×"δ↖↑⁺⁴ε====]

Draw the towel.

F⪪S «

Loop over each word of the message.

×⸿›⁺ⅈLι⁻δ² ι

Print each word without overflowing the width.

\$\endgroup\$
  • \$\begingroup\$ Marvelous, and thank you for taking your time for the challenge. \$\endgroup\$ – Francisco Hahn Jun 8 '18 at 20:00
1
\$\begingroup\$

V, 142, 128, 121, 119 bytes

:let @h=5*@a
:let @w=12*@a-2
O[===|@wá 4á=á]
:set tw=w
Vgq@hï{+@hjI    |kf|@hjjyt=pjp+@hjkl@wr=Ùjlr|Ù@w««r"YkP0xÁ|

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ This is the kind of code i would love to say "Hey, if you do this you can save n Bytes"....but i can't...yet :D. \$\endgroup\$ – Francisco Hahn Jun 12 '18 at 14: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.