Infix to RPN

Score: 189.56 (fail)

hurrah, some semblance of respectability

Hangs on test1.

#!perl -pl
$*='(?:-?\d+(?: (?:[-*\/+]|-?\d+))*)';sub a{$_=~$1?$1:$`;0while s/\(([^()]+)\)/&a/e;s&($*(?:[*/]$*)*)([*/])($*)&a(" $3 $2")⪚$_=a(" $3 $2")if/()([-+])($*)$/;"$_@_";}s/\s//g+s/(.*)/a/e

Score: 194.56 (fail)

hurrah, some semblance of respectability

Sorry, the segfault persists. The problematic piece of code is: s&($*(?:[*/]$*)*)([*/])($*)&a(" $3 $2")⪚

#!perl -pl
$*='(?:-?\d+(?: (?:[-*\/+]|-?\d+))*)';sub a{$_=$1;0while s/\(([^()]+)\)/&a/e;s&($*(?:[*/]$*)*)([*/])($*)&a(" $3 $2")⪚$_=a(" $3 $2")if/^(.*(?:\d| .))([-+])($*)$/;"$_@_";}s/\s//g+s/(.*)/a/e

Score: 206.56 (pass)

sub 200 would be tolerable

#!perl -pl
sub a{$_=$1;0while s/\(([^()]+)\)/$0[++$-]=&a;"%$-"/e;s&([^+	]*\d)([*/])([^+	]+)&a(" $3 $2")⪚$_=a(" $' $2")if/^(.+)([+	])/;"$_@_";}s/\s//g+s/(\d|\))-/$1	/g+s/(.*)/a/e;s/%(\d+)/$0[$1]/ewhile/%/+y/	/-/

Score: 207.57 (pass)

sub 200 would be tolerable

#!perl -pl
sub a{$_=$1;0while s/\(([^()]+)\)/$0[++$-]=&a;"%$-"/e;s&([^+	]*\d)([*/])([-%\d]+)&a(" $3 $2")⪚$_=a(" $' $2")if/^(.+)([+	])/;"$_@_";}s/\s//g+s/(\d|\))-/$1	/g+s/(.*)/a/e;s/%(\d+)/$0[$1]/ewhile/%/+y/	/-/

Score: 208.53 (fail)

so much effort, so little progress

Running test 47 ... Program rpn.pl killed by signal (11) consistently dies on test47

#!perl -pl
$*='(?:-?\d+(?> (?:[	*\/+]|-?\d+))*)';sub a{$_=$1;0while s/\(([^()]+)\)/&a/e;s&($t(?:[*/]$*)*)([*/])($*)&a(" $3 $2")⪚$_=a(" $3 $2")if/^(.*)([+	])($*)/;"$_@_";}s/\s//g+s/(\d|\))-/$1	/g+s/(.*)/a/e+y/	/-/

Score: 218.57 (pass)

I see the light

#!perl -pl
sub a{$_=$1;0while s/\(([^()]+)\)/$0[++$-]=&a;"%$-"/e;s&([^+	]+)([*/])([-%\d]+)&$0[++$-]=a(" $3 $2");"%$-"⪚$_=a(" $' $2")if/(.+)([+	])/;"$_@_"}s/\s//g+s/(\d|\))-/$1	/g+s/(.*)/a/e;s/%(\d+)/$0[$1]/ewhile/%/|y/	/-/

Score: 222.58 (pass)

nearly down to double figures :)

#!perl -pl
sub a{$_=$1;0while s/\(([^()]+)\)/$0[++$-]=&a;"%$-"/e;s&([^+~]+)([*/])([-%\d]+)&$0[++$-]=a(" $3 $2");"%$-"⪚s/(.+)([+~])(.+)/a(" $3 $2")/e;$_.="@_"}s/\s//g;s/(\d|\))-/$1~/g;s/(.*)/a/e;s/%(\d+)/$0[$1]/ewhile/%/|y/~/-/

Score: 263.69 (fail)

guess this approach won't get too close to 104 then...

Segfaults on 5.6.1 on Linux. Probably the (??{}).

#!perl -pl
sub t{$_=pop;s/\((.*)\)/a($1)/e;"$_@_"}$b=qr/\((?:(?>[^()]+)|(??{$b}))*\)/;$p=qr/$b|-?\d+/;sub p{$_=shift;/(\*|\/)($p)$/?p("$`",'',t(" $1@_",$2)):t@_,$_}sub a{$_=shift;/$p([-+]($p(?:[*\/]$p)*))+$/&&/(.)(\Q$+\E)$/?a("$`",'',p($2," $1@_")):p$_,@_}y/ 	//d;$_=a$_

Score: 266.69 (pass)

guess this approach won't get too close to 104 then...

#!perl -pl
sub t{$_=pop;s/\((.*)\)/a($1)/e;"$_@_"}$b=qr/\((?:(?>[^()]+)|(??{$b}))*\)/;$p=qr/$b|-?\d+/;sub p{$_=shift;/(\*|\/)($p)$/?p("$`",'',t(" $1@_",$2)):t@_,$_}sub a{$_=shift;/[\d)]([-+]($p(?:[*\/]$p)*))+$/&&/(.)(\Q$+\E)$/?a("$`",'',p($2," $1@_")):p$_,@_}y/ 	//d;$_=a$_

Score: 276.71 (pass)

parsing by regex still sucks

#!perl -pl
sub t{$_=pop;/^\((.*)\)$/?a($1,@_):"$_@_"}$b=qr{\((?:(?>[^()]+)|(??{$b}))*\)};$p=qr{$b|-?\d+};sub p{$_=shift;/(\*|\/)($p)$/?p("$`",'',t(" $1@_",$2)):t@_,$_}sub a{$_=shift;/(?<=[\d)])(([-+])($p(?:[*\/]$p)*))+$/&&/(.)(\Q$+\E)$/?a("$`",'',p($2," $1@_")):p$_,@_}s/\s//g;$_=a$_

Score: 289.71 (pass)

parsing by regex sucks

#!perl -pl
sub t{$_=shift;/^\((.*)\)$/?a($1,@_):"$_@_"}$b=qr{\((?:(?>[^()]+)|(??{$b}))*\)};sub p{$_=shift;/(\*|\/)($b|-?\d+)$/?p("$`",'',t($2," $1@_")):t $_,@_}$p=qr{(?:$b|-?\d+)};sub a{$_=shift;/(?<=[\d)])(([-+])($p(?:[*\/]$p)*))+$/&&/(.)(\Q$+\E)$/?a("$`",'',p($2," $1@_")):p$_,@_}s/\s//g;$_=a$_