44
\$\begingroup\$

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.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ 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. :) \$\endgroup\$
    – user77954
    Commented Feb 2, 2018 at 12:15
  • 2
    \$\begingroup\$ Welcome to PPCG! I suggest using the Sandbox for your future challenges. \$\endgroup\$
    – DELETE_ME
    Commented Feb 2, 2018 at 12:17
  • 7
    \$\begingroup\$ Welcome to the site! Excellent use of another challenge for inspiration without falling into the dupe trap :) \$\endgroup\$
    – Shaggy
    Commented Feb 2, 2018 at 13:31
  • \$\begingroup\$ Your helper program doesn't work for programs with multiple lines. How about this modified version from the other question? \$\endgroup\$
    – Jo King
    Commented Feb 3, 2018 at 0:09
  • 1
    \$\begingroup\$ @user77954 But my brainfuck code is shorter than your python :( (has anyone ever said that before?) \$\endgroup\$
    – Jo King
    Commented Feb 3, 2018 at 11:33

20 Answers 20

41
+500
\$\begingroup\$

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.

\$\endgroup\$
3
  • 6
    \$\begingroup\$ Holy frick that's short... \$\endgroup\$ Commented Feb 2, 2018 at 12:28
  • \$\begingroup\$ 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. \$\endgroup\$
    – Mr. Xcoder
    Commented Feb 3, 2018 at 20:20
  • 10
    \$\begingroup\$ Everyone (especially HNQ voters), go upvote other answers too and avoid the FGITW effect. \$\endgroup\$
    – DELETE_ME
    Commented Feb 4, 2018 at 9:38
26
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Congrats, passes the tests I have made! \$\endgroup\$
    – user77954
    Commented Feb 2, 2018 at 12:36
  • 1
    \$\begingroup\$ ... Ingenious! :) \$\endgroup\$
    – Shaggy
    Commented Feb 2, 2018 at 12:39
18
\$\begingroup\$

05AB1E, 2 bytes

5=

Try it online!

Port of my Pyth answer.

\$\endgroup\$
2
  • \$\begingroup\$ Congrats, passes the tests I have made! \$\endgroup\$
    – user77954
    Commented Feb 2, 2018 at 12:36
  • 2
    \$\begingroup\$ TFW you're really over-complicating things and see this answer. \$\endgroup\$ Commented Feb 2, 2018 at 15:50
17
\$\begingroup\$

Python 2, 42 38 28 bytes

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

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

\$\endgroup\$
2
  • \$\begingroup\$ Uh... character, not string. \$\endgroup\$
    – DELETE_ME
    Commented Feb 2, 2018 at 12:33
  • \$\begingroup\$ @user202729 thanks for the heads up, fixed =] \$\endgroup\$
    – Rod
    Commented Feb 2, 2018 at 12:36
14
\$\begingroup\$

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.

\$\endgroup\$
9
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ Doesn't seem to work? \$\endgroup\$
    – DELETE_ME
    Commented Feb 4, 2018 at 9:40
  • \$\begingroup\$ @user202729 ah, yes. Thought I fixed a typo but introduced a bug. Reverting. \$\endgroup\$
    – gastropner
    Commented Feb 4, 2018 at 9:41
8
\$\begingroup\$

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I was going to suggest n=False+=1;print'*'*n;, but I keep forgetting that that's not a Python feature... \$\endgroup\$ Commented Feb 2, 2018 at 16:15
6
\$\begingroup\$

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
\$\endgroup\$
6
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

Ruby, 18 bytes

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

Try it online! Doubled! Tripled!

\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
1
4
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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),"") \$\endgroup\$ Commented Feb 4, 2018 at 23:01
  • \$\begingroup\$ @AndreïKostyrka that would be 43 bytes which isn't even, unfortunately. \$\endgroup\$
    – Giuseppe
    Commented Feb 5, 2018 at 16:24
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
1
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ lol @ "slight" improvement. could also express it as s+=0;<<<$s \$\endgroup\$
    – roblogic
    Commented Sep 8, 2019 at 9:57
0
\$\begingroup\$

Japt, 6 bytes

P±QOpP

Test it, doubled, tripled

P±QOpP
P          :Initially empty string
 ±         :Append and reassign result
  Q        :  Quotation mark
   OpP     :Output P with trailing newline
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.