Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Golf: Cabbage, Goat, Wolf

by educated_foo (Vicar)
on Jan 07, 2014 at 02:07 UTC ( #1069602=note: print w/replies, xml ) Need Help??


in reply to Re: Golf: Cabbage, Goat, Wolf
in thread Golf: Cabbage, Goat, Wolf

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

Replies are listed 'Best First'.
Re^3: Golf: Cabbage, Goat, Wolf
by hdb (Prior) on Jan 07, 2014 at 14:19 UTC

    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.)

        The question is where you want to draw the line between finding the solution and providing information about the solution. Based on the encoding of the states of the world used in the code above (represented by $s), the expression

        grep!$s{$_}&!($s&8&&~$s&$_&7)&!(~$s&8&&$s&~$_&7)&&($z=$s^$_)&8&&(($z&= +7)<3|$z==4),15,grep$_%3,1..14

        is used to find the next possible moves based on the rules given. It constructs the following implicit tree:

        0-8 11-1 7-15 \ / \ / 10-2 13-5 \ / 14-4

        Already, I have excluded states 3, 6, 9 and 12 as in thoses states something gets eaten. Looking at this tree, the problem is to find a path from "0" to "15" which is rather simple.

        The question is how much of this information is allowed to be input to the script rather being calculated. In the extreme, one could just provide the two solutions and find the shortest script to print them without having to bother moving boats, wolfs, cabbages and goats. But that would be not very interesting...

        And to clarify: the grep expression is not an obfuscated version of the tree but finds possible moves based upon the rules ($s is where we are, $_ a possible next state):

        grep !$s{$_} # we have not been at $_ already &!($s&8 # prohibit the boat is on the right bank initia +lly &&~$s&$_&7) # and a move from left to right &!(~$s&8 # prohibit the boat is on the left bank initial +ly &&$s&~$_&7) # and a move from right to left &&($z=$s^$_)&8 # the boat moves &&(($z&=7)<3|$z==4), # with at most one passenger 15,grep$_%3,1..14 # possible choices for moves

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1069602]
help
Chatterbox?
[stevieb]: choroba my GPS home device is done, at least prototype v1 is. I'm just making sure all of my code does what it says now
[stevieb]: I test/prototype code on my Pi, then I migrate it over to Arduino when it's ready (if that's a better platform for what I'm doing). The whole RPi stuff was for learning at first, then it snowballed. I'm just trying to get my in-house CI...
[stevieb]: ...done, which obviously requires a specific hardware setup.
[stevieb]: I don't write tests for the lower-level distributions (ie. ICs, sensors etc) within those distributions, I have all tests in a master distribution which encompasess all of the sub-modules

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2017-06-25 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (572 votes). Check out past polls.