Advertisement
xapu

Untitled

Jun 6th, 2017
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. function Problem4() {
  2.  
  3. let activities = arguments[0]
  4. let planes = {}
  5. let towns = {}
  6.  
  7. for (let activiti of activities) {
  8. let currentAction = activiti.split(" ")
  9. let currentPlaneId = currentAction[0]
  10. let currentPlaneDestination = currentAction[1]
  11. let currentPlaneFillment = Number(currentAction[2]) // ppl traveling
  12. let currentPlaneActivity = currentAction[3] // landing or takeing off
  13.  
  14. let isPlaneAvaliable = planes.hasOwnProperty(currentPlaneId)
  15. // First time landing planes
  16. if ((planes == undefined || !isPlaneAvaliable) && currentPlaneActivity == 'land') {
  17. planeGenerator(currentPlaneId, planes)
  18. if (towns == undefined || !towns.hasOwnProperty(currentPlaneDestination)) {
  19. townsGenerator(currentPlaneDestination, 0, currentPlaneFillment, currentPlaneId, towns)
  20. } else {
  21. towns[currentPlaneDestination].arrivals += currentPlaneFillment
  22. towns[currentPlaneDestination].planes.add(currentPlaneId)
  23. }
  24. }
  25. // planes landing
  26. if (isPlaneAvaliable && currentPlaneActivity == "depart" && planes[currentPlaneId].status == "land") {
  27. if (towns.hasOwnProperty(currentPlaneDestination)) {
  28. towns[currentPlaneDestination].departures += currentPlaneFillment
  29. towns[currentPlaneDestination].planes.add(currentPlaneId)
  30. } else {
  31. townsGenerator(currentPlaneDestination, currentPlaneFillment, 0, currentPlaneId, towns)
  32. }
  33. planes[currentPlaneId].status = "depart"
  34. }
  35. // planes flying away
  36. if (isPlaneAvaliable && currentPlaneActivity == "land" && planes[currentPlaneId].status == "depart") {
  37. if (towns.hasOwnProperty(currentPlaneDestination)) {
  38. towns[currentPlaneDestination].arrivals += currentPlaneFillment
  39. towns[currentPlaneDestination].planes.add(currentPlaneId)
  40. } else {
  41. townsGenerator(currentPlaneDestination, 0, currentPlaneFillment, currentPlaneId, towns)
  42. }
  43. planes[currentPlaneId].status = "land"
  44. }
  45. }
  46.  
  47. //From here on im printing
  48. console.log("Planes left:")
  49.  
  50. Object.keys(planes).filter(x => planes[x].status != "depart").sort(function(a,b){
  51. return a.localeCompare(b)
  52. }).map(x => console.log("- " + x))
  53.  
  54. // in sorted towns there is a problem with the sort,
  55.  
  56. let sortedTowns = Object.keys(towns).sort((a, b) => towns[b].arrivals - towns[a].arrivals).sort(function(a,b){
  57. if((towns[b].arrivals === towns[a].arrivals)){
  58. return a.toLowerCase().localeCompare(b.toLocaleLowerCase())
  59. }else{
  60. return -1
  61. }
  62. })
  63.  
  64.  
  65. for (let town of sortedTowns) {
  66. console.log(town)
  67. console.log("Arrivals: " + towns[town].arrivals)
  68. console.log("Departures: " + towns[town].departures)
  69. console.log("Planes:")
  70.  
  71. let tempArr = Array.from(towns[town].planes)
  72.  
  73. tempArr.sort(function(a,b){
  74. return a.localeCompare(b)
  75. }).map(x=>console.log("-- "+x))
  76. }
  77.  
  78. function townsGenerator(id, leaving = 0, arriving = 0, planeId, townsObj) {
  79. townsObj[id] = {
  80. departures: leaving,
  81. arrivals: arriving,
  82. planes: new Set([])
  83. }
  84. townsObj[id].planes.add(planeId)
  85. }
  86.  
  87. function planeGenerator(planeID, planesObj) {
  88. planesObj[planeID] = {
  89. status: "land"
  90. }
  91. }
  92. }
  93.  
  94. Problem4(['RTA72 London 140 land',
  95. 'RTA72 Brussels 240 depart',
  96. 'RTA72 Sofia 450 land',
  97. 'RTA72 Lisbon 240 depart',
  98. 'RTA72 Berlin 350 land',
  99. 'RTA72 Otava 201 depart',
  100. 'RTA72 Haga 350 land',
  101. 'RTA72 Otava 201 depart',
  102. 'RTA72 Dortmund 150 land',
  103. 'RTA72 Montana 243 depart',
  104. 'RTA72 Monreal 350 land',
  105. 'RTA72 NewYork 201 depart',
  106. 'RTA72 Pekin 350 land',
  107. 'RTA72 Tokyo 201 depart',
  108. 'RTA72 Warshaw 350 land',
  109. 'RTA72 Riga 201 depart']
  110. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement