30
\$\begingroup\$

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ I suspect that this challenge is impossible in most languages, given that reading source code is forbidden by default. \$\endgroup\$ – Ørjan Johansen May 4 '17 at 18:12
  • 12
    \$\begingroup\$ @ØrjanJohansen and then Dennis shows up \$\endgroup\$ – Rod May 4 '17 at 18:13
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Ørjan Johansen May 4 '17 at 18:23
  • 3
    \$\begingroup\$ Since this is a very unusual quine, are the usual quine loopholes still disallowed? \$\endgroup\$ – Draconis May 4 '17 at 20:14
15
\$\begingroup\$

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

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.

\$\endgroup\$
  • \$\begingroup\$ an explanation will be forthcoming...I haven't tested it 288 times yet, but I'm fairly convinced it's correct \$\endgroup\$ – Giuseppe May 4 '17 at 19:14
  • \$\begingroup\$ It provably should be 289 bytes since the quine adds a newline character there but anyway its great you've solved it! \$\endgroup\$ – IQuick 143 May 4 '17 at 19:17
  • \$\begingroup\$ ah, you're right, stupid cat adding newlines. \$\endgroup\$ – Giuseppe May 4 '17 at 19:18
  • \$\begingroup\$ But is this a full program? Are the generated code full programs? \$\endgroup\$ – jimmy23013 May 4 '17 at 19:48
  • \$\begingroup\$ @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. \$\endgroup\$ – Steadybox May 4 '17 at 20:11
4
\$\begingroup\$

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.

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

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.
\$\endgroup\$
  • \$\begingroup\$ Great solution. I like the explanation. \$\endgroup\$ – IQuick 143 Mar 21 '18 at 14:11
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ You might want to mention in the first line that it's non-competing/cheating, just to discourage any downvotes it might otherwise receive. \$\endgroup\$ – quintopia May 5 '17 at 22:38
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Doesn't seem to work when I Try it online!, it's just an ordinary quine. \$\endgroup\$ – Ørjan Johansen May 6 '17 at 10:55
  • \$\begingroup\$ @ØrjanJohansen You should run it with php -r 'command'. \$\endgroup\$ – jimmy23013 May 6 '17 at 11:01
  • \$\begingroup\$ Gah, cannot get it to work. TIO seems to just ignore the -r arguments. \$\endgroup\$ – Ørjan Johansen May 6 '17 at 11:54
  • \$\begingroup\$ @ØrjanJohansen This is how you should test it. \$\endgroup\$ – jimmy23013 Mar 17 '18 at 15:07
  • \$\begingroup\$ Aha. I must have misunderstood something back then. Now I managed to get it to work with PHP as the language setting, too. \$\endgroup\$ – Ørjan Johansen Mar 17 '18 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.