%  Cheapskate Discount Airlines, Inc. Flight Planner
%  By Chuck Liang


% "flight(C1,C2,D,A)" iff there's a flight from C1 to C2 leaving at hour D
% and arriving at hour A.

  flight(lax,bos,9,14).
  flight(lga,lax,7,13).
  flight(dul,bdl,19,22).
  flight(bos,dul,15,17).
  flight(phl,sfo,12,18).
  flight(sfo,lax,5,7).
  flight(phl,bos,13,16).
  flight(bos,lax,16,21).
  flight(lax,phl,12,18).
  flight(phl,bdl,20,23).

% route(A,B,DT,AT) iff there's a way to fly from A to B, departing NO
% EARLIER than hour DT, and arriving NO LATER than hour AT, with at 
% least 1 hour for connections.

route(A,B,DT,AT) :- flight(A,B,T1,T2), T1 >= DT, T2 =< AT.

route(A,B,DT,AT) :- flight(A,C,T1,T2), T1 >= DT, T2 < AT, 
	            T3 is T2 + 1,
	            route(C,B,T3,AT). 
	          
% Same as route above, but records flight plan in a list:
itinerary(A,B,DT,AT,[depart(A,T1), arrive(B,T2)]) :-
	flight(A,B,T1,T2), T1 >= DT, T2 =< AT.
itinerary(A,B,DT,AT,[depart(A,T1), arrive(C,T2)|Rest]) :-
	flight(A,C,T1,T2), T1 >= DT, T2 < AT, 
	T3 is T2 + 1,
	itinerary(C,B,T3,AT,Rest). 
	          

% flight(sfo,lax)
% flight(lax,sfo)
% ?- route(sfo,bdl)