Created
December 8, 2013 12:18
-
-
Save terrasea/7856640 to your computer and use it in GitHub Desktop.
Dart Dare - Secret Santas
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
import 'dart:html'; | |
class Person { | |
String firstname; | |
String lastname; | |
Person(this.firstname, this.lastname); | |
@override | |
String toString() { | |
return firstname + " " + lastname; | |
} | |
@override | |
bool equals(Person rhs) { | |
return firstname == rhs.firstname && lastname == rhs.lastname; | |
} | |
} | |
class Pair { | |
Person first; | |
Person second; | |
Pair(this.first, this.second); | |
} | |
void main() { | |
querySelector("#uploadFile") | |
.onChange.listen(upload); | |
querySelector("#processButton") | |
.onClick.listen(createPairs); | |
} | |
void upload(Event e) { | |
FileUploadInputElement input = e.currentTarget; | |
FileList files = input.files; | |
if(files.length > 0) { | |
File file = files.item(0); | |
FileReader reader = new FileReader(); | |
reader.addEventListener('load', (e) { | |
TextAreaElement el = querySelector("#inputText"); | |
el.value = reader.result.toString(); | |
}); | |
reader.readAsText(file, "utf-8"); | |
} | |
} | |
void createPairs(Event e) { | |
List<String> people = new List<String>(); | |
TextAreaElement el = querySelector("#inputText"); | |
el.value.split("\n").forEach((line) { | |
people.add(line); | |
}); | |
DivElement div = querySelector("#pairs"); | |
StringBuffer pairsHTML = new StringBuffer(); | |
matchSecretSantas(people).forEach((pair) { | |
pairsHTML.write("<div><span>${pair.first}</span> - <span>${pair.second}</span></div>"); | |
}); | |
div.innerHtml = pairsHTML.toString(); | |
} | |
//people items are assumed to be "firstname lastname", anything else is ignored | |
List<Pair> matchSecretSantas(List<String> people) { | |
List<Person> potentials = new List<Person>(); | |
people.forEach((line) { | |
List<String> nameList = line.split(" "); | |
if(nameList.length == 2) { | |
potentials.add(new Person(nameList[0], nameList[1])); | |
} | |
}); | |
potentials.shuffle(); | |
return _makePair(potentials.first, potentials); | |
} | |
//RECURSIVE function | |
List<Pair> _makePair(Person first, List<Person> potentials, [List<Pair> pairs]) { | |
if(pairs == null) { | |
pairs = new List<Pair>(); | |
} | |
potentials.remove(first); | |
Person second = potentials.firstWhere((person) => _valid(first, person)); | |
potentials.remove(second); | |
pairs.add(new Pair(first, second)); | |
if(potentials.length == 0) { | |
return pairs; | |
} else if(potentials.length == 1) { | |
Person first = potentials.first; | |
Pair seconds = pairs.firstWhere((pair) => _valid(pair.first, first) || _valid(pair.second, first)); | |
Person second = _valid(seconds.first, first) ? seconds.first : seconds.second; | |
potentials.add(second); | |
} | |
return _makePair(potentials.first, potentials, pairs); | |
} | |
bool _valid(Person first, Person second) { | |
return _notSamePerson(first, second) && _notShareLastname(first, second); | |
} | |
bool _notSamePerson(Person first, Person second) { | |
return !first.equals(second); | |
} | |
bool _notShareLastname(Person first, Person second) { | |
return first.lastname != second.lastname; | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Secret Santa</title> | |
</head> | |
<body> | |
<h1>Secret Santa</h1> | |
<div> | |
<input type="file" id="uploadFile"> | |
</div> | |
<div> | |
<textarea id="inputText" rows="10" cols="50"></textarea> | |
</div> | |
<div> | |
<button id="processButton">Pair Secret Santas</button> | |
</div> | |
<div id="pairs"> | |
</div> | |
<script type="application/dart" src="secret_santa.dart"></script> | |
<script src="packages/browser/dart.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment