Eval Squared
zshzn
created: 2006-04-05 20:09:42
This isn't very original, nor very good in general. Yet I thought I'd throw it up here and see what the monastery thinks. Consider two challenges, the first to figure out what's happening and how to use this, the second to find a mistake in the implementation.
$_=q&pqfo%;-tijgu<%0>voefg<%`>=%;?(/)mfohui)%2*03*/#<#"hfy<
t"))@;?]t*!|3-~*"(%c,>(/)mfohui)%2*03*/#<#"hfy(/)mfohui)%2*03*/#<#"hf(/)mfohui)%2*03*/#<#"hfpse)tvctu
s)=?-1-2**<"hy]t*]^]
t"~"h






Re: Eval Squared
created: 2006-04-06 02:37:22

So, it looks good. First pass, can't parse. Fine. perl -MO=Deparse doesn't help either. So I try a new trick from [liverpole|a fellow monk] (instead of performing the intermediary steps manually):

$ sed s/eval/print/ zshzn.pl | perl

which yields:

open$:,shift;$/=undef;$_=<$:>;><+-.,cd;s!(.)!$1 !g;s!((?:<\s) {2,})!'$b-='.(length($1)/2).";"!gex;s!((?:>\s) {2,})!'$b+='.(length($1)/2).";"!gex;s!((?:\+\s){2,})!'$a[$b]+='.(length($1)/2).";"!ge;s!((?:\-\s){2,})!'$a[$b]-='.(length($1)/2).";"!ge;s!>\s !\$b++;!gx;s!<\s !\$b--;!gx;s!([+-])\s!\$a[\$b]$1$1;\n!g;s!\.\s!print chr(\$a[\$b]);!g;s!,\s !\$a[\$b]=ord(substr(<>,0,1));!gx;s!\[\s!while(\$a[\$b]){!g;s!(?<=\s)\]\s!}!g;print;

Now, I don't particularly feel like formatting all that by hand, so I:

$ s/eval/print/ | perl | perl -MO=Deparse

which yields:

syntax error at - line 1, near ";>"
- had compilation errors.

So I fail challenge #1.

So I try the following:

$ perl zshzn.pl
$ perl zshzn.pl file.txt
$ cat file.txt | perl zshzn.pl
$ perl zshzn.pl 10

All of which do nothing.

So the answer to #1 changes to: you're just doing an expensive noop that compiles cleanly the first time around. You produce no output by default, or when given input by a number of means. So I think that makes the answer to #2: the mistake in the implementation is that you've forgotten to do anything!!! :-)

So as far as what this member of the monastery thinks, it reminds of the dirty joke about the man who goes to the doctor looking awful but feeling great... But in this case, the appearance is quite titillating, but serves no useful practical purpose to me, which almost makes me expect to see another obfuscation, equally as appealing, abreast this one.



--chargrill
$,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse split//=>$*
){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$#C]=$/;($#C
>$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^$$C[$%++]}
Re^2: Eval Squared
created: 2006-04-06 05:27:07
syntax error at - line 1, near ";>"
- had compilation errors.
That's odd. What I get is:
sed s/eval/print/ t.pl | perl | perl -MO=Deparse
open $:, shift @ARGV;
$/ = undef;
$_ = <$:>;
tr/+-.<>[]//cd;
s/(.)/$1 /g;
s((?:<\s) {2,})'$b-=' . length($1) / 2 . ';';egx;
s((?:>\s) {2,})'$b+=' . length($1) / 2 . ';';egx;
s((?:\+\s){2,})'$a[$b+=' . length($1) / 2 . ';';]eg;
s((?:\-\s){2,})'$a[$b-=' . length($1) / 2 . ';';]eg;
s/>\s /\$b++;/gx;
s/<\s /\$b--;/gx;
s/(+-)\s/\$a\$b$1$1;\n/g;
s/\.\s/print chr(\$a\$b);/g;
s/,\s /\$a\$b=ord(substr(<>,0,1));/gx;
s/\\s/while(\$a[\$b){/g;
s/(?<=\s)\]\s/}/g;
eval $_;
This looks like a simple Brainfuck interpreter. Which makes me think the first mistake is tr/+-.<>[]//cd; -- it should be tr/+-.,<>[]//cd; -- and the second mistake is s/(?<=\s)\]\s/}/g; -- the (?<=\s) should be dropped. Unless I'm not seeing the whole picture.
Re^3: Eval Squared
created: 2006-04-10 18:39:54

As the original code is changing char-by-char the character for it's chr(ord - 1)... It actually only works when the user has the same character encoding of the OP...

It seems that the line you deparsed as

tr/+-.<>[]//cd;

Came to me just as

><+-.,cd;

I'll try to get more about it...

daniel
Re^2: Eval Squared
created: 2006-04-06 08:20:12
It doesn't do anything until you know how to tell it what to do.
Re: Eval Squared
created: 2006-04-10 19:12:20

Allright!

The working code is:

$_=q&pqfo%;-tijgu<%0>voefg<%`>=%;?(/)mfohui)%2*03*/#<#"hfy<
t"))@;?]t*!|3-~*"(%c,>(/)mfohui)%2*03*/#<#"hfy(/)mfohui)%2*03*/#<#"hf(/)mfohui)%2*03*/#<#"hfpse)tvctu
s)=?-1-2**<"hy

I removed the strange tr which was depending on the charset... and also removed a strange part of the end of the while block... And now...

>++++++++++>++++++++++++++++++++++++++++
+++++>++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
>+++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++>+++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++>+++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++>+++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++>+++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
+>++++++++++++++++++++++++++++++++>+++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++>++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++>+++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
>+++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
+>++++++++++++++++++++++++++++++++>+++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++>++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
+++++++++++>++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++>+++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++>++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++>++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++>+++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++>++++++++++++++++++++++++++++++++>++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++>+++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++>+++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++>+++++++++++++
++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++[.<]

This code was in the dumb way... of course there is a smarter way of using it... BTW, you must save it as a file an call it as the first parameter to the script

daniel
Re^2: Eval Squared
created: 2006-04-11 23:19:42
The tr should be left in, so that non-brainfuck characters are removed. It wasn't part of the problem.

+J+u+s+t+ 

++[->>+ 

+a+n+o+t+h+e+r+

+<<]>>++++.

-P-e-r-l-

----.>+++[-<+++++>]<.-- 

-h-a-c-k-e-r.
Re^3: Eval Squared
created: 2006-04-12 13:13:15

but as it was breaking in different charsets, I just removed it...

daniel

perlmonks.org content © perlmonks.org and chargrill, ruoso, truedfx, zshzn

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03