grep !$s{$_} # we have not been at $_ already &!($s&8 # prohibit the boat is on the right bank initially &&~$s&$_&7) # and a move from left to right &!(~$s&8 # prohibit the boat is on the left bank initially &&$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