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

6

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.

micsthepick

Posted 2015-05-24T22:12:31.323

Reputation: 421

"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? – Doorknob – 2015-05-24T22:31:28.033

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 – Sparr – 2015-05-24T22:45:45.477

@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. – micsthepick – 2015-05-24T23:58:57.397

Shouldn't the example end with !.py? Also, it states that e.* should create e.* again. – LegionMammal978 – 2015-05-25T00:51:14.900

@LegionMammal978 Fixed. – micsthepick – 2015-05-25T00:55:04.043

2There are 3 ls in Hello world!. What should the filename of the 3rd be? – jimmy23013 – 2015-05-25T01:31:22.583

@user23013 add another space. – micsthepick – 2015-05-25T06:44:10.863

filenames are H.*, e.*, l.*, l .*, o.*, .* , w.*, o .*, r.*, l .*, d.* and !.* – micsthepick – 2015-05-25T09:16:01.203

Will the directory initially be empty, apart from the code itself? If not, then will it have files with the same extension? – LegionMammal978 – 2015-05-25T18:20:33.980

Added a condition, start with an empty folder containing only H.py – micsthepick – 2015-05-25T21:53:44.760

If we don't need an extension, can our files just be H, e, etc? – 2012rcampion – 2015-05-26T03:33:39.393

Answers

2

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
if a in d:o=d[a]+'.py';F(o,'w').write(F(A).read());os.popen('python "'+o+'"')

dieter

Posted 2015-05-24T22:12:31.323

Reputation: 2 010

the code runs without an error, but doesn't appear to do anything? – micsthepick – 2015-05-25T08:57:27.253

oops, after re-reading the specs, it seems that I missed the fact that it had to run itself ... that's fixed – dieter – 2015-05-25T09:05:14.933

you have to name the script "H.py" by the way – dieter – 2015-05-25T09:08:01.290

It still seems to not be working, have you tried it yourself? – micsthepick – 2015-05-25T09:12:32.823

Yes I did ! I run under linux, so maybe running "python.exe" instead of "python" is the cause of the issue – dieter – 2015-05-25T09:17:39.210

Right now I am running from IDLE 2.7.8 on a Mac... – micsthepick – 2015-05-25T09:20:05.873

Managed to get it working with os.system('python H.py') – micsthepick – 2015-05-25T09:24:35.510

Confirmed after testing on a windows laptop, only runs when executed from the command line, or using os.system – micsthepick – 2015-05-25T09:40:09.710

0

Bash, 99 88 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$

Updates

  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.

2012rcampion

Posted 2015-05-24T22:12:31.323

Reputation: 1 319

I would appreciate any bash masters who could advise me on where I can trim off whitespace, if any... – 2012rcampion – 2015-05-26T03:59:16.190