Skip to content

Instantly share code, notes, and snippets.

@qwtel
Created December 10, 2019 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qwtel/e8f09a7a07d9ed135efd8b7ea68ddfe2 to your computer and use it in GitHub Desktop.
Save qwtel/e8f09a7a07d9ed135efd8b7ea68ddfe2 to your computer and use it in GitHub Desktop.
const nearestAsteroidsByAngle = pipe(
asteroids.filter(mkNe(laserPos)),
groupBy(mkCalcAngle(laserPos)),
mapValues(ps => ps.sort((p1, p2) => distToLaser(p1) - distToLaser(p2))),
intoMap(),
);
pipe(
cycle([...nearestAsteroidsByAngle.keys()].sort((a, b) => a - b)),
skipWhile(a => a < -Math.PI/2),
map(angle => nearestAsteroidsByAngle.get(angle)),
filter(nearest => nearest.length),
map(nearest => nearest.shift()),
take(200),
last(),
p => log(p[0] * 100 + p[1]),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment