Water Jug Problem
You are given two jugs, a 5-gallon one and a 2-gallon one. Neither has any measuring markers on it. The 5-gallon jug is initially filled up completelu. How can you get exactly 2 gallons of water into the 2-gallon jug?
:- op(900,fy,'not').
?-op(100,yfx,':').
solve(Goal, Goal, Path, Path).
solve(Current, Goal, Path, Solution)
:-
edge(Step, Current, New),
not marked(solve(New, Goal, _, _)),
solve(New, Goal, Path:Step, Solution).
edge(pour_a_down_drain, A:B , 0:B).
edge(pour_b_down_drain, A:B , A:0).
edge(pour_a_into_b, A:B, C:D) :-
A>0, B<2, T is A+B, (T>=2, C is T-2, D=2 ; T<2, C=0, D=T) .
edge(pour_b_into_a, A:B, C:D) :-
B>0, A<5, T is A+B, (T>=5, D is T-5, C=5 ; T<5, C=T, D=0) .
reset_marked :- retractall(marked(_)),
asserta( (marked(X) :- asserta((marked(X):- !)), fail) ).
jug_problem(X,Y,Z) :- reset_marked, solve(X,Y,start,Z).
?-jug_problem(5:0, A:1, Answer).