Help me Open the Box

33

4

I have an ASCII-art box and I need a program to open it.

Examples

Input:

-------
|     |
|_____|

Output:

      /
     /
    /
   /
  /
 /
/
|     |
|_____|

Specification

  • The first line will only consist of -, at least 3 of them
  • The middle rows will start with | have spaces, and end with |
    • All the middle rows will be the same
  • The last row will start with | have _ and end with a |
  • All rows will be the same length

Opening the box:

  • Each - should be replaced by a / in ascending lines and position.

Downgoat

Posted 2016-01-31T18:54:29.503

Reputation: 27 116

2As opposed to the game "Shut the Box"? – Addison Crump – 2016-01-31T19:14:18.537

Is a leading newline acceptable? – ETHproductions – 2016-01-31T19:58:11.443

@ETHproductions errr, uh.... sure, why not ¯\(ツ) – Downgoat – 2016-01-31T19:59:40.593

Can I read input from a file? – Conor O'Brien – 2016-01-31T20:17:22.233

1cough – Addison Crump – 2016-01-31T21:24:18.470

@CᴏɴᴏʀO'Bʀɪᴇɴ sure, why not ¯\(ツ)/¯; – Downgoat – 2016-01-31T21:25:04.573

28The lid becomes twice as long when opened? Oh well, blame ASCII art. – Darrel Hoffman – 2016-01-31T21:46:24.163

Can I require a training newline in the input? MATL reads each line as a separate input, so an empty line (i.e. a newline) would mark the end – Luis Mendo – 2016-01-31T22:50:43.837

1@LuisMendo yeah, just state that in your answer – Downgoat – 2016-01-31T22:51:40.667

8What's in the box? – Williham Totland – 2016-02-01T11:58:25.290

1

@WillihamTotland Something shiny

– Luis Mendo – 2016-02-01T12:23:44.263

1

This would be 13 bytes in Crayon, just beating all the other entries: '<->+'"↗/%1"r That is, match all the hyphens and store the number of matched hyphens in %1, then replace it with a string of %1 slashes pointing north-east.

– ETHproductions – 2016-02-01T14:58:23.840

Actually, that would be with implicit input, which I've decided against. Fortunately, there's a 10-byte version with explicit input: Ll`/*↗q↩Eq Mind if I post it as non-competing? – ETHproductions – 2016-02-01T16:07:35.230

@ETHproductions if there is a working interpreter and it complies with the rules then Yes. – Downgoat – 2016-02-01T16:09:39.720

2There's not a working interpreter, so I guess not then. Doesn't matter; when I do make the interpreter (soon, hopefully), I'll post the answer. – ETHproductions – 2016-02-01T16:10:07.147

@ETHproductions you can post it now I guess:) – JayCe – 2018-08-18T14:25:10.217

Answers

7

CJam, 14 bytes

l,{N'/@S*}%W%q

Try it online!

How it works

l               Read the first line from STDIN.
 ,              Compute the line's length. Result: L
  {      }%     Map; for each I in [0 ... L-1]:
                  (implicit) Push I.
   N              Push a linefeed.
    '/            Push a slash.
      @           Rotate I on top of the stack.
       S*         Turn I into a string of I spaces.
           W%   Reverse the resulting array of strings and characters.
             q  Read the remaining input from STDIN.

Dennis

Posted 2016-01-31T18:54:29.503

Reputation: 196 637

12

JavaScript ES6, 57 bytes

s=>s[r="replace"](/-+/,s=>s[r](/-/g,`
$'/`))[r](/-/g,' ')

Outputs a leading newline. Works by taking the row of -s and converting them into a triangle, then replacing the -s with spaces.

Edit: Saved 5 bytes thanks to @edc65.

Neil

Posted 2016-01-31T18:54:29.503

Reputation: 95 035

