Skip to content

Instantly share code, notes, and snippets.

@mark-d-holmberg
Created September 7, 2011 21:09
Show Gist options
  • Save mark-d-holmberg/1201737 to your computer and use it in GitHub Desktop.
Save mark-d-holmberg/1201737 to your computer and use it in GitHub Desktop.
The Man, The Wolf, The Goat, and The Cabbage
% See the Gist Here
% https://gist.github.com/1201748
change(e,w).
change(w,e).
move([X,X,Goat,Cabbage],wolf,[Y,Y,Goat,Cabbage]) :-
change(X,Y).
move([X,Wolf,X,Cabbage],goat,[Y,Wolf,Y,Cabbage]) :-
change(X,Y).
move([X,Wolf,Goat,X],cabbage,[Y,Wolf,Goat,Y]) :-
change(X,Y).
move([X,Wolf,Goat,C],nothing,[Y,Wolf,Goat,C]) :-
change(X,Y).
oneEq(X,X,_).
oneEq(X,_,X).
safe([Man,Wolf,Goat,Cabbage]) :-
oneEq(Man,Goat,Wolf),
oneEq(Man,Goat,Cabbage).
solution([e,e,e,e],[]).
solution(Config,[Move|Rest]) :-
move(Config,Move,NextConfig),
safe(NextConfig),
solution(NextConfig,Rest).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment