# Create a square of increasing size by replicating the initial code

Your assignment is to write a program of even length, that prints an ASCII-art square (described below), that increases its side length by 1 unit each time the original source code is pasted in the middle of the current code.

It is quite hard for me to define this task very well, so I'll give you an example:

• Let's say your initial code was CODE and that it printed:

0

• Then, insert CODE in the middle: your code becomes COCODEDE and it should print:

00
00

• Re-insert CODE in the middle: your code becomes COCOCODEDEDE and should print:

000
000
000

• And so on. Your answer should theoretically work after any number of iterations, but I understand if, due to language performance limitations, it cannot run reasonably over a certain threshold.

Some rules:

• You can use any printable ASCII (32-127) as the character to use for your square. Your choice needs to be constant (You should use the same character for each iteration).

• The initial output square must have side-length 1.

• An ascii-art square is defined as a string with N lines (separated by N-1 linefeeds / newlines), and with each line containing N copies of the chosen character.

• Your output isn't allowed to contain any extraneous whitespace, other than a trailing newline.

• You can use the defaults for input and output (programs or functions are allowed, but snippets are not).

• The middle of your code is defined as the point where the source code can be split in two parts such that the two are equal.

• Your answers will be scored by the length of your original program, in bytes. The lowest byte count wins. In case there's a tie, the answer that was submitted earlier wins.

• You can use this program to apply the insertions without having to do that by hand.

• I must admit I was inspired by this quine question posted earlier. If people think it is too close, I'll happily delete this. Also excuse me if I made any mistakes, I am still not too experienced with the rules here. :) – user77954 Feb 2 '18 at 12:15
• Welcome to PPCG! I suggest using the Sandbox for your future challenges. – user202729 Feb 2 '18 at 12:17
• Welcome to the site! Excellent use of another challenge for inspiration without falling into the dupe trap :) – Shaggy Feb 2 '18 at 13:31
• Your helper program doesn't work for programs with multiple lines. How about this modified version from the other question? – Jo King Feb 3 '18 at 0:09
• @user77954 But my brainfuck code is shorter than your python :( (has anyone ever said that before?) – Jo King Feb 3 '18 at 11:33

# Pyth, 2 bytes


5


### How does that work?

\n is the command that prints its argument with a trailing newline, while returning it simultaneously. So, each time you make an insertion, you turn the integer literal 5 into a number containing N copies of 5 concatenated, and the leading newlines basically make sure it's printed the appropriate number of times, thus keeping it square.

• Holy frick that's short... – ETHproductions Feb 2 '18 at 12:28
• Proof of optimality (:P): Since the byte counts must be even, and cannot be negative, the minimum possible byte count is 0 bytes. There is exactly 1 program of 0 bytes, which doesn't fulfil the task. Therefore, 2 bytes is optimal. – Mr. Xcoder Feb 3 '18 at 20:20
• Everyone (especially HNQ voters), go upvote other answers too and avoid the FGITW effect. – user202729 Feb 4 '18 at 9:38

## JavaScript (ES6), 4232 30 bytes

s=[this.s]+0;  console.log(s);


Second iteration:

s=[this.s]+0;  s=[this.s]+0;  console.log(s);console.log(s);


This works by appending a 0 to s each time the first half of the code is run, and printing s itself each time the second half is run. Takes advantage of four quirks of JavaScript:

1. The current environment can be referred to with this. This allows us to do this.s in place of s.
2. When accessing a property that has not been defined on an object, instead of throwing an error, JavaScript returns undefined.
3. An array plus a number returns a string. [1,2,3] + 4 === "1,2,34"
4. When stringifying an array, undefined is converted to the empty string, which means that [undefined] + 0 === "0".

Put together, this means that we can express the first half (generating a string of zeroes) in just 13 bytes. If using alert instead of console.log is allowed, we can save 4 more bytes by shortening the second half.

• Congrats, passes the tests I have made! – user77954 Feb 2 '18 at 12:36
• ... Ingenious! :) – Shaggy Feb 2 '18 at 12:39

# Python 2, 4238 28 bytes

id='%s@'%id  ;print id[22:];


Try it online!. You can also try the 2nd and 3rd iterations

• Uh... character, not string. – user202729 Feb 2 '18 at 12:33
• @user202729 thanks for the heads up, fixed =] – Rod Feb 2 '18 at 12:36

# 05AB1E, 2 bytes

5=


Try it online!

• Congrats, passes the tests I have made! – user77954 Feb 2 '18 at 12:36
• TFW you're really over-complicating things and see this answer. – Magic Octopus Urn Feb 2 '18 at 15:50

# Python 2, 22 bytes

i=0;i+=1; i
print'*'*i


Try it online!

Doubled:

i=0;i+=1; ii=0;i+=1; i
print'*'*i
print'*'*i


Note that the second half starts with a newline character.

# C (gcc), 170168968072 70 bytes

Much shorter version. Still wish I could find a solution without the preprocessor.

i;main(n){for(;i++<n;)printf
#if 0

#endif
(" %*c",n=__LINE__/4, 10);}


Try it online!

Old 168 byte version:

#ifndef A
#define A p(c){putchar(c);}j,n;main(i){for(
#else
#define A n++,
#endif
A

#ifndef B
#define B i=++n;i--;p(10))for(j=n;j--;)p(64);}
#else
#define B
#endif
B


Try it online!

• – user202729 Feb 4 '18 at 9:40
• @user202729 ah, yes. Thought I fixed a typo but introduced a bug. Reverting. – gastropner Feb 4 '18 at 9:41

# Python 2, 30 bytes

False+=1      ;print'*'*False;


Try it online!, 2nd and 3rd iteration

This makes use of the fact that bools in Python are basically ints and the names False and True were reassignable in Python 2.

# Python 1, 32 bytes

exit=exit+'*'  ;print exit[30:];


Try it online!, 2nd and 3rd iteration

In Python 1 the builtin strings exit and quit existed to inform the user of the interactive shell how to exit it. The default value is "Use Ctrl-D (i.e. EOF) to exit.".

• I was going to suggest n=False+=1;print'*'*n;, but I keep forgetting that that's not a Python feature... – ETHproductions Feb 2 '18 at 16:15

let s@(z:n)="0\n"in case lines<$>[]of(h:t):_->(h:h:t)>>(z:h++n);_->s  Try it online once, twice or thrice. Because of Haskell's laziness an expression like the one above counts as a function which takes no arguments, as per this Meta question. # Charcoal, 6 bytes ⊞υωＬυ⸿  Try it online! Explanation:  ω Predefined empty string (any variable would do here) υ Predefined initially empty list ⊞ Push  υ ends up with a length of the number of repetitions.  υ List Ｌ Length Implicitly print as a row of -s ⸿ Move to start of next line  # brainfuck, 44 34 bytes crossed out 44 is still regular 44 ;( ,>-[<+>---]++++++++++[<]>[.>]<----  Try it online! Try it doubled, tripled. Look, no padding! Prints squares of U. It splits right down the middle of the 10 +s. # Brain-Flak, 74 bytes (((((()()()){}){}){}){})((()()()()()<>){})<>([]){({}[()]<(({})<>)<>>)}{}<>  Try it online! Try it doubled and tripled. ### Explanation (((((()()()){}){}){}){}) # push 48 ("0") onto first stack ((()()()()()<>){}) # push 10 (\n) onto second stack <>([]){({}[()]< # a number of times equal to the height of the first stack: (({})<>)<> # copy the top of the first stack to the second stack >)}{}<> # cleanup and return to second stack  The break point is in the middle of the <> in the "push 10" section. Breaking this up will leave a 5 on the third stack until we reach the corresponding second half, at which point pushing 10 will resume right where it left off. While it is possible to push a printable ASCII value (space) in 22 bytes, this would make the central <> be executed after pushing 5. By adding two more bytes, I was able to move the <> so that all of the progress toward pushing 10 was on the third stack. As a bonus, this also made the resulting square more aesthetically pleasing. • – user202729 Feb 4 '18 at 9:30 # Ruby, 18 bytes $/=?x+$/; puts$/;


# tinylisp, 112 bytes

(load library) (d N((q((x)(i x(inc x)1)))(v(h(t(t(h(t(q())))))))))(join(repeat-val(string(repeat-val 42 N))N)nl)


Try it online! Also doubled and fivefold.

The "build a string in the first half, print it in the second half" approach that a lot of languages are taking won't work in tinylisp, since there are no mutable variables. Instead, we do some serious code nesting.

When a second copy of the code is inserted, it is placed inside the (q()), which wraps it in a list. Then (h(t(t(h(t(...)))))) drills into that list to the part after (d N. (v(...)) evaluates it; then we pass it to the unnamed function (q((x)(i x(inc x)1))), which increments the resulting value if it's a number and returns 1 if it's the empty list. The final result in the outermost nested version of the code is assigned to N. In essence, we've set up a weird sort of recursion that counts the number of nesting levels.

The second half of the code then creates a string of N asterisks, then a list of N such strings, then joins the list on newlines. The result is displayed with a trailing newline.

# R, 44 bytes

F=F+1;T=TRUE*TRUE+12;
write(strrep(1,F),"");


Try it online!

Prints with a trailing newline. The T=TRUE*TRUE+12 is just to pad the length.

• You can eliminate 2 bytes by deleting the semicolons. I suppose there is a space at the end of the first line, which you can replace with a #: F=F+1;T=TRUE*TRUE+12# <newline> write(strrep(1,F),"") – Andreï Kostyrka Feb 4 '18 at 23:01
• @AndreïKostyrka that would be 43 bytes which isn't even, unfortunately. – Giuseppe Feb 5 '18 at 16:24

# Julia 0.6, 29 bytes

All my ideas were longer than adapting xnor's clever python solution.

i=0;i+=1;    i
println("0"^i)


Becomes

i=0;i+=1;    ii=0;i+=1;    i
println("0"^i)
println("0"^i)


Try it online!

# SNOBOL4 (CSNOBOL4), 130 68 bytes

Now with no comments! See the edit history for an explanation of the old algorithm.

	X =X + 1
A =ARRAY(X,DUPL(1,X));
I	I =I + 1
OUTPUT =A<I>	:S(I)
END


Try it online!

Try it doubled and tripled

Explanation:

	X =X + 1		;* increment X
A =ARRAY(X,DUPL(1,X));	;* create an x-length array with 1 repeated x times for each element
I	I =I + 1		;* for i < x
OUTPUT =A<I>	:S(I)	;* output a[i]
END


Because an END label is required and anything after the first END label is ignored, we get two advantages for this challenge:

• operations in the first half of the program are repeated X times for the X repetitions
• there will (to the interpreter) only exist one copy of the second half, including labels.

This suggests that we use the repetition for the first half, and then we can use a more "conventional" labeling approach to repeat the output X times.

The first half is

	X =X + 1
A =ARRAY(X,DUPL(1,X));

which, when repeated, increments X the appropriate number of times, and creates an ARRAY A with indices from 1 to X and where each element of A is the string 1 repeated X times.

Then no matter how many times the program is repeated, the interpreter only sees:

I	I =I + 1
OUTPUT =A<I>	:S(I)
END


which is a typical SNOBOL program that prints out the elements of A one at a time until the index goes out of bounds, then terminates the program.

; is an optional line terminator usually reserved for one-line EVAL or CODE statements that quite neatly brings the byte count to 68 and marks the halfway point, allowing the code to be appended there.

# shortC, 56 44 bytes

-12 bytes: wait duh I'm using shortC why not use some shortened C stuff

s[];A
strcat(s,"@");//
Js);/*filling space*/


I would've used standard C, but that requires a } at the end which messes with replication. shortC inserts it at EOF implicitly.

# Perl 5, 25 bytes

24 bytes code + 1 for -p.

Appreciate that you might not want to allow commandline flags, please let me know if this is not valid.

$@=0 x++$n;;$_="$@
"x\$n;


Try it online!

• – user202729 Feb 4 '18 at 9:30