1+1 I learned something new ($'). Instead this old trick can save 4 bytes for you: f=s=>s[R='replace'](/-+/,s=>s[R](/-/g,"\n$'/"))[R](/-/g,' ') – edc65 – 2016-01-31T21:01:13.497

@edc65 Thanks, I actually make it 5 bytes; also thanks for fixing my typo (as you might have guessed I develop with \n and convert afterwards). – Neil – 2016-01-31T22:42:23.720

9

Pyth, 16 14 bytes

j+_m+*;d\/Uz.z

Explanation

   m      Uz   - [V for d in range(len(input()))]
    +*;d\/     - " "*d + "/"
  _            - ^[::-1]
j+          .z - "\n".join(^+rest_of_input())

Thanks @FryAmTheEggman for new algorithm!

Try it here.

Blue

Posted 2016-01-31T18:54:29.503

Reputation: 26 661

9

pb (NONCOMPETING), 125 bytes

^w[B!0]{>}w[B!45]{<w[B=10]{t[T+1]b[0]}}v[X]vw[T!0]{vb[124]<[X]b[124]>w[B=0]{>}t[T-1]}w[X!1]{<b[95]}<w[B!0]{^}w[Y!-1]{b[47]>^}

The version of pbi that you need to run this answer is newer than the question. It would have worked in older versions except that I never got around to allowing newlines in input. Oh well.

First, this determines the height of the box by counting newlines in the input. Once it knows that, it goes to the Y location of the right side of the box, goes down to where it needs to be and draws the walls and floor, finishing with the lid.

Check out this fun animation!

The long pause is the brush going over the input.

Ungolfed:

^w[B!0]{>}                # Go to the end of the input
w[B!45]{<                 # Head left until hitting a hyphen
    w[B=10]{                # For each newline on the way:
            t[T+1]                # Count it
            b[0]                  # Delete it
    }
}

v[X]                      # Move down as far as it is right + the number of \n
v                         # ...plus one

w[T!0]{                   # While the counting variable is nonzero:
    vb[124]                 # Go down and draw a pipe
    <[X]b[124]              # Draw a pipe on the left as well
    >w[B=0]{>}              # Go back to the right side
    t[T-1]                  # Decrement variable
}

w[X!1]{<b[95]}            # Draw the bottom of the box
<w[B!0]{^}                # Go up the left wall
w[Y!-1]{b[47]>^}          # Go up and right, drawing the lid

undergroundmonorail

Posted 2016-01-31T18:54:29.503

Reputation: 5 897

2+1 for the cool animation. How did you create it? – Gowtham – 2016-02-01T18:49:20.167

@Gowtham I would guess you record your screen and crop the video. Then, take that and convert it to a GIF. Of course, just guessing, I don't know the actual method – Spotlight – 2016-02-02T03:42:48.610

@awesomebing1 you got it – undergroundmonorail – 2016-02-02T11:47:14.027

8

Retina, 34 20 bytes

-(?=(-*))¶?
$1/¶
-
 

In the first step every - is substituted with the -'s following it, a / and a newline. The newline at the end of the original first line is deleted. In the second step we change the new -'s to spaces which results in the desired output.

Try it online here.

randomra

Posted 2016-01-31T18:54:29.503

Reputation: 19 909

2

Just for the fun of it, it's also possible in a single stage: http://retina.tryitonline.net/#code=LSg_PSgtKSopwrY_CiQjMSQqIC_Ctg&input=LS0tLS0tLQp8ICAgICB8CnxfX19fX3w (same bytes though)

– Martin Ender – 2016-02-02T12:33:27.017

If you eat the first newline separately you can use $%' to capture the trailing -s which saves you 5 bytes: Try it online!

– Neil – 2019-01-27T11:59:59.950

7

MATL, 14 15 bytes

' /'jnXyPQ)`jt

Input should have a trailing newline.

Try it online!

Explanation

' /'       % push string (will be indexed into to generate the open lid)
jn         % read first line of input and push its length
Xy         % identity matrix with that size
P          % flip vertically
Q          % add 1. Now the matrix contains 1 and 2, to be used as indices
)          % index into string. Produces a 2D char array for the lid
`          % do-while loop
  j        %   push input line
  t        %   duplicate. Truthy if nonempty
           % implicitly end loop. The loop condition is the top of the stack,
           % that is, the input line that has just been read.
           % This is truthy if non-empty; and in that case another line will
           % be read in the next iteration.
           % implicitly display stack contents, bottom to top

Luis Mendo

Posted 2016-01-31T18:54:29.503

Reputation: 87 464

5

Japt, 28 26 25 22 18 17 bytes

Ur-@"
{SpUa- -Y}/

Test it online!

Outputs a leading newline, which is acceptable according to the OP.

How it works

Ur-@       // Replace each hyphen X in the input and its index Y with this function:
"          //  Start a string that contains a newline.
{        } //  Insert here:  
   Ua- -Y  //   Take the index of the last hyphen in the input, subtract Y,
 Sp        //   and return that many spaces.
/          //  Finish off the string with a slash.

This would be 4 bytes shorter if the hinge is allowed to be on the right edge of the box:

Ur-@"
{SpY}\\

ETHproductions

Posted 2016-01-31T18:54:29.503

Reputation: 47 880

4

JavaScript (ES6), 66

b=>([a,t]=b.split`-
`,[...a+0].map(_=>(t=l+`/
`+t,l+=' '),l=''),t)

TEST

f=b=>([a,t]=b.split`-\n`,[...a+0].map(_=>(t=l+`/\n`+t,l+=' '),l=''),t)

var box = `-------
|     |
|_____|`

console.log=x=>O.textContent=x

console.log(f(box))
<pre id=O></pre>

edc65

Posted 2016-01-31T18:54:29.503

Reputation: 31 086

3

Java 8, 158 118 bytes

This is just a start, but hey, FGITWFTW.

n->{String o="";int z=n.lastIndexOf("-"),i=z;for(;i-->0;o+="/\n")for(int y=i;y-->0;o+=" ");return o+n.substring(z+2);}

Expects input as a string, returns the box.

Addison Crump

Posted 2016-01-31T18:54:29.503

Reputation: 10 763

3

Python 3, 1̶7̶0̶ 88 bytes

Here is my short(er) code: EDIT: Now 82 bytes Shorter With @Dennis 's Code Edit!

f=open('f.txt')
d=len(f.readline())-1
a=f.read()
while d:d-=1;print(' '*d+'/')
print(a)

Python 3, 421 bytes

Alternatively, just for fun, you could use one that opens it slowly:

import time
import os
f = open('f.txt', 'r')
e = f.readline()
a = f.read()
d = len(e)
c = 0
t = e + a
g = ''
clear = lambda: os.system('cls')
while c <= d - 1:
    clear()
    print(("\n" * ((d - 1) - (c))) + t)
    c += 1
    e1 = e[0:(d - c)  -1]
    e2 = e[(d - c):len(e)]
    e1 += '/'
    e2 = ' ' * len(e2)
    y = (' ' * len(e1)) + '/' + '\n'
    g += y
    t = (g + e1 + e2 + '\n' + a)[d:len(g + e1 + e2 + '\n' + a)]
    time.sleep(0.2)
f.close()

To use either, you must create a text file in the same directory containing an ascii box of any width or depth called 'f.txt'. It will then open that box.

Monster

Posted 2016-01-31T18:54:29.503

Reputation: 69

1

You can shorten this further by shortening variables to single letters and removing a lot of the whitespace between operators. For general tips for golfing in Python, see here.

– Alex A. – 2016-01-31T23:08:11.133

Why on earth do you need time? This question only asks for a single output. – Addison Crump – 2016-01-31T23:19:44.513

Like I said, my interpretation of this puzzle was slightly off and if you output it it'll show you more than the question asked for. – Monster – 2016-01-31T23:21:06.193

Okay, I have added a simplER answer which does exactly what the question says as I now understand it. It's not pretty but it works. My alternate code is for anyone awesome enough to want to watch it open – Monster – 2016-01-31T23:59:48.963

2A few minor changes bring your byte count down to 81 (reading from STDIN). – Dennis – 2016-02-01T01:01:26.590

Thanks Dennis. The thing is i didn't understand the question in the first place as I thought it was asking for every stage (hence the second code which im sure could be condensed also). I appreciate the improvement. – Monster – 2016-02-01T01:11:43.937

Feel free to incorporate my suggestions in your post. Collaboration is quite common on PPCG. (I didn't see your reply earlier. If you include @Dennis, I will get a ping.) – Dennis – 2016-02-01T03:26:48.923

@Dennis, thanks! also, I thought so but wasn't sure – Monster – 2016-02-01T16:41:22.337

3

Bash, 85 84 79 characters

(Pure Bash version, no external commands used.)

r(){
a="${a/-
/
$s/
}"
s+=\ 
[[ $a = -* ]]&&r
}
mapfile a
r
IFS=
echo "${a[*]}"

Outputs a leading newline.

Sample run:

bash-4.3$ bash open-the-box.sh <<< $'-------\n|     |\n|_____|'

      /
     /
    /
   /
  /
 /
/
|     |
|_____|

manatwork

Posted 2016-01-31T18:54:29.503

Reputation: 17 865

echo is an external command - /usr/bin/echo ;) – Levi – 2016-02-03T01:12:45.343

The echo executable exists for the operating system's conformance with the standards. Nowadays that one is only used if portability is important, as that one is up to the standard, But most modern shells have their own builtin echo which is used by default: http://pastebin.com/RnxhweBv @Levi, if you rename/move your /usr/bin/echo, my code will still work.

– manatwork – 2016-02-03T09:25:59.167

(it was a joke....) – Levi – 2016-02-04T05:31:09.597

1Oh. Ok. Sorry, I already met people earlier who claimed the same, but seriously. – manatwork – 2016-02-04T08:43:48.923

3

Perl, 61 54 33 + 3 = 36 characters

s^-^" "x(length$')."/\n"^ge&chomp

Run it as

perl -ple 's^-^" "x(length${chr 39})."/\n"^ge&chomp' closed_box_file

Each - in first line is replaced by a string that is a result of concatenation of some number of , / and \n. ${chr 39} evaluates to perl's (in)famous $' aka $POSTMATCH special variable. Lastly, chomp gets rid of the trailing newline character that was added for the last - character.

Thanks to @manatwork for saving 7 + more characters.

Bonus - s^-^" "x$i++."\\\n"^ge&&chop opens the box from the right edge in 29 + 3 characters :). Run it as:

gowtham@ubuntu:~$ cat a
----
|  |
|__|
gowtham@ubuntu:~$ perl -plE 's^-^" "x$i++."\\\n"^ge&&chop' closed_box_file
\
 \
  \
   \
|  |
|__|

Gowtham

Posted 2016-01-31T18:54:29.503

Reputation: 571

@manatwork Only first line contains -, so yes, I can golf it even more. Thanks! – Gowtham – 2016-02-01T19:12:10.990

$.==1$.<2, &&chop&chop, remove the extra pair of parenthesis around length, count {chr 39} as 1, as it not is only needed by the command line version due to shell's syntax: $.<2&&s^-^" "x(length$')."/\n"^ge&chop + 2 character for command line options = 40 according to my counting. http://pastebin.com/iDhUs9XX – manatwork – 2016-02-01T19:17:37.143

@manatwork Actually, $.==1 or $.<2 can be eliminated because only first line contains - – Gowtham – 2016-02-02T06:54:38.347

Yes, I saw what you did there. And I was amazed. By the way, you can use a literal line wrap in the code instead of \n. – manatwork – 2016-02-02T08:53:25.403

Doh. Found a shorter one: s^-^$'=~y/-/ /r."/\n"^ge&chomp – manatwork – 2016-02-03T19:05:34.143

2

Canvas, 6 4 bytes

jL/o

Try it here!

Explanation:

j      remove 1st line of the input
 L     get the width of the remaining input
  /    push a diagonal of that size
   o   and output that diagonal
       and implicitly output the remaining input

dzaima

Posted 2016-01-31T18:54:29.503

Reputation: 19 048

2

Pyth, 26 23 bytes

jXK.z0jm+*\ t-lhKd\/lhK

