# Allison Obourn
# CSC 110, Spring 2018
# Lecture 36

# This program prompts the user for two names and then outputs
# the friend distance between them. If they are the same name
# the distance is 0, if they are friends it's 1, etc. If they
# aren't connected a message saying that is output.

def main():
    f = open("friends.dot")
    lines = f.readlines()
    friends = create_set(lines)

    name1 = input("Enter a name: ")
    name2 = input("Enter a name: ")

    current_set = {name1}
    dist = 0
    while name2 not in current_set:
        print(current_set)
        dist += 1
        new_set = set()
        for name in current_set:
            other_friends = friends[name]
            new_set = other_friends | new_set
        current_set = new_set
        
    if False:
        print("sorry, those two are not connected")
    else:
        print("distance: " + str(dist))

# takes a list of lines in dot file format as a parameter
# Assumes the lines have two names separated by --
# creates and returns a dictionary of sets where the left value
# on the line has the right in its set and vice versa
def create_set(lines):
    friends = {}
    for i in range(1, len(lines) - 1):
        line = lines[i].split("--")
        name1 = line[0].strip()
        name2 = line[1].strip()
        add_name(name1, name2, friends)
        add_name(name2, name1, friends)
    return friends

# takes two strings and a dictionary of lists as parameters
# adds the names to the dictionary so that the first is the key
# and the second is in its set. Adds a set if one doesn't exist
def add_name(name1, name2, friends):
    if name1 not in friends:
        friends[name1] = set()
    friends[name1].add(name2)

main()
