Last active
August 25, 2019 15:29
-
-
Save fitsallun/99a7f12e2aa8155c5bbf93b2c61c2a20 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const siblings = { | |
Anthony: ['Kristin'], | |
Tony: ['Sarah'], | |
Joshua: ['Leah'], | |
Kristin: ['Anthony'], | |
Jonathon: [], | |
Leah: ['Joshua'], | |
Donald: ['Rebekah'], | |
Sarah: ['Tony'], | |
Rebekah: ['Donald'], | |
Daniel: ['Rachel'], | |
Rachel: ['Daniel'], | |
Ruth: ['Benjamin'], | |
Benjamin: ['Ruth'], | |
Alyssa: ['Tyler'], | |
Tyler: ['Alyssa'], | |
Josiah: [], | |
Kayla: ['Noah'], | |
Noah: ['Kayla'] | |
} | |
const cousins = { | |
Bennett: ['Ella', 'Livi'], | |
Ella: ['Bennett', 'Livi'], | |
Livi: ['Bennett', 'Ella'], | |
Kayla: ['Emily', 'Mariah', 'Olivia', 'Anthony', 'Julie'], | |
Emily: ['Kayla', 'Mariah', 'Olivia', 'Anthony', 'Julie'], | |
Mariah: ['Kayla', 'Emily', 'Olivia', 'Anthony', 'Julie'], | |
Olivia: ['Kayla', 'Emily', 'Mariah', 'Anthony', 'Julie'], | |
Anthony: ['Kayla', 'Emily', 'Mariah', 'Olivia', 'Julie'], | |
Julie: ['Kayla', 'Emily', 'Mariah', 'Olivia', 'Anthony'], | |
Michael: ['Sophia', 'Jonathan'], | |
Sophia: ['Michael', 'Jonathan'], | |
Jonathan: ['Michael', 'Sophia'], | |
Riley: ['Jack', 'Grey', 'Beck'], | |
Jack: ['Riley', 'Grey', 'Beck'], | |
Grey: ['Riley', 'Jack', 'Beck'], | |
Beck: ['Riley', 'Jack', 'Grey'], | |
Madeline: [], | |
Evelyn: ['Jackie'], | |
Jackie: ['Evelyn'] | |
} | |
const getValidReceivers = (receivers, givers, giver) => { | |
receivers = [...receivers] | |
const exclusions = [...givers[giver], giver] | |
exclusions.forEach((exclusion, index) => { | |
const exclusionIndex = receivers.indexOf(exclusion) | |
const exclusionExistsInReceivers = exclusionIndex >= 0 | |
if (exclusionExistsInReceivers) { | |
receivers.splice(exclusionIndex, 1) | |
} | |
}) | |
return receivers | |
} | |
const assignGivers = (givers) => { | |
const giverList = Object.keys(givers) | |
const receivers = [...giverList] | |
let matches = [] | |
giverList.forEach((giver, index) => { | |
const validReceivers = getValidReceivers(receivers, givers, giver) | |
const receiver = validReceivers[Math.floor(Math.random() * validReceivers.length)]; | |
matches.push({ | |
'giver': giver, | |
'receiver': receiver | |
}) | |
// remove matched receiver from list | |
receivers.splice(receivers.indexOf(receiver), 1) | |
}) | |
let rerun = false; | |
matches.forEach(({ receiver }, index) => { | |
if (typeof receiver === 'undefined') { | |
rerun = true; | |
} | |
}) | |
if (rerun) { | |
// A little hackey, but it's possible to get down to the last giver without a valid | |
// receiver, so rerun the assignments until all assignments are successful | |
// TODO: figure out a better way to retrieve valid receivers that includes a check | |
// for future givers | |
assignGivers(givers); | |
} | |
else { | |
console.log(matches.map((match) => { | |
let encouragment = '' | |
if (match.receiver === 'Jonathon') { | |
encouragment = ' They’d better not mess it up.' | |
} | |
return match.giver + ' is giving a gift to ' + match.receiver + '.' + encouragment | |
}).join('\n')) | |
} | |
} | |
console.log('Siblings:') | |
assignGivers(siblings) | |
console.log('Cousins:') | |
assignGivers(cousins) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment