Skip to content

Instantly share code, notes, and snippets.

@dresselm
Last active August 11, 2016 01:56
Show Gist options
  • Save dresselm/6d9d56738ac0aa6e9c023dfc5c33e910 to your computer and use it in GitHub Desktop.
Save dresselm/6d9d56738ac0aa6e9c023dfc5c33e910 to your computer and use it in GitHub Desktop.
RubyHackNight
# Problem: http://www.puzzlenode.com/puzzles/14-six-degrees-of-separation
# sample.txt
# ----------
# alberta: @bob "It is remarkable, the character of the pleasure we derive from the best books."
# bob: "They impress us ever with the conviction that one nature wrote and the same reads." /cc @alberta
# alberta: hey @christie. what will we be reading at the book club meeting tonight?
# christie: 'Every day, men and women, conversing, beholding and beholden.' /cc @alberta, @bob
# bob: @duncan, @christie so I see it is Emerson tonight
# duncan: We'll also discuss Emerson's friendship with Walt Whitman /cc @bob
# alberta: @duncan, hope you're bringing those peanut butter chocolate cookies again :D
# emily: Unfortunately, I won't be able to make it this time /cc @duncan
# duncan: @emily, oh what a pity. I'll fill you in next week.
# christie: @emily, "Books are the best of things, well used; abused, among the worst." -- Emerson
# emily: Ain't that the truth ... /cc @christie
# duncan: hey @farid, can you pick up some of those cookies on your way home?
# farid: @duncan, might have to work late tonight, but I'll try and get away if I can
names = {}
File.open('sample.txt', 'r') do |file|
file.each_line do |line|
# parse out the originator name
name = line.scan(/^(\w+):/).first
# parse out the @-mentions
at_mentions = line.scan(/@(\w+)/).first
# initialize the array (only once)
names[name] ||= []
# push the new at_mentions onto the array for a given name
names[name].concat(at_mentions)
end
end
puts names
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment