Slow growing Quine

30

3

Make a Quine.

Seems easy right? Well this quine must output itself plus its first character, which then outputs itself plus its second character, and so on.

This way the quine should in several generations output two copies.

Example: Lets your code be x. Running it should output x + x[:1]. Running the resulting program should output x + x[:2] and so on...

If your code was foobar, running this should output foobarf. Running this should output foobarfo. And so on and so forth following this pattern:

foobar
foobarf
foobarfo
foobarfoo
foobarfoob
foobarfooba
foobarfoobar
foobarfoobarf

Your Program must be longer than 2 Bytes and must output only ONE extra character of its own code each iteration.

IQuick 143

Posted 2017-05-04T17:28:14.620

Reputation: 1 229

3I suspect that this challenge is impossible in most languages, given that reading source code is forbidden by default. – Ørjan Johansen – 2017-05-04T18:12:38.120

12@ØrjanJohansen and then Dennis shows up – Rod – 2017-05-04T18:13:58.363

2@Rod Well I didn't say all, it's just that many/most languages have no obvious way to add arbitrary fragments of code to the end in such a way that (1) it doesn't give a syntax error (2) the program can detect the change. – Ørjan Johansen – 2017-05-04T18:23:11.197

3Since this is a very unusual quine, are the usual quine loopholes still disallowed? – Draconis – 2017-05-04T20:14:04.567

Answers

15

Zsh, 110 108 100 bytes

a=`<&0`<<''<<<t;b=${a:0:50};printf $b$b${a:0:-50}
a=`<&0`<<''<<<t;b=${a:0:50};printf $b$b${a:0:-50}

Try it online!

So it is possible.

Explanation

a=`<&0`<<''<<<t;       # Set A to everything following this line, until eof or
                       #   an empty line (which never happens before eof) encountered.
                       # A "t" is appended to prevent automatic trimming of newlines.
b=${a:0:50};           # Set B to the first line.
printf $b$b${a:0:-50}  # Print two copies of B and
                       #   A with 50 trailing characters removed.

jimmy23013

Posted 2017-05-04T17:28:14.620

Reputation: 34 042

1

Related: https://codegolf.stackexchange.com/q/100930/25180

– jimmy23013 – 2017-05-04T19:49:33.023

11

R, 289 bytes

s<-c("s<-", "i=get0('i',ifnotfound=0)+1;p=paste0(s,substr(get0('p',ifnotfound=s),1,i),collapse='');cat(s[1]);dput(s);cat(paste0(s[2],substr(p,1,i)))#")
i=get0('i',ifnotfound=0)+1;p=paste0(s,substr(get0('p',ifnotfound=s),1,i),collapse='');cat(s[1]);dput(s);cat(paste0(s[2],substr(p,1,i)))#

credit to this quine for inspiration. Only works if run in the same R environment as the previous quine is run.

Giuseppe

Posted 2017-05-04T17:28:14.620

Reputation: 21 077

an explanation will be forthcoming...I haven't tested it 288 times yet, but I'm fairly convinced it's correct – Giuseppe – 2017-05-04T19:14:51.680

It provably should be 289 bytes since the quine adds a newline character there but anyway its great you've solved it! – IQuick 143 – 2017-05-04T19:17:26.483

ah, you're right, stupid cat adding newlines. – Giuseppe – 2017-05-04T19:18:48.080

But is this a full program? Are the generated code full programs? – jimmy23013 – 2017-05-04T19:48:42.673

@jimmy23013 As far as I can tell, this answer and the generated code are full programs. There is no main or any other mandatory structure like that in R. Besides, the question doesn't explicitly ask for a full program, so a function or similar would do. – Steadybox – 2017-05-04T20:11:45.150

Yes, even functions are allowed. – IQuick 143 – 2017-05-05T12:29:46.883

5

Alice, 29 bytes

4P.a+80pa2*&wdt,kd&w74*,.ok@

Try it online!

The unprintable character is 0x18.

Explanation

The trouble with the usual "-based Fungeoid quines is that if we repeat the entire source code, then we also get additional " and the string no longer covers the entire source code. I assume that's why the existing answer uses the cheat-y g approach instead.

This answer does use the "-based approach, but instead of including a " in the source, we write it into the program at runtime. That way, there will only ever be one " regardless of how often the program is repeated (because we only write it to one specific coordinate, independently of program size).

The general idea is then that we create a representation of the whole source code on the stack, but only cycle through the first 29 of the characters (i.e. the program length) with the length of the loop determined by the size of the code. Therefore, we can actually append arbitrary characters (except linefeeds) after @ and the result will always be a cyclic repetition of the core program, one character longer than the source.

4P   Push 4! = 24. This is the code point of the unprintable, which we're 
     using as a placeholder for the quote.
.a+  Duplicate it and add 10, to get 34 = '"'.
80p  Write '"' to cell (8,0), i.e. where the first unprintable is.
    Placeholder, becomes " by the time we get here, and pushes the code
     points of the entire program to the stack. However, since we're already
     a good bit into the program, the order will be messed up: the bottom
     of the stack starts at the 24 (the unprintable) followed by all 
     characters after it (including those from extraneous repetitions). Then 
     on top we have the characters that come in front of the `"`. 
     So if the initial program has structure AB, then any valid program has
     the form ABC (where C is a cyclic repetition of the initial program),
     and the stack ends up holding BCA. We don't care about C, except to
     determine how big the program is. So the first thing we need to do is
     bring B to the top, so that we've got the initial program on top of
     the stack:
a2*  Push 10*2 = 20.
&w   Run the following section 21 times, which is the length of B.

  dt,  Pull up the value at the bottom of the stack.

k    End of loop.
d&w  Run the following section D+1 times, where D is the length of ABC.

  74*  Push 28, one less than the number of characters in AB.
  ,    Pull up the 29th stack element, which is the next character to print.
  .o   Print a copy of that character.

k    End of loop.
@    Terminate the program.

Martin Ender

Posted 2017-05-04T17:28:14.620

Reputation: 184 808

Great solution. I like the explanation. – IQuick 143 – 2018-03-21T14:11:57.247

4

Perl 5, 83 bytes (including final newline)

$_=q($/=$;;$_="\$_=q($_);eval
__END__
".<DATA>;print$_,/(.).{82}\z/s);eval
__END__

Try it online!

The good ol' __DATA__ token makes it easy to append an arbitrary string to any Perl program, which the main program can then access via the <DATA> file handle (and actually using __END__, which does the same thing for backwards compatibility, instead of __DATA__ saves two extra bytes).

Note that this program does not read its own source code, but only the extra input data appended to its source after the __END__ token. In effect, the __END__ token and everything after it functions kind of like a string literal terminated by the end of the input.

Also note that, in order to meet the spec exactly, this program must end in a newline. If it does not, the newline actually gets automatically appended after the second __END__ anyway, but then the first iteration output won't be precisely equal to the code plus its first byte any more.

Ilmari Karonen

Posted 2017-05-04T17:28:14.620

Reputation: 19 513

2

Befunge-98, 30 bytes

0>:#;0g:840#;+*#1-#,_$a3*%0g,@

Try it online!

My try using Befunge-98 which uses a space terminated quine which also counts how many characters have been outputed. Does however use the g command.

IQuick 143

Posted 2017-05-04T17:28:14.620

Reputation: 1 229

You might want to mention in the first line that it's non-competing/cheating, just to discourage any downvotes it might otherwise receive. – quintopia – 2017-05-05T22:38:15.327

2

PHP, 146 bytes

ob_start(function($s){return($u=substr($s,0,73)).$u.substr($s,0,-72);})?>ob_start(function($s){return($u=substr($s,0,73)).$u.substr($s,0,-72);})?>

It should be run using -r in command line.

jimmy23013

Posted 2017-05-04T17:28:14.620

Reputation: 34 042

Doesn't seem to work when I Try it online!, it's just an ordinary quine.

– Ørjan Johansen – 2017-05-06T10:55:57.287

@ØrjanJohansen You should run it with php -r 'command'. – jimmy23013 – 2017-05-06T11:01:45.113

Gah, cannot get it to work. TIO seems to just ignore the -r arguments. – Ørjan Johansen – 2017-05-06T11:54:30.477

@ØrjanJohansen This is how you should test it.

– jimmy23013 – 2018-03-17T15:07:38.970

Aha. I must have misunderstood something back then. Now I managed to get it to work with PHP as the language setting, too.

– Ørjan Johansen – 2018-03-17T15:20:20.903

2

Runic Enchantments, 61 bytes

803X4+kw.'.q}͍}͍}͍}͍}͍}͍}͍}͍}͍::l͍5X-:}-$:l͍{-1--@

Try it online!

Uses a similar approach as the Alice answer: reflectively writes the string " command into the code so that there Is Only One. There rest is a lot of string and stack manipulation to recover the original program, how many extra bytes are needed, and print the necessary hunks.

The sequence rotates the in-memory string representation around so that the 803X4+kw appears at the beginning instead of at the end, due to the position of the " and there isn't an easier way of handling this operation without having to compute a lot of awkward numbers.

While the original program is 61 bytes its string-length is only 50, which is easy to construct as 5X and it was only coincidence that this didn't need to be padded out after containing all necessary functionality (e.g. a length 49 program would be easier to encode as 50 with one byte of padding than to encode a literal 49, while 51 would be encoded as 5X3+ or 53, having to account for its own extra bytes).

Draco18s no longer trusts SE

Posted 2017-05-04T17:28:14.620

Reputation: 3 053