Skip to content

Instantly share code, notes, and snippets.

@andrewyatz
Created November 25, 2010 20:09
Show Gist options
  • Save andrewyatz/715849 to your computer and use it in GitHub Desktop.
Save andrewyatz/715849 to your computer and use it in GitHub Desktop.
Reallllyyy dumb way of doing secret santa
public enum P {
PERSON('[email protected]', true),
OTHER('[email protected]', true),
EH('[email protected]', true),
private final String email
private final Boolean satan
private P(String email, Boolean satan) {
this.email = email
this.satan = satan
}
public String getName() {
def val = toString();
return val.substring(0,1).toUpperCase() + val.substring(1).toLowerCase();
}
}
//Put pairs
def disallowed = [
[P.PERSON, P.OTHER] as Set
] as Set
def amount = '£15'
def results
//Meh just loop until it works. Cannot be bothered to solve so we just loop until it works
for(;;) {
def people = Arrays.asList(P.values());
def hat = Arrays.asList(P.values());
Collections.shuffle(people);
Collections.shuffle(hat);
hat = new ArrayList(hat)
results = []
people.each{ person ->
def iter = hat.iterator()
while( iter.hasNext()) {
def picked = iter.next();
def relation = [person, picked] as Set
if(person != picked && ! disallowed.contains(relation)) {
results.add([person, picked])
iter.remove()
break
}
}
}
if(results.size() == P.values().size()) {
break
}
}
results.each { it ->
def type = (it[0].satan) ? 'Satan' : 'Santa'
def name = it[0].getName()
def email = it[0].email
def rec = it[1].getName()
def body = """Congratulations *${name}* on joining in this year's Secret ${type}!
You will be buying a present for *${rec}*. You can spend a maximum of ${amount}.
Good luck!
"""
println it[0]
def f = new File('/tmp/email-'+it[0])
def script = """mail -s 'Secret ${type}' '${email}' <<EOF
${body}
EOF"""
f.write(script)
"bash ${f}".execute()
f.delete()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment