J'ai une boîte ASCII-art et j'ai besoin d'un programme pour l'ouvrir.
Exemples
Contribution:
-------
| |
|_____|
Sortie:
/
/
/
/
/
/
/
| |
|_____|
spécification
- La première ligne ne comportera que
-
3 au moins - Les rangées du milieu commencent par
|
des espaces et se terminent par|
- Toutes les lignes du milieu seront les mêmes
- La dernière rangée commencera par
|
avoir_
et se terminera par un|
- Toutes les lignes auront la même longueur
Ouverture de la boîte:
- Chacune
-
doit être remplacée par une ligne/
ascendante et une position.
Réponses:
CJam, 14 bytes
Try it online!
How it works
la source
JavaScript ES6, 57 bytes
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.
la source
f=s=>s[R='replace'](/-+/,s=>s[R](/-/g,"\n$'/"))[R](/-/g,' ')
\n
and convert afterwards).pb (NONCOMPETING), 125 bytes
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:
la source
Pyth,
1614 bytesExplanation
Thanks @FryAmTheEggman for new algorithm!
Try it here.
la source
Retina,
3420 bytesIn 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.
la source
$%'
to capture the trailing-
s which saves you 5 bytes: Try it online!MATL, 14
15bytesInput should have a trailing newline.
Try it online!
Explanation
la source
Japt,
282625221817 bytesTest it online!
Outputs a leading newline, which is acceptable according to the OP.
How it works
This would be 4 bytes shorter if the hinge is allowed to be on the right edge of the box:
la source
JavaScript (ES6), 66
TEST
la source
Java 8,
158118 bytesThis is just a start, but hey, FGITWFTW.
Expects input as a string, returns the box.
la source
Python 3, 1̶7̶0̶ 88 bytes
Here is my short(er) code: EDIT: Now 82 bytes Shorter With @Dennis 's Code Edit!
Python 3, 421 bytes
Alternatively, just for fun, you could use one that opens it slowly:
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.
la source
Bash,
858479 characters(Pure Bash version, no external commands used.)
Outputs a leading newline.
Sample run:
la source
echo
is an external command -/usr/bin/echo
;)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 builtinecho
which is used by default: pastebin.com/RnxhweBv @Levi, if you rename/move your/usr/bin/echo
, my code will still work.Perl,
615433 + 3 = 36 charactersRun it as
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:la source
-
, so yes, I can golf it even more. Thanks!$.==1
→$.<2
,&&chop
→&chop
, remove the extra pair of parenthesis aroundlength
, 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. pastebin.com/iDhUs9XX$.==1
or$.<2
can be eliminated because only first line contains-
\n
.s^-^$'=~y/-/ /r."/\n"^ge&chomp
Pyth,
2623 bytesYuck. Can definitely be shorter; still working on it.
la source
Python3, 76 bytes
/
preceded by a decreasing number of spaces.stdin
straight tostdout
.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!la source
Canvas,
64 bytesTry it here!
Explanation:
la source
Python 2, 100 bytes
Defines a function
o
that takes a string as its input. (Full program wasn't specified in the question).la source
PowerShell, 55 bytes
Takes input
$args
as a string,-split
s 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 thelength
of the first line (minus 1) to0
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
la source
JavaScript (Node.js), 56 bytes
Try it online!
Should be written as a comment of @Neil's answer but I can't create comments yet
la source
05AB1E (legacy), 9 bytes
Try it online! (legacy-only)
How it works
la source
Charcoal, 14 bytes
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:
Move once to the right:
Split the input by newlines again, and remove the first item, and print what's left implicitly:
(NOTE: Putting the input split by newlines in a variable (since I do it twice above) is
1 byte longeralso 14 bytes by using a slightly different method (thanks to @Neil):≔⮌⪪θ¶θ↙L⊟θM→⮌θ
Try it online (verbose) or try it online (pure)).la source
JavaScript ES6, 106 bytes
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
:(
)Show code snippet
la source
.repeat(n-i-1)
=>.repeat(n+~i)
Python 2.7,
120122 charsNeeds 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.Edit
/
has a space in front; +2 bytesla source
Ruby, 59 characters
(57 characters code + 2 characters command line options.)
Sample run:
la source
Bash, 129 characters
Requires a file called
a
with the closed box, outputs to stdout.It might be possible to make it shorter by using
sed
and using stdin and piping.la source
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
PHP, 127 characters
Ungolfed version :
la source
$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 “↵”: pastebin.com/36t2fb0P )Python, 125 bytes (110 without box)
If anyone has any idea how to shorten it, please let me know!
la source
Awk,
4746 characters(44 characters code + 2 characters command line option.)
Sample run:
la source
Gema,
514931 charactersSample run:
la source
K (ngn/k), 18 bytes
Try it online!
la source
05AB1E (legacy), 12 bytes
Try it online!
la source