Last active
January 15, 2020 20:16
-
-
Save gaswelder/ba29ce46e43163bb11977dde082cb286 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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