Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function main(allFlights) {
- function TownInfo() {
- this.arrivals = 0;
- this.departures = 0;
- this.planes = new Set();
- }
- const ActionLand = 'land';
- const ActionDepart = 'depart';
- allFlights = allFlights.map(s => s.split(/\s+/g));
- const mapAirplanesByAction = new Map();
- mapAirplanesByAction.set(ActionLand, new Set());
- mapAirplanesByAction.set(ActionDepart, new Set());
- const mapTownInfosByTown = new Map();
- for (const [airplaneId, town, people, action] of allFlights) {
- const parsedPeople = Number(people);
- if (airplaneCanLand(mapAirplanesByAction, action, airplaneId)) {
- addAirplaneInAction(mapAirplanesByAction, ActionLand, airplaneId);
- removeAirplaneFromAction(mapAirplanesByAction, ActionDepart, airplaneId);
- updateTownInfo(mapTownInfosByTown, town, parsedPeople, airplaneId, 'arrivals');
- } else if (airplaneCanDepart(mapAirplanesByAction, action, airplaneId)) {
- addAirplaneInAction(mapAirplanesByAction, ActionDepart, airplaneId);
- removeAirplaneFromAction(mapAirplanesByAction, ActionLand, airplaneId);
- updateTownInfo(mapTownInfosByTown, town, parsedPeople, airplaneId, 'departures');
- }
- }
- printResult(mapAirplanesByAction.get(ActionLand), mapTownInfosByTown);
- function printResult(setLandedPlanes, mapTowns) {
- const arrayAirplanesId = getSortedArray(setLandedPlanes.keys(), sortAlphabetic);
- console.log('Planes left:');
- for (const airplaneId of arrayAirplanesId) {
- console.log(`- ${airplaneId}`);
- }
- const arrayTownsEntries = getSortedArray(mapTowns.entries(), sortEntriesTowns);
- for (const [town, townInfo] of arrayTownsEntries) {
- console.log(town);
- console.log(`Arrivals: ${townInfo.arrivals}`);
- console.log(`Departures: ${townInfo.departures}`);
- console.log('Planes:');
- const sortedPlanes = getSortedArray(townInfo.planes.keys(), sortAlphabetic);
- for (const planeId of sortedPlanes) {
- console.log(`-- ${planeId}`);
- }
- }
- }
- function getSortedArray(elements, sortingFunc) {
- return Array.from(elements).sort(sortingFunc);
- }
- function sortEntriesTowns(a, b) {
- const arrivalsA = a[1].arrivals;
- const arrivalsB = b[1].arrivals;
- if (arrivalsA > arrivalsB) {
- return -1;
- } else if (arrivalsA < arrivalsB) {
- return 1;
- }
- return sortAlphabetic(a[0], b[0]);
- }
- function sortAlphabetic(a, b) {
- a = a.toLowerCase();
- b = b.toLowerCase();
- if (a > b) {
- return 1;
- } else if (a < b) {
- return -1;
- }
- return 0;
- }
- function updateTownInfo(map, town, people, airplaneId, objectKey) {
- if (!map.has(town)) {
- map.set(town, new TownInfo());
- }
- const townInfo = map.get(town);
- townInfo[objectKey] += people;
- townInfo.planes.add(airplaneId);
- }
- function removeAirplaneFromAction(map, action, airplaneId) {
- const airplanesForAction = map.get(action);
- airplanesForAction.delete(airplaneId);
- }
- function airplaneCanLand(map, action, airplaneId) {
- return action === ActionLand &&
- !airplaneIsInStatus(map, ActionLand, airplaneId);
- }
- function airplaneCanDepart(map, action, airplaneId) {
- return action === ActionDepart &&
- !airplaneIsInStatus(map, ActionDepart, airplaneId) &&
- airplaneIsInStatus(map, ActionLand, airplaneId);
- }
- function addAirplaneInAction(map, action, airplaneId) {
- const set = map.get(action);
- set.add(airplaneId);
- }
- function airplaneIsInStatus(map, action, airplaneId) {
- const isInStatus = map.get(action).has(airplaneId);
- return isInStatus;
- }
- }
- main([
- 'RTA72 London 140 land',
- 'RTA72 Brussels 240 depart',
- 'RTA72 Sofia 450 land',
- 'RTA72 Lisbon 240 depart',
- 'RTA72 Berlin 350 land',
- 'RTA72 Otava 201 depart',
- 'RTA72 Haga 350 land',
- 'RTA72 Otava 201 depart',
- 'RTA72 Dortmund 150 land',
- 'RTA72 Montana 243 depart',
- 'RTA72 Monreal 350 land',
- 'RTA72 NewYork 201 depart',
- 'RTA72 Pekin 350 land',
- 'RTA72 Tokyo 201 depart',
- 'RTA72 Warshaw 350 land',
- 'RTA72 Riga 201 depart',
- ]
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement