12
\$\begingroup\$

You want to send an ASCII letter to someone, but you need an envelope to put it in. Your task is to output an envelope given a size as input.

Rules:

  • Given positive integer input i, your envelope will be i characters tall (not counting the top row), and i*2 characters wide (not counting the side columns). In other words, your envelope will be i+1 characters tall and (i*2)+2 characters wide.
  • Output must be in the form of text, using \, /, _, |, and .
  • The flap of the envelope must be on the row above the bottom row of the envelope unless impossible (as with an envelope of size 1).
  • Trailing whitespace is allowed.

Example input and output:

1
->
 __
|\/|

2
->
 ____
|\__/|
|____|

3
->
 ______
|\    /|
| \__/ |
|______|

4
->
 ________
|\      /|
| \    / |
|  \__/  |
|________|

This is a challenge, so shortest code wins!

\$\endgroup\$
0

9 Answers 9

3
\$\begingroup\$

05AB1E, 41 bytes

i'_„|\‚ë2ÊiÍFðI×'\Nǝ}}„\_Ij¤Iש)'|쮚}º.c

Try it online or verify the first few outputs.

Expanation:

i              # If the (implicit) input is 1:
 '|„|\‚       '#  Push pair of strings ["_","|\"]
ë              # Else:
 2Êi           #  If the (implicit) input is NOT 2:
    ÍF         #   Loop `N` in the range [0,input-2):
      ðI×      #    Push a string with the input amount of spaces
         '\Nǝ '#    Insert a "\" character at index `N`
     }         #   Close the loop
   }           #  Close the inner if-statement
„\_            #  Push string "\_"
   Ij          #  Pad leading spaces until the string is of a length equal to the input
     ¤         #  Push the last character (without popping): "_"
      I×       #  Repeat it the input amount of times as string
        ©      #  Store it in variable `®` (without popping)
)              #  Wrap all values on the stack into a list
 '|ì          '#  Prepend a "|" in front of each string
    ®š         #  Prepend string `®` to the list
}              # Close the outer if-else statement
 º             # Mirror each inner string
  .c           # Join by newlines and centralize
               # (adding a single leading space to the first "_"-string)
               # (after which the result is output implicitly)
\$\endgroup\$
1
  • 13
    \$\begingroup\$ Congrats on reaching 100k! \$\endgroup\$
    – ophact
    Mar 24, 2022 at 16:09
3
\$\begingroup\$

Python 3, 207 181 180 177 146 130 bytes

lambda i,u="__",b=" ":b+u*i+"".join(f"\n|{b*x}\{(b*(i*2+~x*2),u)[x==i-2]}/{b*x}|"for x in range(i-1))+("\n|\/|",f"\n|{u*i}|")[i>1]

Try it online!

To 181 bytes: Golfed the code even more thanks to @Kevin Cruijssen.

To 180 bytes: -1 bytes thanks again to @Kevin Cruijssen, by replacing the i!=1 with i<2.

To 177 bytes: Moved the whole "nonsense soup" line onto the same line as the for loop, thanks to @des54321's suggestion.

To 146 bytes: Some major byte-saving golfs thanks to @ElPedro.

To 130 bytes: More golfs by @Kevin Cruijssen, including replacing \\ with \.

\$\endgroup\$
14
  • 1
    \$\begingroup\$ 181 bytes with some golfs :) The u,b declaration is a single line to save a byte; the if/else are both single lines with and/or to save byes; and the (i*2)-(x*2)-2 (for which you don't need the parenthesis) is i*2+~x*2 to save some bytes (~x is -x-1 btw). \$\endgroup\$ Mar 24, 2022 at 22:20
  • 1
    \$\begingroup\$ One more small golf for -1: s+(i!=1and f"|{u*i*2}|"or"|\\/|") can be s+(i<2and"|\\/|"or f"|{u*i*2}|") \$\endgroup\$ Mar 25, 2022 at 7:40
  • 2
    \$\begingroup\$ After @ElPedro golfs everything can be put onto a single line and you can use a lambda for 134 bytes. \$\endgroup\$ Mar 29, 2022 at 14:29
  • 2
    \$\begingroup\$ 130 bytes by replacing both \\ with \ (apparently they don't have to be escaped in Python's strings depending on the character behind it). \$\endgroup\$ Mar 29, 2022 at 14:33
  • 2
    \$\begingroup\$ @KevinCruijssen - I guessed you'd be back! Thanks for finishing it off for me. Was busy this afternoon :) Didn't know that about the \\ in f-strings. Learned something new today. Thanks. \$\endgroup\$
    – ElPedro
    Mar 29, 2022 at 15:23
3
\$\begingroup\$

Vyxal C, 49 bytes

1=[\_‛|\"|2≠[⇩ʁ(⁰Ifn\\Ȧ¤j)]‛\_⁰r:t⁰ẋ:£W\|vp¥p]vøṀ

Try it Online!

\$\endgroup\$
2
  • \$\begingroup\$ Doesn't work, draws backslashes instead of slashes \$\endgroup\$
    – allxy
    Apr 30, 2022 at 22:43
  • \$\begingroup\$ @allxy Fixed and shavead off a couple more bytes. \$\endgroup\$
    – naffetS
    May 1, 2022 at 0:44
2
\$\begingroup\$

Charcoal, 28 bytes

Nθ←×_θ↑θ→P×_θ↓¿⊖θ«↘⊖θ←¶_»\‖M

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

Nθ

Input i.

←×_θ

Draw half of the bottom row of _s.

↑θ

Draw the left column of |s.

→P×_θ↓

Draw the top row of _s.

¿⊖θ«

If i is not 1, then...

↘⊖θ←¶_

... draw the i-1 \s on the left and one of the two middle _s, ...

»\

... otherwise overwrite the bottom _ with a \.

‖M

Reflect to complete the envelope.

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

Javascript, 181 177 174 Bytes

Code is split into newlines for readability, see link for 174 byte string.

c=n=>{
r=(d,n)=>d.repeat(n);
z='\n|';
b=r('_',n*2);
o=' '+b;
for(e=0;e<n-1;e++)y=r(' ',e),o+=z+y+'\\'+r(e!=n-2?' ':'_',n*2-2*(e+1))+'/'+y+'|';
o+=(n==1?z+'\\/|':z+b+'|');
return o;}

Try me online.

  • Saved 4 bytes pulling \n| into a variable z
  • Saved 3 bytes pulling r(' ',e) into y every loop

I'm not too happy with my performance here, especially the implicit check I had to do to get the i=1 output working, but oh well.

\$\endgroup\$
1
  • \$\begingroup\$ Just so you know, readability doesn't matter in this case, so you can make it as unreadable as you like ;-)! The goal is to have it as golfed as possible. \$\endgroup\$ Apr 23, 2022 at 23:58
2
\$\begingroup\$

Vyxal o, 37 bytes

\_*:£ðpm,ċ[ɽ‹\\꘍ṫ\_+J↲¥J\|vpvøṀ⁋|‛|\m

Try it Online!

\_*                                   # <input> copies of _
   :£                                 # Duplicate and set gregister
     ðpm,                             # Prepend a space and print mirrored
         ċ[                     |‛|\m # If it's 1, print `|\/|`, else:
           ɽ‹                         # 0...n-1
             \\꘍                      # For each, prepend that many spaces to an underscore
                ṫ   J                 # To the last...
                 \_+                  # Append an underscore
                     ↲                # Pad all of that to length n
                      ¥J              # Append the underscores that we stored in the register earlier
                        \|vp          # Prepend a | to each
                            vøṀ⁋      # Mirror and print
\$\endgroup\$
2
\$\begingroup\$

Python 3.8 (pre-release), 107 bytes

lambda n:[' '+(a:="__"*n)]+['|%s|'%[a," "*i+f'\{"_ "[i<n-2]*2*(n+~i)}/'+" "*i][i<n-(n>2)]for i in range(n)]

Try it online!

-20 by @emanresuA

\$\endgroup\$
1
  • \$\begingroup\$ You can save a bunch by outputting as a list of lines, which opens up a whole host of golfing oppurtunities - 107 bytes. \$\endgroup\$
    – emanresu A
    May 1, 2022 at 10:04
1
\$\begingroup\$

Perl 5 + -M5.10.0 -a, 98 bytes

say$".__ x$_,map{'
|',/@F/*($#a=$_-1)?__ x$_.'|':"@a\\".($_<"@F"-1?'  ':__)x("@F"-$_)."/@a|"}1..$_

Try it online!

Currently on mobile (in a rainy field!) so I'll add an explanation later!

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 68 bytes

ð'_IиJJÂJUX,I<F'|ðNи'\N>I<Qi'_ëð}IN-<·и'/ðNи'|JJ,}I2‹i"|\/|"ëXð'|:},

Try it online!

Explanation:

ð              Push space
'_Iи           Push I underscores (I = input)
JJÂJ           Push reverse of that
UX,            Put that in X and print it
I<F            Loop from 1 to N-1 (N = index)
  '|           Push '|' (pipe)
  ðNи          Push N spaces
  '\           Push '\' (backslash)
  N>I<Qi'_     If N+1 == I-1, push _ (underscore)
    ëð}        Otherwise push space
  IN-<·и       Pop and repeat that by (I-N-1)*2
  '/           Push '/' (slash)
  ðNи          Push N spaces
  '|           Push '|' (pipe)
  JJ,          Join and print
}      
I2‹i"|\/|"     If I == 1, push '|\/|'
  ëXð'|:}      Otherwise, push X with spaces replaced with pipes
,              Print
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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