><>, 106 104 Bytes
I get the feeling that ><> may not be the best language for this, but I've come too far to give up and not post this. The *
at the end of line 4 is supposed to be a space. Don't you love how incredibly grotesque this code looks? Try it online.
<v?(0:i
v>~" ## "}}l:::
>"#"o1-:?!v02.>~a"#"oo
"-2ooa"#"~<.31v!?:-1o"
7v?=3loroo"#"a<.4
.>";^"e3pa2p093
Here's a version without anything but direction changers to give an idea of how the pointer moves (note that I've left out the "teleport" statements, i.e. .
).
Direction flow:
<v
v>
> v >
< v
v <
>
Explanation
My visualization of the stack will be based off of the input input
. ><> is a two dimensional language, so pay attention to where the pointer is moving between lines, as it executes code underneath it (in this code <>v^
are primarily used to change direction). I'll be starting my explanations from where the pointer starts. Note that there will be two lines repeated, as the pointer moves backwards after the fifth line.
What I always find cool about ><> is its ability to modify its own source code, and I make use of it in this program. Lines 3 and 4 are reused to print the last two lines through a modification of a character in each.
Line 1 : Input loop
<v?(0:i
< change direction to left
(0:i checks if input is less than 0 (no input defaults to -1)
v? change direction to down if so
Stack: [-1,t,u,p,n,i]
Line 2: Generates third line of output
v>~" ## "}}l:::
>~" ## "}} remove -1 (default input value) from stack and pads with # and spaces
l::: push 4 lengths of padded input
Stack: [9,9,9,9,#, ,t,u,p,n,i, ,#]
Line 3: Prints first line of output
>"#"o1-:?!v02.>~a"#"oo
>"#"o print "#"
1- subtract 1 from length (it's at the top of the stack)
:?!v move down if top of stack is 0
Stack: [0,9,9,9,#, ,t,u,p,n,i, ,#]
Output:
#########
Line 4: Prints second line of output
"-2ooa"#"~<.31v!?:-1o"*
-2ooa"#"~< pops 0, prints newline, "#", then decrements length by 2
" o"* prints space (* is supposed to be space char)
-1 decrements top of stack
.31v!?: changes direction to down if top of stack is 0, else jumps back to "
Stack: [0,9,9,#, ,t,u,p,n,i, ,#]
Output (*
represents space):
#########
#*******
Line 5: Prints third line of output
7v?=3loroo"#"a<.4
oo"#"a< prints "#",newline
r reverses stack
7v?=3lo .4 outputs until stack has 3 values, then changes direction to down
Stack: [9,9,0]
Output:
#########
# #
# input #
Line 6: Sets itself up to print fourth and fifth lines of output
.>";^"e3pa2p093
>";^" push ";",then "^"
e3p place "^" as the fifteenth character on line 4
a2p place ";" as the eleventh character on line 3
0 push a value (value doesn't matter -- it will be deleted)
. 93 jump to the tenth character on line 4
Stack: [0,9,9,0]
Line 4: Print fourth line of output
"-2ooa"#"~<.31^!?:-1o"*
ooa"#"~< delete 0 (unnecessary value pushed), then print newline,"#"
-2 subtract two from value on top of stack (length)
" .31^!?:-1o"* print space until top of stack is 0, then change direction to up
Stack: [0,9,0]
Output (*
represents space):
#########
# #
# input #
#*******
Line 3: Print last line of output
"#"o1-:?!;02.>~a"#"oo
>~a"#"oo pop top of stack, print "#", newline
"#"o1-:?!;02. print "#" until top of stack is 0, then terminate
Stack: [0,0]
Output:
#########
# #
# input #
# #
#########
The same challenge as editor golf. – Martin Ender – 2015-09-13T12:44:29.627
@MartinBüttner Not exactly the same, different input format, this requires spacing all around the phrase. Also not language-restricted. – orlp – 2015-09-13T12:50:31.660
@orlp Well this task is a subset of this challenge.
– mınxomaτ – 2015-09-13T12:54:11.0871
Not so different: http://codegolf.stackexchange.com/questions/57442/show-tree-rings-age/57470#57470
– edc65 – 2015-09-13T13:40:54.1531The input string won't contain linebreaks, right? – flodel – 2015-09-13T13:51:23.480
2@edc65 I disagree, this challenge is hugely different – Beta Decay – 2015-09-13T13:55:40.757
@BetaDecay not a duplicate, just similar – edc65 – 2015-09-13T14:02:11.713
@edc65 Meh, debatable :) – Beta Decay – 2015-09-13T14:02:44.630
10I would recommend to wait at least a week before accepting an answer. While it doesn't really matter if you are planning to update the accepted answer if a shorter submission comes in, there will be people complaining about an early accepted answer, or even downvote it. There will also be some people who won't be interested in posting an answer if there is already an accepted one. – Martin Ender – 2015-09-13T17:36:16.793
What about strings like s̄trin̂gs with 8 code points but only 6 columns, or strings like wide with 8 columns but only 4 code points? What about tabs? – tchrist – 2015-09-13T23:35:38.090