Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
gets.to_i # I don't care about the number :D
gets # Friendships:
direct_friends = {}
indirect_friends = {}
until /Queries/ =~ (data = gets) do # Until it reads the line Queries:
names = data.split # Split on space
# Create entries in the friend dictionaries if they don't exist
direct_friends[names[0]] = [] unless direct_friends.has_key? names[0]
direct_friends[names[1]] = [] unless direct_friends.has_key? names[1]
indirect_friends[names[0]] = [] unless indirect_friends.has_key? names[0]
indirect_friends[names[1]] = [] unless indirect_friends.has_key? names[1]
# Store the direct friend relations
direct_friends[names[0]] << names[1]
direct_friends[names[1]] << names[0]
# For each person
direct_friends.each do |name, friends|
friends.each do |friend| # Find all their friends
friends.each do |friend2| # And add them to eachothers indirect friends list
indirect_friends[friend] << friend2 unless friend == friend2
indirect_friends[friend].uniq! # And remove duplicates
while (data = gets) do # Read queries and execute them
names = data.split
if direct_friends.has_key? names[0] and direct_friends[names[0]].include? names[1] then
puts "direct access"
elsif indirect_friends.has_key? names[0] and indirect_friends[names[0]].include? names[1] then
puts "indirect access"
puts "no access allowed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment