Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%%%%%%%% 1
- short(Flight) :-
- flight(Flight, _, _, _, Duration, _),
- Duration < 90.
- %%%%%%%%% 2
- earlier(Flight1, Flight2, EarlierFlight) :-
- flight(Flight1, _, _, Departure1, _, _),
- flight(Flight2, _, _, Departure2, _, _),
- Departure1 =\= Departure2, !,
- decide_earlier(Flight1, Departure1, Flight2, Departure2, EarlierFlight).
- decide_earlier(Flight1, Departure1, _Flight2, Departure2, Flight1) :-
- Departure1 < Departure2, !.
- decide_earlier(_, _, Flight2, _, Flight2).
- %%%%%%%%% 3
- countries(Company, ListOfCountries) :-
- countries_aux(Company, [], ListOfCountries).
- countries_aux(Company, CurrCountries, ListOfCountries) :-
- operates_in_country(Company, Country),
- \+ member(Country, CurrCountries), !,
- append(CurrCountries, [Country], CurrCountries2),
- countries_aux(Company, CurrCountries2, ListOfCountries).
- countries_aux(_, ListOfCountries, ListOfCountries).
- operates_in_country(Company, Country) :-
- flight(_, Origin, _, _, _, Company),
- airport(_, Origin, Country).
- operates_in_country(Company, Country) :-
- flight(_, _, Destination, _, _, Company),
- airport(_, Destination, Country).
- %%%%%%%%% 4
- arrivalTime(Flight, ArrivalTime) :-
- flight(Flight, _, _, Departure, Duration, _),
- DepartureMinutes is (Departure mod 100),
- DepartureHours is floor(Departure / 100) * 60,
- ArrivalTimeRaw is DepartureMinutes + DepartureHours + Duration,
- write(ArrivalTimeRaw),
- % Hours, then minutes
- ArrivalTime is floor(ArrivalTimeRaw / 60) * 100 + (ArrivalTimeRaw mod 60).
- %%%%%%%%% 5
- % Incomplete (1/2)
- tripDays([TripH | TripT], Time, FlightTimes, Days) :-
- tripDays_aux(TripH, TripT, Time, [], FlightTimes, 0, Days).
- % Base case, trip is over (Reached only 1 element in list, it is the final destination
- tripDays_aux(_, [_X], _, FlightTimes, FlightTimes, Days, Days).
- tripDays_aux(From, [To | TripT], CurrDepatureTime, CurrTimes, FlightTimes, CurrDays, Days) :-
- % We want a flight that goes from and to where we want it and that departs later than the time we are ready at
- flight(DesiredFlight, From, To, DepartureTime, _, _),
- DepartureTime >= CurrDepatureTime, !,
- arrivalTime(DesiredFlight, ArrivalTimeRaw),
- ArrivalTimeWithExtra is ArrivalTimeRaw + 30,
- append(CurrTimes, [DepartureTime], CurrTimes2),
- tripDays_aux(To, TripT, ArrivalTimeWithExtra, CurrTimes2, FlightTimes, CurrDays, Days).
- % 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
- tripDays_aux(From, TripList, _, CurrTimes, FlightTimes, CurrDays, Days) :-
- CurrDays2 is CurrDays + 1,
- tripDays_aux(From, TripList, 0, CurrTimes, FlightTimes, CurrDays2, Days).
- %%%%%%%%% 6
- %incomplete (1.9/2)
- pairableFlights :-
- pairableFlights_aux([]).
- get_in_minutes(Time, Mins) :-
- Mins is floor(Time / 100) * 60 + (Time mod 100).
- pairableFlights_aux(CurrPaired) :-
- % Flight1 with destination Airport that has an arrival time that is less than Flight2's departure time, with a margin of 30-90 minutes
- % Flight2 must also have origin Airport
- flight(Flight1, _, Airport, _, _, _),
- arrivalTime(Flight1, ArrivalTime1),
- flight(Flight2, Airport, _, Departure2, _, _),
- get_in_minutes(ArrivalTime1, AT1),
- get_in_minutes(Departure2, D2),
- D2 - AT1 >= 30,
- D2 - AT1 =< 90,
- \+ member(Airport-Flight1-Flight2, CurrPaired), !,
- write(Airport), write(' - '), write(Flight1), write(' \\ '), write(Flight2), nl,
- append(CurrPaired, [Airport-Flight1-Flight2], CurrPaired2),
- pairableFlights_aux(CurrPaired2).
- pairableFlights_aux([]).
- %%%%%%%%% 7
- :- use_module(library(lists)).
- avgFlightLengthFromAirport(Airport, AvgLength) :-
- findall(Duration, flight(_, Airport, _, _, Duration, _), FlightsDuration),
- sumlist(FlightsDuration, Sum),
- length(FlightsDuration, Count),
- Count =\= 0,
- AvgLength is Sum/Count.
- %%%%%%%%% 8 (MUITO ESPARGUETE NÃO RECOMENDO, USAR PREDICADOS DE ANTES)
- mostInternational(ListOfCompanies) :-
- findall(Company, company(Company, _, _, _), CompaniesList),
- findall(Company-NrCountries, (member(Company, CompaniesList), company_nr_countries(Company, NrCountries)), Companies),
- get_bests(Companies, 0, [], ListOfCompanies).
- % base case
- get_bests([], _, ListOfCompanies, ListOfCompanies).
- % new best
- get_bests([Company-NrCountries | T], CurrBest, _CurrListOfCompanies, ListOfCompanies) :-
- NrCountries > CurrBest, !,
- get_bests(T, NrCountries, [Company], ListOfCompanies).
- % equal to best
- get_bests([Company-NrCountries | T], CurrBest, CurrListOfCompanies, ListOfCompanies) :-
- NrCountries = CurrBest, !,
- append(CurrListOfCompanies, [Company], CurrListOfCompanies2),
- get_bests(T, CurrBest, CurrListOfCompanies2, ListOfCompanies).
- % less than best
- get_bests([_ | T], CurrBest, CurrListOfCompanies, ListOfCompanies) :-
- get_bests(T, CurrBest, CurrListOfCompanies, ListOfCompanies).
- company_nr_countries(Company, NrCountries) :-
- setof(Origin, OriginAirport^(flight(_, OriginAirport, _, _, _, Company), airport(_, OriginAirport, Origin)), Origins),
- findall(Destination, DestinationAirport^(flight(_, _, DestinationAirport, _, _, Company), airport(_, DestinationAirport, Destination)), Destinations),
- setof(Country, (member(Country, Origins) ; member(Country, Destinations)), Countries),
- length(Countries, NrCountries), !.
- %%%%%%%%% 9
- :- use_module(library(lists)).
- make_pairs(L, P, [X-Y|Zs]) :-
- select(X, L, L2),
- select(Y, L2, L3),
- G =.. [P, X, Y], G,
- make_pairs(L3, P, Zs), !.
- make_pairs([], _, []).
- %%%%%%%%% 10
- :- use_module(library(lists)).
- make_pairs(L, P, [X-Y|Zs]) :-
- select(X, L, L2),
- select(Y, L2, L3),
- G =.. [P, X, Y], G,
- make_pairs(L3, P, Zs), !.
- make_pairs([_|T], P, Zs) :-
- make_pairs(T, P, Zs).
- make_pairs([], _, []).
- %%%%%%%%%% 11
- make_max_pairs(L, P, S) :-
- % Obtaining all possible pairs
- findall(Pairs, make_pairs(L, P, Pairs), PairsList),
- % Comparing their size and picking the longest
- % Starting the comparison with size -1 so that the element is swapped straight away
- get_largest(PairsList, -1, [], S).
- get_largest([], _, S, S).
- get_largest([Pairs | T], CurrBestVal, _CurrBestPairs, S) :-
- length(Pairs, Len),
- Len > CurrBestVal, !,
- get_largest(T, Len, Pairs, S).
- get_largest([_ | T], CurrBestVal, CurrBestPairs, S) :-
- get_largest(T, CurrBestVal, CurrBestPairs, S).
- %%%%%%%%% 12
- (rip)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement