# Code that changes itself

Forward:

Depending on what language you use and what user you are logged in as this challenge could be dangerous so proceed with Caution! You could delete something, fork bomb yourself, or worse...

Challange:

Write a program of n bytes which creates n copies of itself on disk, modifies the first byte of the first copy to an 8-bit value of your choosing, modifies the second byte of the second copy to an a bit-value of your choosing, ... , modifies the nth byte of the nth copy of the program to an 8-bit value of your choosing, and then executes all n of the modified copies synchronously starting with the first and ending with the last, the program should print "alive", then program should then exit.

Scoring: Lowest Score Wins

Score = (-1 * (Number of times "alive" is printed)) + number of bytes in your program

RULES

1. Infinite loops in the program or one of its creations will disqualify an answer.
2. Number of times alive is printed has to be less than 1024.

Why this interests me:

I think a lot about the relationships between biology and analogies between living things and computer programs. One of the qualities of living things is that they know how to reproduce. One of the factors of replication that make evolution possible is that the replication is not perfect. If one were to design artificial life it should have a very compact method of reproducing itself. This is why I am turning to the code golf community to see what sort of strange ideas are out there. Sorry for all the edits to this question.

• So does “infinite loop” here mean “infinite loop given infinite memory”, or…? And is a program allowed to print “alive” as many times as it wants? – Ry- Jul 6 '13 at 21:03
• If you hover over the language-agnostic tag then you'll see that it says "This tag should not be used". Unfortunately we don't seem to have any active moderators to kill it with fire. I've replaced it with code-challenge because the question has to be tagged with something. – Peter Taylor Jul 6 '13 at 21:46
• Are we allowed to replace some of the bytes with themselves? Makes it pretty easy if so. – histocrat Jul 7 '13 at 16:48
• You can replace the bytes with anything you wish. My original thought was to replace the code with random bytes to replicate non perfect children found in genetics. But you could seriously damage your computer. – ojblass Jul 7 '13 at 17:29
• @histocrat - It is wrong idea to replace some byte with itself. Exact copy of main program can not be created as it will lead to never-ending recursion. – Egor Skriptunoff Jul 10 '13 at 13:57

## Perl, 167 - 973 = -806 142 - 55 = 87

Here's an entry in the proper spirit of this code challenge:

if($ARGV[0]=~s/r/X/){open F,$0;for(@x=split//,join"",<F>){
$c=$_++;open F,">$0~".++$x;print F@x;$_=$c}sleep 1;
fork||exit exec"perl $0~$_ @ARGV"for 1..@x}print"alive\n"


I forkbombed myself a few times in the process of writing this script. As a result, this version comes with a couple of safety features. First off is the sleep 1 just before the spawning loop. That leaves enough idle processing time to allow one to type killall perl, if and when such measures become necessary. The second safety feature is that the spawn loop is disabled by default. To run in full power, you need to supply the word reproduce as an argument on the command line.

(Seriously, it's embarrassing to acknowledge the number of times I've had to do a hard reboot today because of some bug in my script, before I finally got the balance right.)

The program ends up producing several thousand imperfect copies of itself, from which about one in eight succeeds in printing alive. Most of the copies have syntax errors and so don't get run. A handful of scripts run but produce invalid output.

Here's a sample test run. (Note that, due to the sleep, it takes several seconds before the spawning finally grinds to a halt and all the scripts stop running.)

$mkdir run$ cp changer run/
$cd run$ ls -l
total 4
$perl changer alive$ perl changer reproduce 2> /dev/null >> out
$ls -l out -rw-r--r-- 1 breadbox breadbox 2608 2013-07-19 21:35 out$ ls -l out
$ls -l out -rw-r--r-- 1 breadbox breadbox 9370 2013-07-19 21:35 out$ ls -l out
$ls -l | wc -l 7691$ grep -c alive out
973
$cd ..$ rm -r run/
\$


Finally, I should note that the script has a bit of nondeterminism -- a common enough circumstance when starting up lots of child processes. As a result, the number of alives that get output varies a bit with each run. The highest count I've seen so far is 984, so I believe that it's not in any danger of exceeding the 1024 limit.

Here is my submission:

It's a program of o Bytes. It created 0 copies of itself and modified all 0 bytes. It prints out Alive 0 times for a total socre of -1*0+0=0

Feel free to smack me for this submission :)

• This seems to be compliant with the rules as currently written. Carry on. – primo Jul 20 '13 at 6:04
• This site makes me laugh... have a look at this answer: codegolf.stackexchange.com/a/11797/8501 – ojblass Jul 23 '13 at 14:40