While browsing thru some notes from one of my old college notebooks, I came across this interesting contest that we had in one of my data structures classes. The idea is to create a program that will clone itself when it is ran, generating an exact copy of its source code as output that can be compiled and ran again, and then repeated using the second generation copy, and so on and so on... I thought it would be a great idea here, just to see how other people would solve it. See who could do it in the least amount of lines, the most amount of lines, the most obfuscated code. Anyway, here is my attempt to solve the puzzle.
#!/usr/bin/perl #Programmer : cerberus use strict; my @p = (); my $t = chr(9); my $spc = chr(32); my $s = chr(36) . "p"; my $LB = chr(91); my $RB = chr(93); my $eql = chr(61); my $tick = chr(39); my $sc = chr(59); my $i = 0; $p[0] = '#!/usr/bin/perl'; $p[1] = '#Programmer : cerberus'; $p[2] = 'use strict;'; $p[3] = 'my @p = ();'; $p[4] = 'my $t = chr(9);'; $p[5] = 'my $spc = chr(32);'; $p[6] = 'my $s = chr(36) . "p";'; $p[7] = 'my $LB = chr(91);'; $p[8] = 'my $RB = chr(93);'; $p[9] = 'my $eql = chr(61);'; $p[10] = 'my $tick = chr(39);'; $p[11] = 'my $sc = chr(59);'; $p[12] = 'my $i = 0;'; $p[13] = 'for($i=0; $i<13; $i++) { print "$p[$i]" . chr(10);}'; $p[14] = 'for($i=0; $i<17; $i++) { print "$t$s$LB$i$RB$spc$eql$spc +$tick" . $p[$i]. "$tick$sc" . chr(10); }'; $p[15] = 'for($i=13; $i<17; $i++){ print "$t$p[$i]" . chr(10);}'; $p[16] = 'exit 0;'; for($i=0; $i<13; $i++) { print "$p[$i]" . chr(10);} for($i=0; $i<17; $i++) { print "$t$s$LB$i$RB$spc$eql$spc$tick" . $ +p[$i]. "$tick$sc" . chr(10); } for($i=13; $i<17; $i++){ print "$t$p[$i]" . chr(10);} exit 0;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: The 6th Day Challenge
by jryan (Vicar) on Jan 16, 2002 at 04:23 UTC |