Clone yourself!

13

1

You are to create a program that creates exact clones of itself infinitely until stopped. Whatever is in the original program must be in the clones. In other words, the clones and original program are the same in every way except for that the clones don't have to be in the same type of file as the source code (they can be text files).

Example:

If my original program is:

for i in range(0, 10):
     print i

the clone must also be:

for i in range(0, 10):
     print i

Rules and Clarifications:

  • Standard loopholes are forbidden

  • The clones must be the exact copy of the original

  • The clones must be readable files that can be run if put inside the correct interpreter

  • The program can read its own source code

  • All clones must be separate files

  • Printing your program out is not allowed

  • File names count into the number of bytes

  • The clones don't have to be in the same directory as the source file nor share the same file name

  • At least 1000 clones should be able to be created

Winning:

Least bytes wins!

Anthony Pham

Posted 2016-12-27T16:20:27.433

Reputation: 1 911

Answers

3

Zsh, 19 17 9 bytes

#!/bin/zsh
<$0>$$;$0

Per consensus on meta, the shebang is excluded from the byte count.

Try it online!

Note that TIO's forkbomb protection will kill the process after 113 files have been generated. It can easily generate 1000 files on a system without such conservative resource limits.

Dennis

Posted 2016-12-27T16:20:27.433

Reputation: 196 637

I like yes `<$0`|split -1 a lot better, but that doubles the byte count... – Dennis – 2016-12-27T20:10:37.897

Too bad the in-out redirection like that does not work well in bash :( For one moment, I naïvely thought to have outgolfed you :) – zeppelin – 2016-12-27T20:27:36.220

4

Batch, 32 bytes

set/an=%1+1
copy %0 %n%
%0 %n%

Not using @ because there's no restriction on STDOUT. %1 defaults to the empty string, so n becomes 1 the first time and increments on every pass. Alternatively, this might work for 28 bytes, but I have no idea how random %random% actually is when used like this:

copy %0 %random%%random%
%0

Neil

Posted 2016-12-27T16:20:27.433

Reputation: 95 035

2IIRC %random% is based off the current time and can generate anything from 0 to something slightly above 32,000. – user2428118 – 2016-12-27T20:08:50.213

To clarify, %random% is a variable that, when accessed, yields a random number as @user2428118 mentioned. – Conor O'Brien – 2016-12-27T20:51:53.003

3

Bash, 25, 16, 12, 11 bytes

EDITS:

  • Removed the newline (-1 byte), Added "Try It Online". Thanks @Dennis !
  • Use background job PID $! as the filename (will be reused every ~32k files, but that is now allowed), -4 bytes

Golfed

#!/bin/bash
$0&cp $0 $!

Explained

Re-spawns itself as a background job with &, before doing a copy, so each iteration will run under its own PID.

Uses the last job PID as the filename.

This can run infinitely (or until stopped) but will be reusing clone filenames approx. every ~32k iterations.

Could be a bit nasty to kill, but AFAIK is not against the rules.

Try It Online!

zeppelin

Posted 2016-12-27T16:20:27.433

Reputation: 7 884

Do you have a testing place where I can try this out for myself? – Anthony Pham – 2016-12-27T17:22:18.253

@PythonMaster, I will normally use Tutorial Point's online testbed, but it does not have uuid installed, and does not like the script doing so much IO. – zeppelin – 2016-12-27T18:39:01.730

3

Ruby, 78 bytes, 77 43 + 4 (filename: a.rb) = 47 bytes

  • Version 1.2

43 bytes + name / Thanks to @Alexis So much shorter!!!

loop{File.write "#{$.+=1}",open("a.rb",?r)}

that's about as golfed as I think it's going to get

  • Version 1.1

73 bytes + name / Thanks to @Alexis Anderson

i=0
loop do
File.open("#{i}",?w){|b|b.puts File.read("a.rb",?r)}
i+=1
end
  • Version 1.0

74 bytes + name

i=0
loop do
File.open("#{i}","w") do{|b|b.puts File.read("a.rb")}
i+=1
end

This is my first Ruby answer so all inprovements are welcome.

Roman Gräf

