obfuscated
educated_foo
I was reminded by a recent Reddit thread of the classic "get a goat, wolf, and cabbage across a river" problem (<a href="http://www.mathcats.com/explore/river/crossing.html">spazzy kittens version</a>, <a href="http://blog.jle.im/entry/wolf-goat-cabbage-the-list-monadplus-logic-problems">painfully verbose Haskell version</a>). You're on the left side of a river with a cabbage, goat, and wolf, and want to get them to the right side. The wolf will eat the goat, and the goat the cabbage, but only if you leave them alone. You can only fit one of them in your boat at a time.
<p>The problem can be solved by computer with some backtracking, or by hand with some thought. Being a Perl programmer, I naturally thought to golf it, and thought that the best solution would involve a clever regex or substitution. Here's a terse, but un-obfuscated version:
<code>
sub wgc {
return if $seen{"@_"}++;
my%x=@_;
if ($x{b} && $x{c} && $x{g} && $x{w}) {
print+(sort keys%$_),"\n" for @h;
exit;
} elsif ((!$x{b} && ($x{c} && $x{g} || $x{g} && $x{w})) ||
($x{b} && (!$x{c} && !$x{g} || !$x{g} && !$x{w}))) {
return;
} else {
if ($x{b}) {
delete $x{b};
for ('xx', keys %x) {
my %y=%x;
delete $y{$_};
local @h=(@h, \%y);
wgc(%y);
}
} else {
$x{b}=1; {
local (@h) = (@h, \%x);
wgc(%x);
}
for my $k (qw(c g w)) {
if (!$x{$k}) {
my %y=(%x,$k,1);
local (@h) = (@h, \%y);
wgc(%y);
};
}
}
}
}
wgc
</code>
And here's the output, where "b", "c", "g", and "w" represent the boat, cabbage, goat, and wolf being on the right bank:
<code>
bg
g
bcg
c
bcw
cw
bcgw
</code>
I wasn't clever enough to come up with the regex solution, but here's a compressed version of the above, weighing in at <strike>382</strike>379 strokes:
<code>
sub w{return if$s{"@_"}++;my%x=@_;if($x{b}&$x{c}&$x{g}&$x{w}){print+(sort keys%$_),"\n"for@h;exit;}elsif(($x{b}||!($x{c}&&$x{g}||$x{g}&&$x{w}))&&(!$x{b}||!(!$x{c}&&!$x{g}||!$x{g}&&!$x{w}))){if($x{b}){delete$x{b};for(A,keys%x){my%y=%x;delete$y{$_};local@h=(@h,\%y);w(%y)}}else{$x{b}=1;{local@h=(@h,\%x);w(%x);}for(qw(c g w)){if(!$x{$_}){my%y=(%x,$_,1);local@h=(@h,\%y);w(%y)}}}}}w
</code>
Have at it!