Sunday, October 8, 2017

Family Dinner in Farfaraway

Farfaraway

HP CodeWars 2016 Barcelona

Problem 28: Family Dinner in Farfaraway

In [1]:
# Hardcoded test data
earth = (0,0,0)
farfaraway = (2000,0,0)
planetList = [(0,900,0),(1600,0,0),(800,0,0)]
In [2]:
def get_dist(P,Q):
    a,b,c = P
    x,y,z = Q
    dist = (((a-x)**2+(b-y)**2+(c-z)**2)**(0.5))
    #print("\t\ t",P,Q,dist)
    return dist

def bfs_queue(queue):
    explored = []

    while queue:
        # pop first planet from queue
        planet = queue.pop(0)

        if planet not in explored:
            print("\t exploring ",planet)
            if get_dist(planet,farfaraway)<=1000.0:
                return True
            else:
                explored.append(planet)
                for neighbor in planetList:
                    if neighbor not in explored and get_dist(planet,neighbor)<=1000.0:
                        print("\t\t",neighbor, " is reachable from ", planet)
                        queue.append(neighbor)
 
    return False
In [3]:
queue = [earth]

if bfs_queue(queue):
    print("yes")
else:
    print("no")
  exploring  (0, 0, 0)
   (0, 900, 0)  is reachable from  (0, 0, 0)
   (800, 0, 0)  is reachable from  (0, 0, 0)
  exploring  (0, 900, 0)
  exploring  (800, 0, 0)
   (1600, 0, 0)  is reachable from  (800, 0, 0)
  exploring  (1600, 0, 0)
yes