Created
December 9, 2013 22:37
-
-
Save Scorpiion/7882261 to your computer and use it in GitHub Desktop.
Dart Dare - Secret Santas
This file contains 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: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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing! BTW you can use
print()
instead ofstdout.write
. It's a bit shorter, and print works in the browser, too.