Skip to content

Instantly share code, notes, and snippets.

@abernier
Last active September 9, 2019 11:46
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 abernier/00ac26ff978aebdfa6f3f65019fe2bf0 to your computer and use it in GitHub Desktop.
Save abernier/00ac26ff978aebdfa6f3f65019fe2bf0 to your computer and use it in GitHub Desktop.
ironpairs
  1. Open a terminal and paste the following command ☝️😲WITHOUT EXECUTING IT:

    pbpaste | npx https://gist.github.com/abernier/00ac26ff978aebdfa6f3f65019fe2bf0 10 | pbcopy

    NB: 10 being the number of combinations wanted
  2. In a spreadsheet, copy rows of names:
  3. In the previous terminal, press now ENTER to execute the pasted command at step 0.:
  4. Paste the result into spreadsheet
#!/usr/bin/env node
// Run
// ---
// $ npm install roundrobin
// $ pbpaste | node pairs.js 25 | pbcopy
const roundrobin = require('roundrobin')
const numberOfRounds = Number(process.argv[2])
let names = process.argv.slice(3)
if (names.length > 0) {
assignPairs(names)
} else {
let nameString = ''
process.stdin.setEncoding('utf8')
process.stdin.on('data', chunk => nameString += chunk)
process.stdin.on('end', () => {
names = nameString.split('\n')
assignPairs(names)
})
}
function assignPairs (namesArray) {
// Add an extra name for odd numbered groups.
// Otherwise roundrobin leaves out the extra name in each round.
if (namesArray.length % 2 === 1) {
namesArray.push('Imaginary Pair')
}
let pairs = []
// Create several sets of pairs to fill all pairing days needed.
// We will get some extras but they can be useful.
while (pairs.length < numberOfRounds) {
const newPairs = roundrobin(namesArray.length, namesArray)
pairs = pairs.concat(newPairs)
}
// If odd numbered group, assign trios in each round.
if (namesArray.indexOf('Imaginary Pair') > -1) {
const trioCount = {}
const triodPairs = []
namesArray.forEach(name => trioCount[name] = 0)
pairs.forEach(round => {
let extraName
// Get rid of the 'Imaginary Pair' pairing
const roundWithTrio = round
.filter(pair => {
const extraIndex = pair.indexOf('Imaginary Pair')
// Keep extra name that was paired with 'Imaginary Pair' this round
if (extraIndex > -1) {
const nameIndex = (extraIndex + 1) % 2
extraName = pair[nameIndex]
}
return extraIndex === -1
})
.sort((pairA, pairB) => {
const trioCountA = trioCount[pairA[0]] + trioCount[pairA[1]]
const trioCountB = trioCount[pairB[0]] + trioCount[pairB[1]]
return trioCountA - trioCountB
})
let currentTrio = roundWithTrio[0]
currentTrio.push(extraName)
currentTrio.forEach(name => trioCount[name] += 1)
// Save new round
triodPairs.push(roundWithTrio)
})
pairs = triodPairs
}
// Display results for pasting onto spreadsheet.
let row = ''
pairs.forEach(function (round) {
row += '"'
round.forEach(function (pairing) {
row += `${pairing[0]} with ${pairing[1]}`
if (pairing.length > 2) {
row += ` and ${pairing[2]}`
}
row += '\n'
})
row += '"\t'
})
console.log(row)
}
{"name": "ironpairs", "version": "0.1.0", "bin": "./index.js", "dependencies": {"roundrobin": "1.0.3"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment