in reply to Good and bad news
in thread Is Perl less compact than Ruby? (Kind of competition)

is 342 bytes

map{push@L,@C+0;push@C,lc=~/./g}<>;sub p{pop@n}sub w{push@n,@_}while($ +n<@C){$_= $C[$n++];if(/h/){$_=p;w$n[-1-abs];$_>0&&splice@n,-2-$_,1}if(/n/){w 0;/ +-/||w$_+7 *p while($_=index"htaoinse",$C[$n++])<7}$0=p,$_=p,w int$_/$0,$_%$0if/e +/;if(/t/) {$_=p;$_?$n=$L[$_-1]:last if p}/a/?w 1+grep$n>$_,@L:/o/?print chr p:/s +/?w-(p)+p :/i/?w+($_=getc)?ord:-1:0}

Beg you for another 26 bytes, is it possible?

Replies are listed 'Best First'.
Re: last version
by grinder (Bishop) on Feb 19, 2002 at 11:44 UTC
    Down to 340 bytes:

    map{push@L,@C+0;push@C,lc=~/./g}<>;sub p{pop@n}sub w{push@n,@_}while($ +n<@C){$_= $C[$n++];if(/h/){$_=p;w$n[-1-abs];$_>0&&splice@n,-2-$_,1}if(/n/){w 0;/ +-/||w$_+7 *p while($_=index"htaoinse",$C[$n++])<7}$0=p,$_=p,w int$_/$0,$_%$0if/e +/;if(/t/) {$_=p;$_?$n=$L[$_-1]:last if p}/a/?w 1+grep$n>$_,@L:/o/?print chr p:/s +/?w-(p)+p :/i/&&w($_=getc)?ord:-1}

    We can s/last if p/p&&last/ and that brings it own to 338 bytes:

    map{push@L,@C+0;push@C,lc=~/./g}<>;sub p{pop@n}sub w{push@n,@_}while($ +n<@C){$_= $C[$n++];if(/h/){$_=p;w$n[-1-abs];$_>0&&splice@n,-2-$_,1}if(/n/){w 0;/ +-/||w$_+7 *p while($_=index"htaoinse",$C[$n++])<7}$0=p,$_=p,w int$_/$0,$_%$0if/e +/;if(/t/) {$_=p;$_?$n=$L[$_-1]:p&&last}/a/?w 1+grep$n>$_,@L:/o/?print chr p:/s/? +w-(p)+p: /i/&&w($_=getc)?ord:-1}

    But wait, there's more! change $0=p,$_=p,w int$_/$0,$_%$0 to w int($_=p)/($0=p),$_%$0} Which gives 337 (I think) bytes:

    map{push@L,@C+0;push@C,lc=~/./g}<>;sub p{pop@n}sub w{push@n,@_}while($ +n<@C){$_= $C[$n++];if(/h/){$_=p;w$n[-1-abs];$_>0&&splice@n,-2-$_,1}if(/n/){w 0;/ +-/||w$_+7 *p while($_=index"htaoinse",$C[$n++])<7}w int($_=p)/($0=p),$_%$0if/e/; +if(/t/) {$_=p;$_?$n=$L[$_-1]:p&&last}/a/?w 1+grep$n>$_,@L:/o/?print chr p:/s/? +w-(p)+p: /i/&&w($_=getc)?ord:-1}

    A ternary operator can be replaced by a && and the plus in w+($_=getc)?ord:-1 trick is not needed, it works without (or, at least, hello.eta still outputs correctly).

    I have an idea it must be possible to replace the w$_+7*p while($_=index"htaoinse",$C[$n++])<7 section with a redo and/or a pos, but it is so difficult to understand what the program does I am beginning to wonder whether it wouldn't be easier to look at the instruction set and come up with another angle of attack. For instance, it would be nice to be able to do away with the @L array as well. Replace @C and @L by a hash that is keyed on byte offset and the value is the byte.


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

      final && is great!

      hate to disappoint you, i see some op-precedence problems on other suggestions:

      • $_?$n=$L[$_-1]:p&&last   doesn't work cause "&&" is higher than "?:"
      • int($_=p)/($0=p),$_%$0   is wrong, needed "int" of "/" not vice versa
      • w($_=getc)?ord:-1   is wrong, needed "w" of "?:" not vice versa