In the first part I tried an arithmetic approach, trying to find - for each asteroid - if there was another one in line with the target location. JavaScript as usual:
The second part showed that it would have been too complex. So I just converted all the coordinates in polar, sorted them out and counted to the 200th asteroid. Finally, converted back to orthogonal coordinates:
// `asteroids` defined as above...conststationRow=29;conststationColumn=23;constpolarCoords=newMap();asteroids.forEach(([column,row])=>{if(column===stationColumn&&row===stationRow){return;}constdiffColumn=stationColumn-column;constdiffRow=stationRow-row;consttheta=(Math.atan2(diffColumn,-diffRow)+Math.PI)%(Math.PI*2);constrho=Math.sqrt(diffColumn**2+diffRow**2);if(polarCoords.has(theta)){polarCoords.get(theta).add(rho);}else{polarCoords.set(theta,newSet([rho]));}});function*getTargets(){constsortedAngles=newSet([...polarCoords.keys()].sort((a,b)=>a-b));while(sortedAngles.size){for(constthetaofsortedAngles){constrhos=polarCoords.get(theta);if(rhos.size===1){sortedAngles.delete(theta);yield[theta,...rhos];}else{constnearest=Math.min(...rhos);rhos.delete(nearest);yield[theta,nearest];}}}}functiontoOrtho(theta,rho){return[stationColumn+Math.round(Math.sin(theta)*rho),stationRow-Math.round(Math.cos(theta)*rho)]}consttargets=getTargets();letvalue;for(leti=0;i<200;i++){({value}=targets.next());}const[resColumn,resRow]=toOrtho(...value);console.log(resColumn*100+resRow);
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.
Things are getting complicated now! I like it!
In the first part I tried an arithmetic approach, trying to find - for each asteroid - if there was another one in line with the target location. JavaScript as usual:
The second part showed that it would have been too complex. So I just converted all the coordinates in polar, sorted them out and counted to the 200th asteroid. Finally, converted back to orthogonal coordinates: