In: Computer Science
QUESTION:
write a program for the function : def collision(program1, start1, program2, start2):
- takes program1 and start1 of a vehicle and program2 and start2 of another vehicle and checks whether the two vehicles will collide
- if a collision occurs then a tuple must be returned containing (x,y) coordinates of the cell where the collision has occurred
- if the two vehicles collide in multiple cells the coordinates of the first cell must be returned where the collision has occurred
- if the vehicles never collide then None must be returned
FOR THIS QUESTION IS CAN BE ASSUMED:
- both vehicles start executing their programs at the same time
- both vehicles perform actions at the same rate
- all actions take the same amount of time
- once a vehicle completes its program it remains in the same location
- a collision occurs when two vehicles are in the same cell at the same time, irrespective of their directions
EXAMPLE CALLS:
- collision([‘Drive', ‘TurnR', ‘Drive’], (0, 0, ‘E’), [‘Drive', ‘Drive’], (1, 1, ‘S’)) returns (1, 0)
- collision([‘Drive', ‘Drive', ‘Drive’], (2, 2, ‘N’), [‘Drive', ‘Drive', ‘Drive’], (2, 1, ‘N’)) returns None
- collision([‘Drive', ‘Drive', ‘Drive’], (2, 2, ‘N’), [‘Drive', ‘Drive', ‘Drive’], (2, 2, ‘N’)) returns (2, 2)
- collision([‘Drive', ‘TurnR', ‘Drive’], (2, 2, ‘N’), [‘Drive', ‘Drive', ‘Drive’], (2, 1, ‘N’)) returns (2, 3)
Please find the solution below:
SOLUTION:
#move the vehicle by one space in the current direction
def move(position):
    if(position[2] == 'E'):
        position[0] = position[0] + 1 
    elif(position[2] == 'N'):
        position[1] = position[1] + 1
    elif(position[2] == 'S'):
        position[1] = position[1] - 1
    elif(position[2] == 'W'):
        position[0] = position[0] - 1
        
#changing the direction of the vehicle
def changeDirection(position, action):
    if(action == "TurnR" and position[2] == 'E'):
        position[2] = 'S'
    elif(action == "TurnR" and position[2] == 'S'):
        position[2] = 'W'
    elif(action == "TurnR" and position[2] == 'W'):
        position[2] = 'N'
    elif(action == "TurnR" and position[2] == 'N'):
        position[2] = 'E'
    elif(action == "TurnL" and position[2] == 'E'):
        position[2] = 'N'
    elif(action == "TurnL" and position[2] == 'N'):
        position[2] = 'W'
    elif(action == "TurnL" and position[2] == 'W'):
        position[2] = 'S'
    elif(action == "TurnL" and position[2] == 'S'):
        position[2] = 'E'
#detect collision based on programs of the vehicles
def collision(program1, start1, program2, start2):
    position1 = list(start1)
    position2 = list(start2)
    
    # checking collision for initial position of vehicles
    if(position1[0]==position2[0] and position1[1]==position2[1]):
            return ((position1[0],position2[0]))
    
    #tracking simultaneous movement of vehicles
    for x,y in zip(program1,program2):
        if(x=="Drive"):
            move(position1)
        elif(x=="TurnR" or x=="TurnL"):
            changeDirection(position1,x)
        if(y=="Drive"):
            move(position2)
        elif(y=="TurnR" or y=="TurnL"):
            changeDirection(position2,y)
        #checking if vehicles collide after finishing one action
        if(position1[0]==position2[0] and position1[1]==position2[1]):
            return (position1[0],position1[1])
    
    #vehicles did not collide throughout programs
    return "None"
Thanks