Advertisement
bebo231312312321

Untitled

Apr 3rd, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     input.pop();
  3.     // parse each command
  4.     let animalObj = {};
  5.     let areaObj = {};
  6.     for (let el of input) {
  7.         let line = el.split(': ');
  8.         let command = line[0];
  9.         let animalInfo = line[1];
  10.         let animalLine = animalInfo.split('-');
  11.         let [name, food, area] = animalLine;
  12.         if (command === 'Add') {
  13.             if (!animalObj.hasOwnProperty(name)) {
  14.                 animalObj[name] = {
  15.                     food: Number(food),
  16.                     area: area
  17.                 }
  18.                 if (!areaObj.hasOwnProperty(area)) {
  19.                     areaObj[area] = [name];
  20.                 } else {
  21.                     areaObj[area].push(name);
  22.                 }
  23.             } else {
  24.                 animalObj[name].food += Number(food);
  25.             }
  26.         } else if (command === 'Feed') {
  27.             if (animalObj.hasOwnProperty(name)) {
  28.                 animalObj[name].food -= Number(food);
  29.                 if (animalObj[name].food <= 0) {
  30.                     console.log(`${name} was successfully fed`);
  31.                     delete animalObj[name];
  32.                     let tuples = Object.entries(areaObj);
  33.                     for (let el of tuples) {
  34.                         let areaName = tuples[0];
  35.                         let namesArray = tuples[1];
  36.                         let index = namesArray.indexOf(name);
  37.                         namesArray.splice(index, 1);
  38.                     }
  39.                     console.log(tuples);
  40.                 }
  41.             }
  42.         }
  43.     }
  44.     //console.log(animalObj);
  45.     // console.log(areaObj);
  46. }
  47.  
  48. solve(["Add: Bonie-3490-RiverArea",
  49. "Add: Sam-5430-DeepWoodsArea",
  50. "Add: Bonie-200-RiverArea",
  51. "Add: Maya-4560-ByTheCreek",
  52. "Add: hehe-4560-ByTheCreek",
  53. "Feed: Maya-2390",
  54. "Feed: hehe-4570",
  55. "Feed: Bonie-3500",
  56. "Feed: Johny-3400",
  57. "Feed: Sam-5500",
  58. "EndDay"])
  59.  
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement