Create a square of increasing size by replicating the initial code

45

5

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.

user77954

Posted 2018-02-02T12:14:49.237

Reputation:

1

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. :)

– None – 2018-02-02T12:15:43.960

2

Welcome to PPCG! I suggest using the Sandbox for your future challenges.

– user202729 – 2018-02-02T12:17:47.663

7Welcome to the site! Excellent use of another challenge for inspiration without falling into the dupe trap :) – Shaggy – 2018-02-02T13:31:21.087

Your helper program doesn't work for programs with multiple lines. How about this modified version from the other question?

– Jo King – 2018-02-03T00:09:00.353

@JoKing Oh whoops, fixed now. I actually modified my Python program to take input from command line arguments rather that STDIN, which removes the issues. I'd rather not have a reference implementation in Brainfuck, but thanks anyway for pointing that out! – None – 2018-02-03T09:49:09.080

1@user77954 But my brainfuck code is shorter than your python :( (has anyone ever said that before?) – Jo King – 2018-02-03T11:33:59.070

Answers

41

Pyth, 2 bytes


5

Try it online! Also Try it doubled, tripled!

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.

Mr. Xcoder

Posted 2018-02-02T12:14:49.237

Reputation: 39 774

6Holy frick that's short... – ETHproductions – 2018-02-02T12:28:55.290

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 – 2018-02-03T20:20:52.940

10Everyone (especially HNQ voters), go upvote other answers too and avoid the FGITW effect. – user202729 – 2018-02-04T09:38:31.627

26

JavaScript (ES6), 42 32 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.

ETHproductions

Posted 2018-02-02T12:14:49.237

Reputation: 47 880

Congrats, passes the tests I have made! – None – 2018-02-02T12:36:19.053

1... Ingenious! :) – Shaggy – 2018-02-02T12:39:12.227

18

05AB1E, 2 bytes

5=

Try it online!

Port of my Pyth answer.

Mr. Xcoder

Posted 2018-02-02T12:14:49.237

Reputation: 39 774

Congrats, passes the tests I have made! – None – 2018-02-02T12:36:38.897

2TFW you're really over-complicating things and see *this* answer. – Magic Octopus Urn – 2018-02-02T15:50:39.170

17

Python 2, 42 38 28 bytes

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

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

Rod

Posted 2018-02-02T12:14:49.237

Reputation: 17 588

Uh... character, not string. – user202729 – 2018-02-02T12:33:30.617

@user202729 thanks for the heads up, fixed =] – Rod – 2018-02-02T12:36:41.650

13

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.

xnor

Posted 2018-02-02T12:14:49.237

Reputation: 115 687

9

C (gcc), 170 168 96 80 72 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!

gastropner

Posted 2018-02-02T12:14:49.237

Reputation: 3 264

Doesn't seem to work? – user202729 – 2018-02-04T09:40:12.500

@user202729 ah, yes. Thought I fixed a typo but introduced a bug. Reverting. – gastropner – 2018-02-04T09:41:26.330

8

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.".

ovs

Posted 2018-02-02T12:14:49.237

Reputation: 21 408

1I was going to suggest n=False+=1;print'*'*n;, but I keep forgetting that that's not a Python feature... – ETHproductions – 2018-02-02T16:15:14.060

6

Charcoal, 6 bytes

⊞υωLυ⸿

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
   L    Length
        Implicitly print as a row of `-`s
     ⸿  Move to start of next line

Neil

Posted 2018-02-02T12:14:49.237

Reputation: 95 035

6

Haskell, 68 bytes

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.

Laikoni

Posted 2018-02-02T12:14:49.237

Reputation: 23 676

5

Ruby, 18 bytes

$/=?x+$/;  puts$/;

Try it online! Doubled! Tripled!

m-chrzan

Posted 2018-02-02T12:14:49.237

Reputation: 1 390

5

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.

Jo King

Posted 2018-02-02T12:14:49.237

Reputation: 38 234

5

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.

Nitrodon

Posted 2018-02-02T12:14:49.237

Reputation: 9 181

Command line flags are free now. – user202729 – 2018-02-04T09:30:43.437

4

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.

DLosc

Posted 2018-02-02T12:14:49.237

Reputation: 21 213

3

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.

Try it doubled and Try it tripled.

Giuseppe

Posted 2018-02-02T12:14:49.237

Reputation: 21 077

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 – 2018-02-04T23:01:22.443

@AndreïKostyrka that would be 43 bytes which isn't even, unfortunately. – Giuseppe – 2018-02-05T16:24:00.853

3

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.

Giuseppe

Posted 2018-02-02T12:14:49.237

Reputation: 21 077

3

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!

gggg

Posted 2018-02-02T12:14:49.237

Reputation: 1 715

3

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.

MD XF

Posted 2018-02-02T12:14:49.237

Reputation: 11 605

2

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!

Dom Hastings

Posted 2018-02-02T12:14:49.237

Reputation: 16 415

1Command line flags are free now. – user202729 – 2018-02-04T09:30:36.520

1

Zsh, 10 bytes

s+=0
<<<$s

Try a full test suite online!

...yeah, this is a bit better. Append to string N times, then print N times. Turns out <<<foo<<<foo works just fine.


Zsh, 64 bytes

Character used: (space).

f(){printf '%*s\n' $1}
:<<'E'

E
repeat $[i=LINENO/3];f $i
exit

Try a full test suite online!

The midpoint is between the second E and the newline following it. A heredoc will end when there is an E on a line by itself, which happens right in the middle of the code.

GammaFunction

Posted 2018-02-02T12:14:49.237

Reputation: 2 838

lol @ "slight" improvement. could also express it as s+=0;<<<$s – roblogic – 2019-09-08T09:57:41.353