Yuck. Can definitely be shorter; still working on it.

Doorknob

Posted 2016-01-31T18:54:29.503

Reputation: 68 138

2

Python3, 76 bytes

f=open(0)
w=len(f.readline())
while w:w-=1;print(' '*w+'/')
print(f.read())
  1. Get the length of the first input line.
  2. Print lines of / preceded by a decreasing number of spaces.
  3. Push the rest of stdin straight to stdout.

EDIT: I've just noticed that my code is almost identical to @Dennis' comment edit of @Monster's shorter Python3 code, the only difference being print the remainder of stdin directly instead of store it in a variable. Great minds!

josh2112

Posted 2016-01-31T18:54:29.503

Reputation: 151

1

JavaScript (Node.js), 56 bytes

a=>a[b="replace"](/-+/,c=>c[b](d=/-/g,`
$'/`))[b](d,' ')

Try it online!

Should be written as a comment of @Neil's answer but I can't create comments yet

Any3nymous user

Posted 2016-01-31T18:54:29.503

Reputation: 539

Hello and welcome to PPCG. I assume you mean Neil's answer which you have golfed further. You may want to add a link to their answer and correct the author's name to give credit.

– Jonathan Frech – 2018-08-18T18:23:37.537

@JonathanFrech thanks, done – Any3nymous user – 2018-08-18T18:57:23.470

1

05AB1E (legacy), 9 bytes

g'/1.Λ|»»

Try it online! (legacy-only)

How it works

g'/1.Λ|»» – Full program. Takes input from STDIN.
g         - Length. Only takes the first line into account.
 '/       – Push a slash character, "/".
   1.Λ    – And diagonally up-right, draw a line of slashes of the given length.
      |»  – Push the remaining inputs (all other lines) joined on newlines.
        » – Then join the stack on newlines.

Mr. Xcoder

Posted 2016-01-31T18:54:29.503

Reputation: 39 774

1

Charcoal, 14 bytes

↙L§⪪θ¶⁰M→✂⪪θ¶¹

Try it online (verbose) or try it online (pure).

Explanation:

Split the input by newlines, take the length of the first line, and print a line of that length from the Top-Right to Down-Left:

Print(:DownLeft,Length(AtIndex(Split(q,"\n"),0)))
↙L§⪪θ¶⁰

Move once to the right:

Move(:Right)
M→

Split the input by newlines again, and remove the first item, and print what's left implicitly:

Slice(Split(q,"\n"),1)
✂⪪θ¶¹

(NOTE: Putting the input split by newlines in a variable (since I do it twice above) is 1 byte longer also 14 bytes by using a slightly different method (thanks to @Neil):
≔⮌⪪θ¶θ↙L⊟θM→⮌θ Try it online (verbose) or try it online (pure)).

Kevin Cruijssen

Posted 2016-01-31T18:54:29.503

Reputation: 67 575

If you reverse the input split by newlines you can pop off the first line which then brings you back down to 14 bytes: Try it online!

– Neil – 2019-01-27T12:03:31.407

1

Python 2, 100 bytes

def o(b):
 m=b.split('\n')[1:]
 print"\n".join(["/".rjust(i)for i in range(len(m[0]),0,-1)]+m)

Defines a function o that takes a string as its input. (Full program wasn't specified in the question).

Scimonster

Posted 2016-01-31T18:54:29.503

Reputation: 2 905

1

PowerShell, 55 bytes

$d,$b=$args-split"`n";($d.length-1)..0|%{" "*$_+"/"};$b

Takes input $args as a string, -splits on newlines `n (reference link), stores the first line into $d (as a string) and the remaining into $b (as an array of strings). We then loop from the length of the first line (minus 1) to 0 and each iteration output that number of spaces plus a /. Finally, output $b (the rest of the input string) which by default will output one per line.

Example Run

PS C:\Tools\Scripts\golfing> .\help-me-open-the-box.ps1 "----`n|  |`n|__|"
   /
  /
 /
/
|  |
|__|

AdmBorkBork

Posted 2016-01-31T18:54:29.503

Reputation: 41 581

Split on the linefeed instead of `n, 54 bytes – Veskah – 2019-01-29T23:38:24.280

0

K (ngn/k), 18 bytes

{(" /"@|=#*x),1_x}

Try it online!

ngn

Posted 2016-01-31T18:54:29.503

Reputation: 11 449

0

05AB1E (legacy), 12 bytes

€ðƶ€¦'/«R|«»

Try it online!

Mr. Xcoder

Posted 2016-01-31T18:54:29.503

Reputation: 39 774

0

brainfuck, 118 bytes

-->+[<+>,----------]>>>++++[<++++++++>-]>-[<+>-----]++++++++++<----<<<<[[>+>+<<-]>[<+>-]>[>.<-]>>.>.[<]<<-]>>>>.>.[,.]

Try it online!

(You will have to check the "!" box to allow for everything after the ! to be considered input)

It is the same length as the Java answer, so I'm happy. Any help golfing it to be shorter than the Java answer would be greatly appreciated.

Explanation

-->+[<+>,----------]                                 Counts the number of characters until a linefeed
>>>++++[<++++++++>-]>-[<+>-----]++++++++++<----<<<<  Initialises the next 3 cells to 32 (space), 47 (/) and 10 (linefeed)
[                                                    Starts a loop on the length of the first line
[>+>+<<-]>[<+>-]>[>.<-]>>.>.[<]<<-                   Prints a space for every character and then prints a slash, a linefeed and then takes one from the number of characters, and repeats until that value is equal to zero
]                                                    Ends the loop
>>>>.>.[,.]                                          Prints the last slash, a newline and then the rest of the input

FinW

Posted 2016-01-31T18:54:29.503

Reputation: 477

0

Brain-Flak, 144 bytes

({()<{}({}[((()()){}()){}])>}{}){(({}[()])<>){({}[()]<(((((()()){}){}){}){})>)}{}(((((()()()){}){}){}){}[()])(((()()){}()){})<>}{}<>{({}<>)<>}<>

Try it online!

The longest answer yet.

Explanation

({()<{}({}[((()()){}()){}])>}{}) #count the dashes "n"
{ #while N != 0
  (({}[()])<>) #copy --N to second stack for the next line
  {({}[()]<(((((()()){}){}){}){})>)}{} #indent
  (((((()()()){}){}){}){}[()]) # '/'
  (((()()){}()){}) # newline
  <>
}{}
<>{({}<>)<>}<> #copy output to main stack

MegaTom

Posted 2016-01-31T18:54:29.503

Reputation: 3 787

0

JavaScript ES6, 106 bytes

q=>(q=q.split`
`,n=q[0].length,eval('for(i=0,r="";i<n;i++)r+=" ".repeat(n-i-1)+"/\\n"'),r+q.slice(1).join`
`)

Simple enough: getting the length of the first line, creating a spaced-triangle with trailing /, and adding that to the original, sliced and joined.

Test it out! (ES6 only :()

F=q=>(q=q.split`
`,n=q[0].length,eval('for(i=0,r="";i<n;i++)r+=" ".repeat(n-i-1)+"/\\n"'),r+q.slice(1).join`
`)
function n(){q.innerHTML=F(o.value)}
o.onkeydown=o.onkeyup=o.onchange=o.onkeypress=n;
n();
*{font-family:Consolas,monospace;}textarea{width:100%;height:50%;}#q{white-space:pre;}
<textarea id=o>----
|  |
|__|</textarea><div id=q>

Conor O'Brien

Posted 2016-01-31T18:54:29.503

Reputation: 36 228

2

I see you solved the XKCD problem using formatting. Clever.

– Kroltan – 2016-02-02T12:10:49.433

.repeat(n-i-1) => .repeat(n+~i) – Downgoat – 2018-08-21T04:18:40.670

0

PHP, 127 characters

$s=$argv[1];$l=strlen(strtok($s,"\n"));for($i=0;$i<$l;$i++)$s=preg_replace("/-/","\n".str_repeat(" ",$l-$i-1)."/",$s,1);echo$s;

Ungolfed version :

$s=$argv[1];
$l=strlen(strtok($s,"\n"));

for($i=0;$i<$l;$i++){
    $v="\n".str_repeat(" ",$l-$i-1)."/";
    $s=preg_replace("/-/",$v,$s,1);
}
echo $s;

kuldeep.kamboj

Posted 2016-01-31T18:54:29.503

Reputation: 625

There is a typo in your code: you missed the sigil of $argv. There are a couple of minor tricks you could apply: $l=strlen(strtok($s=$argv[1],"↵"));while($l)$s=preg_replace("/-/","↵".str_repeat(" ",--$l-$i)."/",$s,1);echo$s; (Use a literal newline in your code where is “↵”: http://pastebin.com/36t2fb0P )

– manatwork – 2016-02-01T17:03:04.413

0

Python 2.7, 120 122 chars

Needs a file f with the original/closed box, output is the opened one. Cheers to @Monster for the idea... will try to figure out multi-line input later and see if it's shorter.

for l in open('f').readlines():
 if l[1]==('-'):
  for x in range(1,len(l)):print(' '*(len(l)-x+1)+'/')
 else:print l[:-1]

Edit

  • just noticed that the leftmost / has a space in front; +2 bytes

janrn

Posted 2016-01-31T18:54:29.503

Reputation: 51

0

Ruby, 59 characters

(57 characters code + 2 characters command line options.)

s=""
$_=$_.chars.map{(s<<" ")[1..-1]+?/}.reverse*$/if$.<2

Sample run:

bash-4.3$ ruby -ple 's="";$_=$_.chars.map{(s<<" ")[1..-1]+?/}.reverse*$/if$.<2' <<< $'-------\n|     |\n|_____|'
      /
     /
    /
   /
  /
 /
/
|     |
|_____|

manatwork

Posted 2016-01-31T18:54:29.503

Reputation: 17 865

0

Gema, 51 49 31 characters

-\P/-+/=@subst{-=\\ ;$1}/\n
-=/

Sample run:

bash-4.3$ gema -e '-\P/-+/=@subst{-=\\ ;$1}/\n;-=/' <<< $'-------\n|     |\n|_____|'
      /
     /
    /
   /
  /
 /
/
|     |
|_____|

manatwork

Posted 2016-01-31T18:54:29.503

Reputation: 17 865

0

Bash, 129 characters

Requires a file called a with the closed box, outputs to stdout.

for i in $(seq `cat a|awk 'NR==1{print length($1)-1}'` -1 1);{ for j in `seq 1 $i`;{ printf " ";};echo "/";};echo "/";tail -n2 a

It might be possible to make it shorter by using sed and using stdin and piping.

Daniel Peukert

Posted 2016-01-31T18:54:29.503

Reputation: 1

Nice first golf answer. Some simple syntax change suggestions: for i in $(seq \awk 'NR<2&&$0=length-1' a` -1 1);{ for j in `seq 1 $i`;{ printf \ ;};echo /;};echo /;tail -n2 a` – manatwork – 2016-02-01T15:13:12.307

0

Awk, 47 46 characters

(44 characters code + 2 characters command line option.)

/-/{OFS=RS;for(i=NF;i;i--){$i=s"/";s=s" "}}1

Sample run:

bash-4.3$ awk -F '' '/-/{OFS=RS;for(i=NF;i;i--){$i=s"/";s=s" "}}1' <<< $'-------\n|     |\n|_____|'
      /
     /
    /
   /
  /
 /
/
|     |
|_____|

manatwork

Posted 2016-01-31T18:54:29.503

Reputation: 17 865

0

Python, 125 bytes (110 without box)

i="\n---\n| |\n|_|"
l,b,r=i.count("-"),i.split('\n'),range
for x in r(1,l):print" "*(l-x)+"/"
for x in r(2,len(b)):print b[x]

If anyone has any idea how to shorten it, please let me know!

Dave Lin

Posted 2016-01-31T18:54:29.503

Reputation: 71