http://www.perlmonks.org?node_id=1069541


in reply to Golf: Cabbage, Goat, Wolf

Prints both solutions in 360 characters.

@s=(0,(grep{$_%3}1..14),15);%m=map{$t=$_,[grep{$z=($t^$_)&7;($t^$_)&8& +& !($t&8&&~$t&$_&7)&&!(~$t&8&&$t&~$_&7)&&($z<3||$z==4)}@s]}@s;sub t{@c=q +w (w g c b);join'',map{$_[0]&2**$_?$c[$_]:'_'}0..3}sub p{printf"%s %s\n" +, t(~$_),t($_)for@_;print$/}sub r{my($s,$e,$h,%s)=@_;($s==$e&&p@$h)||r($ +_ ,$e,[@$h,$_],$_=>1,%s)for grep{!$s{$_}}@{$m{$s}}}r+0,$s[-1],[0],0,1

Update: something's gone wrong in the obfu process. Pls ignore for now.

Update 2: fixed. Even without strict one needs my at times. Now improved to 358 351 characters.

Replies are listed 'Best First'.
Re^2: Golf: Cabbage, Goat, Wolf
by educated_foo (Vicar) on Jan 07, 2014 at 02:07 UTC
    Nice! The multiple uses of
    &8&&
    are a nice touch. 345 with a few stupid golf tricks:
    @s=(0,(grep$_%3,1..14),15);%m=map{$t=$_,[grep{$z=($t^$_)&7;($t^$_)&8&& +!($t&8&&~$t&$_&7)&&!(~$t&8&&$t&~$_&7)&&($z<3|$z==4)}@s]}@s;sub t{@c=q +w(w g c b);join'',map{$_[0]&2**$_?$c[$_]:'_'}0..3}sub p{printf"%s %s\ +n",t(~$_),t$_ for@_;print$/}sub r{my($s,$e,$h,%s)=@_;($s==$e&&p@$h)|r +($_,$e,[@$h,$_],$_,1,%s)for grep!$s{$_},@{$m{$s}}}r+0,$s[-1],[0],0,1

      271 but sacrifizing the nice underscores in the output:

      sub t{(sprintf('%04b',@_)=~tr/01/ w/r)&'bcgw'}sub p{printf"%s%8s$/", t(15-$_),t$_ for@_;print$/}sub r{my($s,$h,%s)=@_;($s^15||p@$h)|r($_, [@$h,$_],$_,1,%s)for grep{!$s{$_}&&!($s&8&&~$s&$_&7)&&!(~$s&8&&$s&~ $_&7)&&($z=$s^$_)&8&&(($z&=7)<3|$z==4)}15,grep$_%3,1..14}r+0,[0],0,1
        263 with some more micro-optimization (hey, that's what I do...):
        sub t{sprintf('%04b',@_)=~y/01/ w/r& bcgw}sub p{printf"%s%8s$/",t(15-$ +_),t$_ for@_;print$/}sub r{my($h,$s,%s)=@_;($s^15||p@$h)|r([@$h,$_],$ +_,$_,1,%s)for grep!$s{$_}&!($s&8&&~$s&$_&7)&!(~$s&8&&$s&~$_&7)&&($z=$ +s^$_)&8&&(($z&=7)<3|$z==4),15,grep$_%3,1..14}r[0],0,0,1
        Tricks: (1) grep with brackets usually costs an extra character; (2) barewords don't need quotes; (3) play with -- nay, mercilessly abuse! -- precedence; (4) spelling y/// as tr/// makes Baby Jesus cry.

        I wonder if one of Ton's magic formulae could be applied? (See Golf: Magic Formula for Roman Numerals.)