6
\$\begingroup\$

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.

\$\endgroup\$
11
  • \$\begingroup\$ "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? \$\endgroup\$
    – Doorknob
    May 24, 2015 at 22:31
  • \$\begingroup\$ 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 \$\endgroup\$
    – Sparr
    May 24, 2015 at 22:45
  • \$\begingroup\$ @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. \$\endgroup\$ May 24, 2015 at 23:58
  • \$\begingroup\$ Shouldn't the example end with !.py? Also, it states that e.* should create e.* again. \$\endgroup\$ May 25, 2015 at 0:51
  • 2
    \$\begingroup\$ There are 3 ls in Hello world!. What should the filename of the 3rd be? \$\endgroup\$
    – jimmy23013
    May 25, 2015 at 1:31

2 Answers 2

2
\$\begingroup\$

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

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.
\$\endgroup\$
1
  • \$\begingroup\$ I would appreciate any bash masters who could advise me on where I can trim off whitespace, if any... \$\endgroup\$ May 26, 2015 at 3:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.