-
Senior Member
An amusing challenge.
An amusing challenge which I've done before in C in Perl, but never in actionscript.
Can you write a piece of actionscript which produces (via trace)
an exact copy of it's own source code?
Props to the shortest example.
- Jim
-
the friendly canadian
Wow, that's different. Good luck with that jbum. I think I'd get lost at 'trace ()'.
-
Ok .. no matter how hard I try, I can't seem to "trace-my-steps":
Code:
trace("trace(\"trace\");");
trace("trace(\"trace(\\\"trace\\\");\");");
trace("trace(\"trace(\\\"trace{\\\\\\\"trace\\\\\\\");\\\");\");");
// returns:
//trace("trace");
//trace("trace(\"trace\");");
//trace("trace(\"trace{\\\"trace\\\");\");");
Richard
-
Hmmm .. double-posted .. owel, I'll edit this with another attempt that returns 1 line short as well. I can't twist my brain enough to see a recursive algorithm for it yet:
Code:
var str:String = "var str:String = \"function printSelf(str) {\\n\\ttrace(str);\\n}\\nprintSelf(str);\";\n\n"+
"function printSelf(str) {\n\ttrace(str);\n}\n\nprintSelf(str);";
function printSelf(str) {
trace(str);
}
printSelf(str);
returns:
Code:
var str:String = "function printSelf(str) {\n\ttrace(str);\n}\nprintSelf(str);";
function printSelf(str) {
trace(str);
}
printSelf(str);
Last edited by Dickee; 04-13-2004 at 04:09 AM.
-
Here's a try that is more in tune with the spirit of the challenge .. escaped characters don't get traced, but everything else is in place:
Code:
var mirror_arr:Array = [
"var mirror_arr:Array = [",
"\n\tfirst\u002c",
"\n\tlast",
"\n];",
"\ntrace(mirror_arr.join(\"\"));"
];
trace(mirror_arr.join(""));
<edit>
Aaargh! How high is my horizon set? I totally side-stepped the iterative nature of tracing the algorithm on this one .. I don't show mirror_arr[0,3,4] elements in the trace! This returns:
Code:
var mirror_arr:Array = [
first,
last
];
trace(mirror_arr.join(""));
This is harder to do than I originally thought .. back to the drawing board. Please feel free to revise/outdo any of my attempts!
Richard
Last edited by Dickee; 04-13-2004 at 04:04 AM.
-
OK .. I think this will do it for now!
This try returns verbatum, except for the formatting escape characters (\n, \t, \", and \u002c), however, they display when the string requires them in the 'splice' array elements:
Code:
var mirror_arr:Array = [
"var mirror_arr:Array = [",
"\n\t\"first\"\u002c",
"\n\t\"second\"\u002c",
"\n\t\"];\"\u002c",
"\nmirror_arr.splice(1,0,\"\\n\\t\"+mirror_arr[0]+\"\\u002c\");\"\u002c",
"\nmirror_arr.splice(5,0,\"\\n\\t\\\"trace(mirror_arr.join(\\\"\\\"));\\\"\");\"\u002c",
"\nmirror_arr.splice(6,0,\"\\n];\");\"\u002c",
"\nmirror_arr.splice(5,0,\"\\n\\t\\\"\"+mirror_arr[7].substr(1)+\"\\n\\t\\\"\"+mirror_arr[8].substr(1)+\"\\n\\t\\\"\"+mirror_arr[9].substr(1)+\"\\n\\t\\\"\"+mirror_arr[10].substr(1));\"\u002c",
"\ntrace(mirror_arr.join(\"\"));"
];
mirror_arr.splice(1,0,"\n\t\""+mirror_arr[0]+"\"\u002c");
mirror_arr.splice(5,0,"\n\t\"trace(mirror_arr.join(\"\"));\"");
mirror_arr.splice(6,0,"\n];");
mirror_arr.splice(5,0,"\n\t\""+mirror_arr[7].substr(1)+"\n\t\""+mirror_arr[8].substr(1)+"\n\t\""+mirror_arr[9].substr(1)+"\n\t\""+mirror_arr[10].substr(1));
trace(mirror_arr.join(""));
returns:
Code:
var mirror_arr:Array = [
"var mirror_arr:Array = [",
"first",
"second",
"];",
"mirror_arr.splice(1,0,"\n\t"+mirror_arr[0]+"\u002c");",
"mirror_arr.splice(5,0,"\n\t\"trace(mirror_arr.join(\"\"));\"");",
"mirror_arr.splice(6,0,"\n];");",
"mirror_arr.splice(5,0,"\n\t\""+mirror_arr[7].substr(1)+"\n\t\""+mirror_arr[8].substr(1)+"\n\t\""+mirror_arr[9].substr(1)+"\n\t\""+mirror_arr[10].substr(1));",
"trace(mirror_arr.join(""));"
];
mirror_arr.splice(1,0,"\n\t"+mirror_arr[0]+"\u002c");",
mirror_arr.splice(5,0,"\n\t\"trace(mirror_arr.join(\"\"));\"");",
mirror_arr.splice(6,0,"\n];");",
mirror_arr.splice(5,0,"\n\t\""+mirror_arr[7].substr(1)+"\n\t\""+mirror_arr[8].substr(1)+"\n\t\""+mirror_arr[9].substr(1)+"\n\t\""+mirror_arr[10].substr(1));",
trace(mirror_arr.join(""));
Richard
Last edited by Dickee; 04-13-2004 at 07:28 AM.
-
Senior Member
Pretty darn close!
The output needs to be identical (you need to be able to run the output and get the same result).
When I've done this in other languages, I've generally found it helpful to make variables to stand for the escaped characters.
So you have something like:
ret = String.fromCharCode(13);
tab = String.fromCharCode(10);
and then just use ret and tab in the program.
This page contains a number of samples in other languages, including one in Javascript, which is pretty close to what we need...
Self-reproducing programs
-
Senior Member
Here's my contribution:
Code:
q=String.fromCharCode(34);
r=String.fromCharCode(10);
x=String.fromCharCode(81);
y=String.fromCharCode(82);
z=String.fromCharCode(68);
d="q=String.fromCharCode(23);Rr=String.fromCharCode(10);Rx=String.fromCharCode(81);Ry=String.fromCharCode(82);Rz=String.fromCharCode(68);Rd=QDQ;Rtrace(d.split(x).join(q).split(y).join(r).split(z).join(d));";
trace(d.split(x).join(q).split(y).join(r).split(z).join(d));
I didn't bother dropping the semicolons.
Last edited by jbum; 04-13-2004 at 02:17 PM.
-
Senior Member
Here's a shorter variant. I eliminated the returns, and used a substitution for the wordy String.fromCharCode.
Code:
a=String.fromCharCode;q=a(34);d="a=String.fromCharCode;q=a(23);d=QDQ;trace(d.split(a(81)).join(q).split(a(68)).join(d));";trace(d.split(a(81)).join(q).split(a(68)).join(d));
-
Senior Member
amusing heh?
well since this is an amusing task here's an amusing way to solve it:
Code:
Scene=Scene 1, Layer=Layer 1, Frame=1: Line 1: ';' expected
Scene=Scene 1, Layer=Layer 1, Frame=1: Line 1: ';' expected
its true, you did say via trace (which i haven't used) but on the other hand, the output is traced in the Output window
Anyways, great issue.
bye
-
Senior Member
Oh that's funny. Took me a minute to figure out what you meant, but DEFINITELY a creative solution to the problem!
-
I dont know if this is cheating but....
in an external text file "myQuine.txt":
Code:
var myLV : LoadVars = new LoadVars();
myLV.onData =
function(src)
{
trace(src);
}
myLV.load("myQuine.txt");
my program code:
Code:
var myLV : LoadVars = new LoadVars();
myLV.onData =
function(src)
{
trace(src);
}
myLV.load("myQuine.txt");
output:
Code:
var myLV : LoadVars = new LoadVars();
myLV.onData =
function(src)
{
trace(src);
}
myLV.load("myQuine.txt");
Am I thinking too far outside of the box?
-
Senior Member
I'd say that's cheating
Similar to the old BASIC solution to this problem.
10 LIST
- jim
-
Senior Member
I read something on this a while back and made one. Here's mine anyway.
Code:
var s=String.fromCharCode(39);
var data='trace("var s=String.fromCharCode(39);"); trace("var data="+s+data+s+";"); trace(data);';
trace("var s=String.fromCharCode(39);"); trace("var data="+s+data+s+";"); trace(data);
Anyone for making one that reads the same backwards as forwards (I forget the name)? I've seen it done in C.
Last edited by Penleeki; 04-26-2004 at 10:45 PM.
-
Spelunker
Originally posted by Penleeki
I read something on this a while back and made one. Here's mine anyway.
Code:
var s=String.fromCharCode(39);
var data='trace("var s=String.fromCharCode(39);"); trace("var data="+s+data+s+";"); trace(data);';
trace("var s=String.fromCharCode(39);"); trace("var data="+s+data+s+";"); trace(data);
Anyone for making one that reads the same backwards as forwards (I forget the name)? I've seen it done in C.
You mean a palindrome?
-
Senior Member
Yeah, I've seen them done as palindromes in C. I believe there were some tricks played with comments in the code, where the contents of a line read forwards as a comment, but backwards as executable...
Oy...
- Jim
-
Senior Member
Thats the one
-
Senior Member
-
yEa! Hi there!
Yup! Highly entertaining matey! Couldnt get me fortune tho' with Magic 8-Ball just kept 'loading' Perhaps I'm a bit too elusive what with all this baggage (chainmail sweaters) and swanning thru' mystic eons etc lol cough cough! Hey! Props! Go esy wid der dry ice, wouldcha? Oooops! soz JB - oh yea, tanx a bunch fer de graphics, got any more, please?
Swooshin' regards
'The'Highlandar
-
I'm far too much of a novice to fully sink my teeth into these... but it seems like this idea could be used to write scripts that change, rather than adhering strictly to exactly duplicating themselves. Have you done any work with "genetic algorithms" (programs that evolve a solution)?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|