A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: An amusing challenge.

  1. #1
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920

    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

  2. #2
    the friendly canadian DaVulf's Avatar
    Join Date
    Feb 2004
    Location
    Singapore
    Posts
    2,017
    Wow, that's different. Good luck with that jbum. I think I'd get lost at 'trace ()'.

  3. #3
    Senior Member
    Join Date
    Mar 2000
    Posts
    472
    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

  4. #4
    Senior Member
    Join Date
    Mar 2000
    Posts
    472
    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.

  5. #5
    Senior Member
    Join Date
    Mar 2000
    Posts
    472
    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.

  6. #6
    Senior Member
    Join Date
    Mar 2000
    Posts
    472
    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.

  7. #7
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    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

  8. #8
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    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.

  9. #9
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    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));

  10. #10
    Senior Member olias32's Avatar
    Join Date
    Jan 2003
    Location
    Romania
    Posts
    126

    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

  11. #11
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Oh that's funny. Took me a minute to figure out what you meant, but DEFINITELY a creative solution to the problem!

  12. #12
    Junior Member
    Join Date
    Nov 2003
    Posts
    18

    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?

  13. #13
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    I'd say that's cheating

    Similar to the old BASIC solution to this problem.

    10 LIST

    - jim

  14. #14
    Senior Member Penleeki's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    223
    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.

  15. #15
    Spelunker Beatcow's Avatar
    Join Date
    Apr 2001
    Location
    after 5, before 4 Location::~Location(){}
    Posts
    797
    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?

  16. #16
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    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

  17. #17
    Senior Member Penleeki's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    223
    Thats the one

  18. #18
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Madam, I am adam.

    Here's a bit of palindromic, self-reproducing actionscript. Not as short as I can make it, but somewhat readable.

    Note that the buggy VBOARD code has auto-replaced some of my code with smileys...

    However, if you run the first part (the non-comments) you can see the whole thing in its pristine form.


    code:

    d="d=QDQ; //Ra=String.fromCharCode; q=a(23); r=a(10); //Rd=d.split(a(81)).join(q).split(a(82)).join(r).spl it(a(68)).join(d); //Rtrace(d+d.split('').reverse().join('')); //R"; //
    a=String.fromCharCode; q=a(34); r=a(10); //
    d = d.split(a(81)).join(q).split(a(82)).join(r).split( a(68)).join(d); //
    trace(d+d.split('').reverse().join('')); //

    // )''(nioj.)(esrever.)''(tilps.d+d(ecart
    // d(nioj.))86(a(tilps.)r(nioj.))28(a(tilps.)q(nioj.) )18(a(tilps.d=d
    // 01(a=r 32(a=q ;edoCrahCmorf.gnirtS=a
    // ;"R// )''(nioj.)(esrever.)''(tilps.d+d(ecartR// d(nioj.))86(a(tilps.)r(nioj.))28(a(tilps.)q(nioj.) )18(a(tilps.d=dR// 01(a=r 32(a=q ;edoCrahCmorf.gnirtS=aR// ;QDQ=d"=d

    Last edited by jbum; 04-27-2004 at 10:43 PM.

  19. #19
    Junior Member
    Join Date
    Apr 2004
    Posts
    1

    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

  20. #20
    Junior Member
    Join Date
    Mar 2004
    Location
    Austin, TX
    Posts
    9
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center