Advertisement
go6odn28

1-task

Dec 15th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(initData) {
  2.     const farmersNum = Number(initData.shift());
  3.     const farmerDetails = initData.slice(0, farmersNum);
  4.     const commands = initData.slice(farmersNum, - 1);
  5.     const farmers = farmerDetails.reduce((acc, elem) => {
  6.         const [name, workArea, tasksList] = elem.split(' ');
  7.         acc[name] = [workArea, tasksList];
  8.         return acc;
  9.     }, {});
  10.  
  11.     for ( line of commands) {
  12.         const commList = line.split(' / ');
  13.         const command = commList[0];
  14.         const farmerName = commList[1];
  15.  
  16.         if (! farmerName in farmers) return
  17.  
  18.         const [ farmerArea, farmerTasks ] = farmers[farmerName];
  19.  
  20.         switch (command) {
  21.  
  22.             case("Execute"):
  23.             const currentArea = commList[2];
  24.             const currentTask = commList[3];
  25.  
  26.                 if ( ( currentArea === farmerArea ) && ( farmerTasks.includes(currentTask) )) {
  27.                     console.log(`${farmerName} has executed the task: ${currentTask}!`);
  28.                 } else console.log(`${farmerName} cannot execute the task: ${currentTask}.`);
  29.                 break;
  30.  
  31.             case('Change Area'):
  32.                 const newArea = commList[2];
  33.                 farmers[farmerName][0] = newArea;
  34.  
  35.                 console.log(`${farmerName} has changed their work area to: ${newArea}`);
  36.                 break;
  37.  
  38.             case('Learn Task'):
  39.                 const newTask = commList[2];
  40.  
  41.                 if (farmerTasks.includes(newTask)) {
  42.                     console.log(`${farmerName} already knows how to perform ${newTask}.`);
  43.                 } else {
  44.                     console.log(`${farmerName} has learned a new task: ${newTask}.`);
  45.                     farmers[farmerName][1] += `,${newTask}`;
  46.                 break;
  47.                 }
  48.         }
  49.  
  50.     }
  51.  
  52.     for (currFarmer in farmers) {
  53.  
  54.         const [area, tasks] = farmers[currFarmer];
  55.         const orderedTasks = tasks.split(',').sort();
  56.  
  57.         console.log(`Farmer: ${currFarmer}, Area: ${area}, Tasks: ${orderedTasks.join(', ')}`)
  58.     }
  59. }
  60.  
  61.  
  62. solution([
  63.         "3",
  64.         "Alex apiary harvesting,honeycomb",
  65.         "Emma barn milking,cleaning",
  66.         "Chris garden planting,weeding",
  67.         "Execute / Chris / garden / pruning",
  68.         "Execute / Chris / garden / planting,weeding",  // TODO тук реално нямаш задача "planting,weeding" обаче излиза че имаш по начина по който ги добавяш, все едно е една задача
  69.         "End"
  70.     ]
  71. )
  72.  
  73. //  OUTPUT
  74.  
  75. // Chris cannot execute the task: pruning.
  76. // Chris has executed the task: planting,weeding! TODO тук се принитра, че я изпълнява
  77. // Farmer: Alex, Area: apiary, Tasks: harvesting, honeycomb
  78. // Farmer: Emma, Area: barn, Tasks: cleaning, milking
  79. // Farmer: Chris, Area: garden, Tasks: planting, weeding
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement