I decided to create a tree to represent each Step, adding dependencies and dependents for each steap, and a sorting algorithm to sort steps about to processed (i.e. when it has no dependents).
Then, I use a BFS traversal, employing a queue to visit the steps. In the 1st part, if a step has been processed but still has dependents, I re-add it to the end queue. For the 2nd part, I don't need this because then it won't be picked up by another worker, it remains with the same worker for the rest of the duration of that step.
const{readFile}=require('./reader');const{Step,getStep,buildSteps,getFirstSteps,sortSimilarSteps}=require('./07-common');constgetStepsOrder=steps=>{letstepsOrder='';while(steps.length>0){conststep=steps.shift();stepsOrder+=step.letter;for(letdependentofstep.dependents){constindex=dependent.dependencies.indexOf(step);dependent.dependencies.splice(index,1);if(dependent.dependencies.length===0){steps.push(dependent);}}sortSimilarSteps(steps);}returnstepsOrder;};(async()=>{constlines=awaitreadFile('07-input.txt');conststeps=buildSteps(lines);constfirstSteps=getFirstSteps(steps);conststepsOrder=getStepsOrder(firstSteps);console.log(`The steps order is ${stepsOrder}`);})();
07b.js
const{readFile}=require('./reader');const{Step,getStep,buildSteps,getFirstSteps,sortSimilarSteps}=require('./07-common');constWARM_UP=60;constWORKERS=15;constgetStepDuration=step=>WARM_UP+step.letter.charCodeAt(0)-64;constgetStepsTime=steps=>{constworkers=Array.from({length:WORKERS},w=>newObject({step:null}));letisThereWorkLeft=steps.length>0;letstepsOrder='';lettotalSeconds=0;while(isThereWorkLeft){for(letworkerofworkers){letstep=worker.step;// If worker is done, go over dependents and get yourself a new stepif(step&&step.elapsedSeconds===getStepDuration(step)){stepsOrder+=step.letter;for(letdependentofstep.dependents){constindex=dependent.dependencies.indexOf(step);dependent.dependencies.splice(index,1);if(dependent.dependencies.length===0){steps.push(dependent);}}sortSimilarSteps(steps);worker.step=null;step=null;}// Assigning new stepif(!step){step=steps.shift();if(!step){continue;}worker.step=step;step.worker=worker;step.elapsedSeconds=0;}if(step.elapsedSeconds<getStepDuration(step)){step.elapsedSeconds++;}}isThereWorkLeft=!!workers.find(w=>!!w.step);if(isThereWorkLeft){totalSeconds++;}}return{stepsOrder,totalSeconds};};(async()=>{constlines=awaitreadFile('07-input.txt');conststeps=buildSteps(lines);constfirstSteps=getFirstSteps(steps);const{totalSeconds}=getStepsTime(firstSteps);console.log(`The steps time is ${totalSeconds}`);})();
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Javascript solution
I decided to create a tree to represent each Step, adding dependencies and dependents for each steap, and a sorting algorithm to sort steps about to processed (i.e. when it has no dependents).
Then, I use a BFS traversal, employing a queue to visit the steps. In the 1st part, if a step has been processed but still has dependents, I re-add it to the end queue. For the 2nd part, I don't need this because then it won't be picked up by another worker, it remains with the same worker for the rest of the duration of that step.
reader.js
07-common.js
07a.js
07b.js