move((E8,E5,_), 'E3 ausschuetten', (E8,E5,0)).
move((E8,_,E3), 'E5 ausschuetten', (E8,0,E3)).
move((_,E5,E3), 'E8 ausschuetten', (0,E5,E3)).

move((E8,E5,E3), 'E8 -> E3', (R8,E5,R3)) :- R3 is E3 + min(E8,3-E3), R8 is E8 - min(E8,3-E3).
move((E8,E5,E3), 'E8 -> E5', (R8,R5,E3)) :- R5 is E5 + min(E8,5-E5), R8 is E8 - min(E8,5-E5).
move((E8,E5,E3), 'E5 -> E8', (R8,R5,E3)) :- R8 is E8 + min(E5,8-E8), R5 is E5 - min(E5,8-E8).
move((E8,E5,E3), 'E5 -> E3', (E8,R5,R3)) :- R3 is E3 + min(E5,3-E3), R5 is E5 - min(E5,3-E3).
move((E8,E5,E3), 'E3 -> E5', (E8,R5,R3)) :- R5 is E5 + min(E3,5-E5), R3 is E3 - min(E3,5-E5).
move((E8,E5,E3), 'E3 -> E8', (R8,E5,R3)) :- R8 is E8 + min(E3,8-E8), R3 is E3 - min(E3,8-E8).

start((8,0,0)).
final((4,0,0)).
solution(Ms) :- start(F0), solve(F0,FL,Ms,[F0]), final(FL).

solve(F0,F0,[],_).
solve(F0,FL,[M|Ms],Fs) :- move(F0,M,F1), not(member(F1,Fs)), solve(F1,FL,Ms,[F1|Fs]).

