><>, 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 #
# #
#########