Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solution(initData) {
- const farmersNum = Number(initData.shift());
- const farmerDetails = initData.slice(0, farmersNum);
- const commands = initData.slice(farmersNum, - 1);
- const farmers = farmerDetails.reduce((acc, elem) => {
- const [name, workArea, tasksList] = elem.split(' ');
- acc[name] = [workArea, tasksList];
- return acc;
- }, {});
- for ( line of commands) {
- const commList = line.split(' / ');
- const command = commList[0];
- const farmerName = commList[1];
- if (! farmerName in farmers) return
- const [ farmerArea, farmerTasks ] = farmers[farmerName];
- switch (command) {
- case("Execute"):
- const currentArea = commList[2];
- const currentTask = commList[3];
- if ( ( currentArea === farmerArea ) && ( farmerTasks.includes(currentTask) )) {
- console.log(`${farmerName} has executed the task: ${currentTask}!`);
- } else console.log(`${farmerName} cannot execute the task: ${currentTask}.`);
- break;
- case('Change Area'):
- const newArea = commList[2];
- farmers[farmerName][0] = newArea;
- console.log(`${farmerName} has changed their work area to: ${newArea}`);
- break;
- case('Learn Task'):
- const newTask = commList[2];
- if (farmerTasks.includes(newTask)) {
- console.log(`${farmerName} already knows how to perform ${newTask}.`);
- } else {
- console.log(`${farmerName} has learned a new task: ${newTask}.`);
- farmers[farmerName][1] += `,${newTask}`;
- break;
- }
- }
- }
- for (currFarmer in farmers) {
- const [area, tasks] = farmers[currFarmer];
- const orderedTasks = tasks.split(',').sort();
- console.log(`Farmer: ${currFarmer}, Area: ${area}, Tasks: ${orderedTasks.join(', ')}`)
- }
- }
- solution([
- "3",
- "Alex apiary harvesting,honeycomb",
- "Emma barn milking,cleaning",
- "Chris garden planting,weeding",
- "Execute / Chris / garden / pruning",
- "Execute / Chris / garden / planting,weeding", // TODO тук реално нямаш задача "planting,weeding" обаче излиза че имаш по начина по който ги добавяш, все едно е една задача
- "End"
- ]
- )
- // OUTPUT
- // Chris cannot execute the task: pruning.
- // Chris has executed the task: planting,weeding! TODO тук се принитра, че я изпълнява
- // Farmer: Alex, Area: apiary, Tasks: harvesting, honeycomb
- // Farmer: Emma, Area: barn, Tasks: cleaning, milking
- // Farmer: Chris, Area: garden, Tasks: planting, weeding
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement