place(aberdeen).
place(dundee).
place(edinburgh).
place(glasgow).
place(kirkaldy).
place(st_andrews).

miles(aberdeen, dundee, 60).
miles(dundee, edinburgh, 60).
miles(st_andrews, edinburgh, 45).
miles(dundee, st_andrews, 10).
miles(dundee, aberdeen, 60).
miles(st_andrews, kirkaldy, 30).
miles(kirkaldy, edinburgh, 35).
miles(glasgow, edinburgh, 45).

miles_between(X, Y, V) :- miles(X, Y, V).
miles_between(X, Y, V) :- miles(Y, X, V).
long_miles_between(X, Y, V) :- miles_between(X, Y, V).
long_miles_between(X, Y, V) :- miles_between(X, Z, DA), long_miles_between(Z, Y, DB), V is DA+DB.

no_contains(_, []).
no_contains(H, [H|T]) :- fail.
no_contains(E, [H|T]) :- E \= H, no_contains(E, T).

nolop_dist_between(X, X, [], 0).
nolop_dist_between(X, Y, _, V) :- miles_between(X, Y, V).
nolop_dist_between(X, Y, L, V) :-
        miles_between(X, Z, VA),
        no_contains(N, L),
        nolop_dist_between(Z, Y, [H|L], VB),
        V is VA + VB.

