# Write a self replicating program that stops replicating after "printing" "Hello world!" with the filenames

Write a self replicating program, in the source code filename H.*, contianed in an empty folder then manually compile and run or just run the program if your chosen language is dynamic, and it should then just create a new source file, e.*, with the same exact source code, and automatically compile/run that new program, which makes the new source file l.*, etc. until the source file !.* is made, and that program should do essentially nothing once it is compiled/run. "Hello world!" has some repeated characters, so the second character should be created by a similar filename, just with a new <space> at the end. Programs that just make/compile source files from the single instance of the program are invalid, and yes, " " is a valid filename. It should appear to say "Hello World!" when viewed in the file explorer sorted by time created.

For example, if you were to attempt this challenge in python, than you would create the source file H.py, which complies and runs e.py, which then itself creates l.py, that file l .py, o.py, .py, w.py, o .py, r.py, l .py and d.py which finally creates and runs !.py, which does not do anything once run.

• "Self-replicating" implies that the program creates another file exactly identical to the original. Do the files all have to be identical, so that they have to read their own filename? May 24, 2015 at 22:31
• maybe re-write this as an ouroboros challenge? instead of the programs compiling the next one, just output the source, so H.py produces e.py... d.py produces H.py May 24, 2015 at 22:45
• @Doorknob yes, They would have to have the same content, and need to check their filename to see what to make the next one called. May 24, 2015 at 23:58
• Shouldn't the example end with !.py? Also, it states that e.* should create e.* again. May 25, 2015 at 0:51
• There are 3 ls in Hello world!. What should the filename of the 3rd be? May 25, 2015 at 1:31

# Python 2, 194

import os,sys
h,A='H|e|l|l |o| |w|o |r|l  |d|!'.split('|'),sys.argv[0]
d,a,F=dict(zip(h,h[1:])),A.split('.')[0],file

• the code runs without an error, but doesn't appear to do anything? May 25, 2015 at 8:57
• oops, after re-reading the specs, it seems that I missed the fact that it had to run itself ... that's fixed May 25, 2015 at 9:05
• you have to name the script "H.py" by the way May 25, 2015 at 9:08
• It still seems to not be working, have you tried it yourself? May 25, 2015 at 9:12
• Yes I did ! I run under linux, so maybe running "python.exe" instead of "python" is the cause of the issue May 25, 2015 at 9:17

# Bash, 9988 84

X=grep -Po "(?<=#$0#)[^#]*" "$0" #H#e#l#l #o# #W#o #r#l  #d#!
cp "$0" "$X"
sh "$X"  ## Explanation: 1. First we create a regex from the name of the file (the argument $0). For example, here is the regex constructed when the first file executes ($0 is H):  (?<=#H#)[^#]*  This searches for a sequence of non-# characters preceded by #H#. That is, it finds the next filename in a sequence of #-separated filenames. (I used # so that the separator before H would function as the string delimiter itself, with the bonus of not needing a closing delimiter.) 2. Then we search (grep) the currently-executing file "$0" with this regex. We need the -P flag (or similar) to use the lookbehind construct, and -o to only return the new filename, not the whole line.

3. We store the result (using backticks ) of this whole evaluation in the variable X.

4. We copy (cp) the current file to the new file.

5. Finally we execute the new file with sh.

## Example usage:

robert@unity:~/hwbash$ls -lrt total 4 -rw-r--r-- 1 robert robert 97 May 25 23:52 H robert@unity:~/hwbash$ sh H
cp: cannot create regular file ': No such file or directory
sh: 0: Can't open
robert@unity:~/hwbash$ls -lrt total 48 -rw-r--r-- 1 robert robert 97 May 25 23:52 H -rw-r--r-- 1 robert robert 97 May 25 23:52 e -rw-r--r-- 1 robert robert 97 May 25 23:52 l -rw-r--r-- 1 robert robert 97 May 25 23:52 l -rw-r--r-- 1 robert robert 97 May 25 23:52 o -rw-r--r-- 1 robert robert 97 May 25 23:52 -rw-r--r-- 1 robert robert 97 May 25 23:52 W -rw-r--r-- 1 robert robert 97 May 25 23:52 o -rw-r--r-- 1 robert robert 97 May 25 23:52 r -rw-r--r-- 1 robert robert 97 May 25 23:52 l -rw-r--r-- 1 robert robert 97 May 25 23:52 d -rw-r--r-- 1 robert robert 97 May 25 23:52 ! robert@unity:~/hwbash$


1. I saved some bytes by using bash H instead of #!/bin/bash. This also means that I don't have to copy the executable permission, so sorting by timestamps works now (but you need to add a sleep 1 or something to make it reliable).
2. Saved some more bytes by moving the string to a comment and changing the command from bash to sh.
• I would appreciate any bash masters who could advise me on where I can trim off whitespace, if any... May 26, 2015 at 3:59