Skip to content

Instantly share code, notes, and snippets.

@Scorpiion
Created December 9, 2013 22:37
Show Gist options
  • Save Scorpiion/7882261 to your computer and use it in GitHub Desktop.
Save Scorpiion/7882261 to your computer and use it in GitHub Desktop.
Dart Dare - Secret Santas
import 'dart:io';
void main() {
var input = '''
Zoe Washburne
Hoban Washburne
Malcolm Reynolds
Simon Tam
River Tam
Buffy Summers
Dawn Summers''';
Map<String, List<String>> people = new Map<String, List<String>>();
Map<String, List<String>> schizoPeople = new Map<String, List<String>>();
input.split("\n").forEach((s) {
var p = s.trim().split(" ");
if(people[p[1]] == null) {
people[p[1]] = new List<String>();
schizoPeople[p[1]] = new List<String>();
}
people[p[1]].add(p[0]);
schizoPeople[p[1]].add(p[0]);
});
people.forEach((k, v) => people = Match(people, false));
schizoPeople.forEach((k, v) => schizoPeople = Match(schizoPeople, true));
}
Map<String, List<String>> Match(Map<String, List<String>> people, bool newLine) {
for(var family in people.keys) {
if(people[family].isNotEmpty) {
stdout.write(people[family].removeLast() + " " + family);
if(newLine) {
print("");
newLine = false;
} else {
stdout.write(", ");
newLine = true;
}
}
}
return people;
}
@sethladd
Copy link

Thanks for sharing! BTW you can use print() instead of stdout.write. It's a bit shorter, and print works in the browser, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment