Make an interweaving quine
Fission, 6 bytes
'!+OR"
Try it online! Try two copies! Try three!
Explanation
This is just the Fission standard quine. It happens to work for this challenge, because Fission has explicit entry points into the program. In particular, by duplicating the program, we add another R
which adds another atom (instruction pointer). Since the source code is toroidal, the effective code being executed doesn't change otherwise — to each atom, the code still looks the same locally. However, the atoms are executed in lock step, so that the things they print are interleaved and we get an additional copy of each character in the output.
For completeness' sake I'll just shortly reiterate how the program itself works. Regardless of whether we repeat the program or not (e.g. '!+OR"'!+OR"'!+OR"
), each atom sees the following code:
R"'!+OR"'!+O
The "
toggles string printing mode, so that the program starts by printing '!+OR
directly to STDOUT, which is all of the quine except the quote. Then '!
sets the atom's mass to the character code of !
, +
increments it, which gives "
, and O
prints it while simultaneously destroying the atom. The program then terminates, because there are no atoms left.
Python 2.7, 377 310 304 194 191 bytes!
This is my first golf, so I didn't expect it to be too good. But I thought the concept in the solution I came up with was somewhat funny, so I'm posting it anyway.
def f():
import threading as T,inspect as i;global t,a,i
try:t.cancel()
except:a=0
a+=1;t=T.Timer(1,d);t.start()
def d():print''.join(c*a for c in i.getsource(f)+i.getsource(d)+"f()")
f()
Indeed, this is a quine; you can try it here. It abuses the inspect module pretty hard.
If we try running it with the same source code x2, we get the right output as well; you can try that here. x3, x4, etc. all work as expected.
Ungolfed with explanation:
def f(): # Defines a central function f
import threading as T,inspect as i # Imports threading and inspect
global t,a,i # Global vars
try:
t.cancel() # Tries to cancel Timer from previous code
except:
a = 0 # Reached when code is 1st copy; initializes a.
a += 1 # a++; this is the number of copies thus far.
t = T.Timer(1,d) # Creates, then starts a timer to call function
t.start() # d in 1 second.
def d(): # Prints out the source code; the quine part.
print''.join(c*a for c in i.getsource(f)+i.getsource(d)+"f()")
f() # Calls f()!
CJam, 19 bytes
{]W=s"_~"+T):Te*}_~
Try it online!
How it works
{ }_~ Define an anonymous code block (function).
_~ Push a copy, and execute the copy.
]W= Wrap the entire stack in an array and select its last element.
This discards whatever was on the stack before the original
code block, which is needed for subsequent iterations.
s"_~"+ Cast the code block to string, push "_~", and concatenate.
This pushes the stringified source code on the stack.
T):T Push T (initially 0), increment it, and save the result in T.
e* Repeat each character in the stringified source code T times.