Advertisement
Guest User

PLOG teste 1 1819 migas

a guest
Jan 29th, 2019
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 6.32 KB | None | 0 0
  1. %%%%%%%%% 1
  2.  
  3. short(Flight) :-
  4.     flight(Flight, _, _, _, Duration, _),
  5.     Duration < 90.
  6.  
  7. %%%%%%%%% 2
  8.  
  9. earlier(Flight1, Flight2, EarlierFlight) :-
  10.     flight(Flight1, _, _, Departure1, _, _),
  11.     flight(Flight2, _, _, Departure2, _, _),
  12.     Departure1 =\= Departure2, !,
  13.     decide_earlier(Flight1, Departure1, Flight2, Departure2, EarlierFlight).
  14.  
  15. decide_earlier(Flight1, Departure1, _Flight2, Departure2, Flight1) :-
  16.     Departure1 < Departure2, !.
  17.  
  18. decide_earlier(_, _, Flight2, _, Flight2).
  19.  
  20. %%%%%%%%% 3
  21.  
  22. countries(Company, ListOfCountries) :-
  23.     countries_aux(Company, [], ListOfCountries).
  24.    
  25. countries_aux(Company, CurrCountries, ListOfCountries) :-
  26.     operates_in_country(Company, Country),
  27.     \+ member(Country, CurrCountries), !,
  28.     append(CurrCountries, [Country], CurrCountries2),
  29.     countries_aux(Company, CurrCountries2, ListOfCountries).
  30.  
  31. countries_aux(_, ListOfCountries, ListOfCountries).
  32.    
  33. operates_in_country(Company, Country) :-
  34.     flight(_, Origin, _, _, _, Company),
  35.     airport(_, Origin, Country).
  36.  
  37. operates_in_country(Company, Country) :-
  38.     flight(_, _, Destination, _, _, Company),
  39.     airport(_, Destination, Country).
  40.  
  41. %%%%%%%%% 4
  42.  
  43. arrivalTime(Flight, ArrivalTime) :-
  44.     flight(Flight, _, _, Departure, Duration, _),
  45.     DepartureMinutes is (Departure mod 100),
  46.     DepartureHours is floor(Departure / 100) * 60,
  47.     ArrivalTimeRaw is DepartureMinutes + DepartureHours + Duration,
  48.     write(ArrivalTimeRaw),
  49.     % Hours, then minutes
  50.     ArrivalTime is floor(ArrivalTimeRaw / 60) * 100 + (ArrivalTimeRaw mod 60).
  51.  
  52. %%%%%%%%% 5
  53.  
  54. % Incomplete (1/2)
  55. tripDays([TripH | TripT], Time, FlightTimes, Days) :-
  56.     tripDays_aux(TripH, TripT, Time, [], FlightTimes, 0, Days).
  57.    
  58. % Base case, trip is over (Reached only 1 element in list, it is the final destination
  59. tripDays_aux(_, [_X], _, FlightTimes, FlightTimes, Days, Days).
  60.  
  61. tripDays_aux(From, [To | TripT], CurrDepatureTime, CurrTimes, FlightTimes, CurrDays, Days) :-
  62.     % We want a flight that goes from and to where we want it and that departs later than the time we are ready at
  63.     flight(DesiredFlight, From, To, DepartureTime, _, _),
  64.     DepartureTime >= CurrDepatureTime, !,
  65.     arrivalTime(DesiredFlight, ArrivalTimeRaw),
  66.     ArrivalTimeWithExtra is ArrivalTimeRaw + 30,
  67.     append(CurrTimes, [DepartureTime], CurrTimes2),
  68.     tripDays_aux(To, TripT, ArrivalTimeWithExtra, CurrTimes2, FlightTimes, CurrDays, Days).
  69.    
  70. % no more flights can be found in the same day, go to next day (adding 1 day to the counter and setting CurrDepatureTime to the start of the day
  71. tripDays_aux(From, TripList, _, CurrTimes, FlightTimes, CurrDays, Days) :-
  72.     CurrDays2 is CurrDays + 1,
  73.     tripDays_aux(From, TripList, 0, CurrTimes, FlightTimes, CurrDays2, Days).
  74.  
  75. %%%%%%%%% 6
  76.  
  77. %incomplete (1.9/2)
  78. pairableFlights :-
  79.     pairableFlights_aux([]).
  80.    
  81. get_in_minutes(Time, Mins) :-
  82.     Mins is floor(Time / 100) * 60 + (Time mod 100).
  83.    
  84. pairableFlights_aux(CurrPaired) :-
  85.     % Flight1 with destination Airport that has an arrival time that is less than Flight2's departure time, with a margin of 30-90 minutes
  86.     % Flight2 must also have origin Airport
  87.     flight(Flight1, _, Airport, _, _, _),
  88.     arrivalTime(Flight1, ArrivalTime1),
  89.     flight(Flight2, Airport, _, Departure2, _, _),
  90.     get_in_minutes(ArrivalTime1, AT1),
  91.     get_in_minutes(Departure2, D2),
  92.     D2 - AT1 >= 30,
  93.     D2 - AT1 =< 90,
  94.     \+ member(Airport-Flight1-Flight2, CurrPaired), !,
  95.     write(Airport), write(' - '), write(Flight1), write(' \\ '), write(Flight2), nl,
  96.     append(CurrPaired, [Airport-Flight1-Flight2], CurrPaired2),
  97.     pairableFlights_aux(CurrPaired2).
  98.    
  99. pairableFlights_aux([]).
  100.  
  101. %%%%%%%%% 7
  102.  
  103. :- use_module(library(lists)).
  104.  
  105. avgFlightLengthFromAirport(Airport, AvgLength) :-
  106.     findall(Duration, flight(_, Airport, _, _, Duration, _), FlightsDuration),
  107.     sumlist(FlightsDuration, Sum),
  108.     length(FlightsDuration, Count),
  109.     Count =\= 0,
  110.     AvgLength is Sum/Count.
  111.  
  112. %%%%%%%%% 8 (MUITO ESPARGUETE NÃO RECOMENDO, USAR PREDICADOS DE ANTES)
  113.  
  114. mostInternational(ListOfCompanies) :-
  115.     findall(Company, company(Company, _, _, _), CompaniesList),
  116.     findall(Company-NrCountries, (member(Company, CompaniesList), company_nr_countries(Company, NrCountries)), Companies),
  117.     get_bests(Companies, 0, [], ListOfCompanies).
  118.  
  119. % base case
  120. get_bests([], _, ListOfCompanies, ListOfCompanies).
  121.  
  122. % new best
  123. get_bests([Company-NrCountries | T], CurrBest, _CurrListOfCompanies, ListOfCompanies) :-
  124.     NrCountries > CurrBest, !,
  125.     get_bests(T, NrCountries, [Company], ListOfCompanies).
  126.  
  127. % equal to best
  128. get_bests([Company-NrCountries | T], CurrBest, CurrListOfCompanies, ListOfCompanies) :-
  129.     NrCountries = CurrBest, !,
  130.     append(CurrListOfCompanies, [Company], CurrListOfCompanies2),
  131.     get_bests(T, CurrBest, CurrListOfCompanies2, ListOfCompanies).
  132.    
  133. % less than best
  134. get_bests([_ | T], CurrBest, CurrListOfCompanies, ListOfCompanies) :-
  135.     get_bests(T, CurrBest, CurrListOfCompanies, ListOfCompanies).
  136.  
  137.    
  138. company_nr_countries(Company, NrCountries) :-
  139.     setof(Origin, OriginAirport^(flight(_, OriginAirport, _, _, _, Company), airport(_, OriginAirport, Origin)), Origins),
  140.     findall(Destination, DestinationAirport^(flight(_, _, DestinationAirport, _, _, Company), airport(_, DestinationAirport, Destination)), Destinations),
  141.     setof(Country, (member(Country, Origins) ; member(Country, Destinations)), Countries),
  142.     length(Countries, NrCountries), !.
  143.  
  144. %%%%%%%%% 9
  145.  
  146. :- use_module(library(lists)).
  147.  
  148. make_pairs(L, P, [X-Y|Zs]) :-
  149.     select(X, L, L2),
  150.     select(Y, L2, L3),
  151.     G =.. [P, X, Y], G,
  152.     make_pairs(L3, P, Zs), !.
  153.  
  154. make_pairs([], _, []).
  155.  
  156. %%%%%%%%% 10
  157.  
  158. :- use_module(library(lists)).
  159.  
  160. make_pairs(L, P, [X-Y|Zs]) :-
  161.     select(X, L, L2),
  162.     select(Y, L2, L3),
  163.     G =.. [P, X, Y], G,
  164.     make_pairs(L3, P, Zs), !.
  165.  
  166. make_pairs([_|T], P, Zs) :-
  167.     make_pairs(T, P, Zs).
  168.    
  169. make_pairs([], _, []).
  170.  
  171. %%%%%%%%%% 11
  172.  
  173. make_max_pairs(L, P, S) :-
  174.     % Obtaining all possible pairs
  175.     findall(Pairs, make_pairs(L, P, Pairs), PairsList),
  176.     % Comparing their size and picking the longest
  177.     % Starting the comparison with size -1 so that the element is swapped straight away
  178.     get_largest(PairsList, -1, [], S).
  179.    
  180. get_largest([], _, S, S).
  181.  
  182. get_largest([Pairs | T], CurrBestVal, _CurrBestPairs, S) :-
  183.     length(Pairs, Len),
  184.     Len > CurrBestVal, !,
  185.     get_largest(T, Len, Pairs, S).
  186.    
  187. get_largest([_ | T], CurrBestVal, CurrBestPairs, S) :-
  188.     get_largest(T, CurrBestVal, CurrBestPairs, S).
  189.  
  190. %%%%%%%%% 12
  191. (rip)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement