%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                          %
% Three thieves have robbed a wine barrel of 24 liters.    %
% They would like to divide the wine in three equal parts  %
% (8 liters each). Unfortunately, they only have at their  %
% disposal three vessels : one of 5 liters,                %
% one of 11 liters and one of 13 liters.                   %
%                                                          %
% Write a prolog program to solve this decanting problem.  %
%                                                          %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


:- set_prolog_flag(toplevel_print_options,
                   [quoted(true), portray(true), max_depth(0)]).

:- use_module(search).



thief(Ls) :-
    solve([0, 0, 0, 24], Ls).

solve(A, B) :-
    df_id(A, B).




goal([0, 8, 8, 8]).

s(St1, St2) :-
    nth0(A, St1, Cont1),
    nth0(B, St1, Cont2),
    A \= B,
    capacity(B, Cap2),
    Cont2 < Cap2,
    Liters is Cap2 - Cont2,
    Use is min(Liters, Cont1),
    Rest is Cont1 - Use,
    NewCont2 is Cont2 + Use,
    setN(St1, A, Rest, St3),
    setN(St3, B, NewCont2, St2).


capacity(0, 5).
capacity(1, 11).
capacity(2, 13).
capacity(3, 24).


% Set the nth value in the list
setN([_|Ls], 0, E, [E|Ls]) :- !.
setN([X|Ls], N, E, [X|Zs]) :-
    N1 is N-1,
    setN(Ls, N1, E, Zs).

