Skip to content

Instantly share code, notes, and snippets.

@ananace
Created April 10, 2014 21:39
Show Gist options
  • Save ananace/10425924 to your computer and use it in GitHub Desktop.
Save ananace/10425924 to your computer and use it in GitHub Desktop.
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]
end
# 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
end
indirect_friends[friend].uniq! # And remove duplicates
end
end
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"
else
puts "no access allowed"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment