Skip to content

Instantly share code, notes, and snippets.

@gaswelder
Last active January 15, 2020 20:16
Show Gist options
  • Save gaswelder/ba29ce46e43163bb11977dde082cb286 to your computer and use it in GitHub Desktop.
Save gaswelder/ba29ce46e43163bb11977dde082cb286 to your computer and use it in GitHub Desktop.
// https://github.com/yankouskia/love-triangle
module.exports = function getLoveTrianglesCount(preferences = []) {
return (
preferences.map((x, i) => getTriangle(preferences, i)).filter(x => x)
.length / 3
);
};
const getTriangle = (preferences, index) => {
if (preferences[index] - 1 == index) {
return null; // narcissus
}
const b = preferences[index] - 1;
const c = preferences[b] - 1;
const d = preferences[c] - 1;
return d == index ? [b, c, d] : null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment