Thursday Code Puzzler: Self Replicating Program
Thursday is code puzzler day here at DZone. The idea is simple: solve the coding problem as efficiently as you can, in any language or framework that you find suitable.
Note: Even though there really is nothing stopping you from finding a solution to this on the internet, try to keep honest, and come up with your own answer. It's all about the participation!
Do you have code puzzlers that you'd like to share with the DZone community? If so, please submit here.
Write a Self Replicating Program
Catch up on all our previous puzzlers here.
Tags:






Comments
Claude Lalyre replied on Thu, 2012/12/20 - 9:18am
Beware of Java viruses with such challenge...
Matt Cholick replied on Thu, 2012/12/20 - 11:29pm
Groovy:
def p = ''' @Grab(group='commons-lang', module='commons-lang', version='2.4') import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric File file = new File("${randomAlphanumeric(8)}.groovy").write(p) new GroovyShell(new Binding([p: "def p = ${"'"*3}${p}${"'"*3}; ${p.readLines()[3]}"])).evaluate(p.readLines()[0..2].join(';') as String) '''; new GroovyShell(new Binding([p: "def p = ${"'"*3}${p}${"'"*3}; ${p.readLines()[3]}"])).evaluate(p.readLines()[0..2].join(';') as String)The comment block messes with newline a bit, it's easier to read on Github: https://gist.github.com/4350645
Hari Ravula replied on Fri, 2012/12/21 - 4:46am
in response to:
Matt Cholick
public class SelfPrinting { public static void main(String[] args) { String str = "public class SelfPrinting {\n\tpublic static void main(String[] args) {\n\t\tString str = \"%s\";" + "\n\t\tSystem.out.printf(str, str, str);\n\t}\n}"; System.out.printf(str, str, str); } }