Posted 2016-12-27T16:20:27.433

Reputation: 2 915

kernel open forwards to file open, you should be able to omit the File. altogether. ?w in place of "w". I think the do on the File.open is useless, since you have the curly braces. – Alexis Andersen – 2016-12-27T20:49:23.933

open('a.rb',?r) in place of File.read('a.rb') – Alexis Andersen – 2016-12-27T20:57:57.220

?w is a character w. A string in it's own right. ?w == 'w'. so you don't need the quotes in "?w" – Alexis Andersen – 2016-12-27T20:59:22.763

i=0 loop do File.write "#{i}",open("a.rb",?r) i+=1 end – Alexis Andersen – 2016-12-27T21:04:24.993

actually, you can also use curly braces for the loop method too – Alexis Andersen – 2016-12-27T21:07:08.190

loop{File.write "#{$.+=1}",open("a.rb",?r)} that's about as golfed as I think it's going to get – Alexis Andersen – 2016-12-27T21:10:58.727

Which version of Ruby? In latest 2.4.0 Kernel#open returns an IO object, resulting things like “#File:0x0055b7ad129bd8” in the clones.

– manatwork – 2016-12-28T08:58:16.843

You can omit the space between write and ". Also, is it really necessary to have "a.rb" instead of "a" or, say, $/ (which, hilariously, would create a file whose name is a newline, but hey, it works!)? – Jordan – 2016-12-29T19:36:34.407

2

sh, 24 bytes

yes cp $0 '`uuid`'|sh -s

Rainer P.

Posted 2016-12-27T16:20:27.433

Reputation: 2 457

2

C#, 104 102 bytes

-2 bytes thanks to berkeleybross

namespace System.IO{class a{static void Main(){int i=0;while(1>0){i++;File.Copy("c{i}.cs");}}}}

No, it's not the shortest. But it's C#. What did you expect?

Hunter Robertson

Posted 2016-12-27T16:20:27.433

Reputation: 41

1You don't need a namespace so you can fully qualify the File.Copy. Changing to a for loop so you can inline the int declaration and remove the 1>0 part. And pre increment the i in the File.Copy statement, that gives you 86 bytes I believe: class P{static void Main(){for(int i=0;;)System.IO.File.Copy("c.cs","c"+ ++i+".cs");}} – TheLethalCoder – 2016-12-28T15:43:38.600

Actually this won't work because c.cs won't be in the output directory and has to be copied there manually. How does this work? – TheLethalCoder – 2016-12-28T15:45:24.723

Dont forget about string interpolation $"c{++i}.cs" is 2 bytes shorter than "c"+ ++i+".cs" – berkeleybross – 2016-12-28T17:59:33.853

1

Python 2, 54 bytes

from shutil import*
a=__file__
while`copy(a,a*2)`:a*=2

Blue

Posted 2016-12-27T16:20:27.433

Reputation: 26 661

Don't you have to import sys? – Samuel Shifterovich – 2016-12-27T21:18:55.017

@SamuelShifterovich I would have had. I'm not sure how that edit survived as the byte count I was using was for the program now shown – Blue – 2016-12-27T21:52:11.353

1

Processing, 55 + 5 (name of file) = 60 bytes

I don't know if the filename counts as extra bytes

for(int i=0;;)saveStrings(i+++"",loadStrings("a.pde"));

Pretty much a series of builtins all chained together

Explanation

for(int i=0;;)           //A complex piece of code that cannot be explained in words alone
saveStrings(             //Save param2 (String[]) into file param1 (String)
 i+++"",                 // filename: the int i (and then is incremented)
 loadStrings("a.pde")    // content: load the content of file a.pde as a String[]
);

user41805

Posted 2016-12-27T16:20:27.433

Reputation: 16 320

That "complex code" is smiling – Roman Gräf – 2016-12-27T20:04:47.343

1

ForceLang + the ForceLang-JS module, 162 bytes

set j require njs
j var a=function(){for(;;){var p=new java.io.PrintStream(Math.random()+".txt");p.println("set j require njs");p.print("j var a="+a+";a()")}};a()

SuperJedi224

Posted 2016-12-27T16:20:27.433

Reputation: 11 342

1

PHP, 91 60 bytes

Saved 31 bytes thanks to manatwork

<?for(;;)file_put_contents(++$i,file_get_contents("f.php"));

Usage: $ php f.php clones f.php and its code reproducing infinitely itself in filenames like 1, 2, 3... until timeout.

Previous version:

<?for($i=0;;$i++)$p=fwrite(fopen("$i.php","w"),fread(fopen($f="f.php","r"),filesize($f)));

Usage: $ php f.php clones f.php and its code reproducing infinitely itself like f1.php, f2.php, f3.php ... f(n).php until timeout.

Mario

Posted 2016-12-27T16:20:27.433

Reputation: 3 043

As the clone file name can be anything, why not use just numbers without extension? <?for(;;)file_put_contents(++$i,file_get_contents("f.php")); – manatwork – 2016-12-28T09:06:24.857

1

Mathematica, 41 bytes

For[a=0,1>0,$Input~CopyFile~ToString@a++]

Full program. Copies its own source file into 0, 1, 2, etc. in the current directory.

LegionMammal978

Posted 2016-12-27T16:20:27.433

Reputation: 15 731

0

awk, 29 (21) bytes,

{while(close(i++)||1)print>i}

awk is not really the tool for this as it requires close() when running indefinitely. Otherwise all it produces are empty files.

If the clone count had a limit, for example 5:

{while(++i<5)print>i}

the program would be 21 bytes.

Paste the program to a file a and run:

$ awk -f a a

James Brown

Posted 2016-12-27T16:20:27.433

Reputation: 663

0

Python, 69 bytes

a=open(__file__).read()
i=0
while 1:i+=1;open(`i`+'.py','w').write(a)

I tried using a for the file name, but (unsurprisingly) Windows does not like files named a=open(__file__).read()\ni=0\nwhile 1:i+=1;open(i+'.py','w').write(a). I also tried this:

a=open(__file__).read()
i=''
while 1:i+='.py';open(i,'w').write(a)

However it gives me this error:

Traceback (most recent call last):
  File "C:/Python27/codeGolfCopy.py", line 3, in <module>
    while 1:i+='.py';open(i,'w').write(a)
IOError: [Errno 2] No such file or directory: '.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py.py'

It creates 83 files, but that is not even close to the needed 1000.

If you would like to remove these files, you can use this:

import os;i = 1
while 1:os.remove(`i`+'.py');i+=1

nedla2004

Posted 2016-12-27T16:20:27.433

Reputation: 521

0

RBX.Lua, 14 bytes

IS THIS EVEN A FILESYSTEM I DON'T KNOW

script:Clone()

Clones itself. As it gets instanced, in runs again, therefore providing automatic recursion.

devRicher

Posted 2016-12-27T16:20:27.433

Reputation: 1 609

0

JavaScript ES6 34 bytes

Not sure if this counts, but here:

_=()=>alert("_="+_+";_()")&_();_()

Kuilin Li

Posted 2016-12-27T16:20:27.433

Reputation: 421

0

Python 2, 61 bytes (unix) 65 (cross-platform)

Unix - 61 bytes

import os;i=1
while os.popen('cp %s %i.py'%(__file__,i)):i+=1

On unix cp is the systems copy-file - command. Using the console via popen allows me to copy the file by cp. new files will spawn in old-files directory.

CrossPlatform - 65 bytes

i=0
while 1:i+=1;open('%i.py'%i,'w').write(open(__file__).read())

This works, as open on default allows reading.

Funfact: replace 1:i+=1 with i:i-=1 and it will stop at i copies.

Other than that, I see no way of making it shorter than @muddyfish did it.

Teck-freak

Posted 2016-12-27T16:20:27.433

Reputation: 131

0

C, 80 bytes

main(a){while(1)printf(a="main(a){while(1)printf(a=%c%s%c,34,a,34);}",34,a,34);}

Prints itself until you kill it. Just a standard C quine with an infinite loop.

MD XF

Posted 2016-12-27T16:20:27.433

Reputation: 11 605