Advertisement
Guest User

Untitled

a guest
Jul 29th, 2019
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. function solve(input) {
  2.  
  3. let animals = {};
  4.  
  5. for (let line of input) {
  6.  
  7. if (line === 'Last Info') {
  8. break;
  9. }
  10.  
  11. let [command, name, limit, area] = line.split(':');
  12.  
  13. if (command === 'Add') {
  14. if (!animals.hasOwnProperty(name)) {
  15. animals[name] = [];
  16. animals[name].push(area);
  17. animals[name].push(0);
  18. }
  19. let currentLimit = Number(animals[name][1]);
  20. animals[name][1] = Number(limit) + currentLimit;
  21. } else {
  22. if (animals.hasOwnProperty(name)) {
  23. animals[name][1] -= limit;
  24. if (animals[name][1] <= 0) {
  25. console.log(`${name} was successfully fed`);
  26. delete animals[name];
  27. }
  28. }
  29. }
  30. }
  31.  
  32. let sortedAnimals = Object.entries(animals).sort((a, b) => {
  33. return b[1][1] - a[1][1] || a[0].localeCompare(b[0]);
  34.  
  35. });
  36.  
  37. let areas = [];
  38.  
  39. console.log('Animals:');
  40. if (sortedAnimals.length > 0) {
  41.  
  42. sortedAnimals.forEach((line) => {
  43. console.log(`${line[0]} -> ${line[1][1]}g`);
  44. areas.push(line[1][0]);
  45. });
  46. }
  47.  
  48. let areasObj = {};
  49.  
  50. areas.forEach((area) => {
  51. if (!areasObj.hasOwnProperty(area)) {
  52. areasObj[area] = 1
  53. } else {
  54. areasObj[area] += 1;
  55. }
  56. });
  57.  
  58. let sortedAreas = Object.entries(areasObj).sort((a, b) => {
  59. return b[1] - a[1] || b[0].localeCompare(a[0]);
  60. });
  61.  
  62. console.log('Areas with hungry animals:');
  63. if (sortedAreas.length > 0) {
  64.  
  65. sortedAreas.forEach((area) => {
  66. console.log(`${area[0]} : ${area[1]}`)
  67. })
  68. }
  69.  
  70.  
  71. }
  72.  
  73.  
  74. // solve(['Add:Maya:7600:WaterfallArea',
  75. // 'Add:Bobbie:6570:DeepWoodsArea',
  76. // 'Add:Adam:4500:ByTheCreek',
  77. // 'Add:Jamie:1290:RiverArea',
  78. // 'Add:Gem:8730:WaterfallArea',
  79. // 'Add:Maya:1230:WaterfallArea',
  80. // 'Add:Jamie:560:RiverArea',
  81. // 'Feed:Bobbie:6300:DeepWoodsArea',
  82. // 'Feed:Adam:4650:ByTheCreek',
  83. // 'Feed:Jamie:2000:RiverArea',
  84. // 'Last Info']);
  85.  
  86.  
  87. solve(['Add:Bonie:3490:RiverArea',
  88. 'Add:Sam:5430:DeepWoodsArea',
  89. 'Add:Bonie:200:RiverArea',
  90. 'Add:Maya:4560:ByTheCreek',
  91. 'Feed:Maya:2390:ByTheCreek',
  92. 'Feed:Bonie:3500:RiverArea',
  93. 'Feed:Johny:3400:WaterFall',
  94. 'Feed:Sam:5500:DeepWoodsArea',
  95. 'Last Info']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement