In: Computer Science
Write a function collision(p1, start1, p2, start2) that takes p1 and start1 of one vehicle, and p2 and start2 of another vehicle, and checks whether the two vehicles will collide (i.e., at some stage they will be in the same cell at the same time). The function should return a tuple containing the (x,y) coordinates of the cell where they collide or None if they do not ever collide. If the two vehicles collide in multiple cells, return the coordinates of the first cell where they collide.
The parameters to this function are as follows:
Assumptions:
Preferably do not use the zip function/ import itertools :)
Example calls to function:
>>> collision(['Drive', 'TurnR', 'Drive'], (0, 0, 'E'), ['Drive', 'Drive'], (1, 1, 'S'))
(1, 0)
>>> collision(['Drive', 'Drive', 'Drive'], (2, 2, 'N'), ['Drive', 'Drive', 'Drive'], (2, 1, 'N'))
None
>>> collision(['Drive', 'Drive', 'Drive'], (2, 2, 'N'), ['Drive', 'Drive', 'Drive'], (2, 2, 'N'))
(2, 2)
>>> collision(['Drive', 'TurnR', 'Drive'], (2, 2, 'N'), ['Drive', 'Drive', 'Drive'], (2, 1, 'N'))
(2, 3)
def collision(program1,start1,program2,start2):
direction1,direction2 = start1[2],start2[2]# Variables to store
current direction of vehicle
i,j=0,0
pos1=(start1[0],start1[1]) #Position of vehicle 1
pos2=(start2[0],start2[1]) #Position of vehicle 2
while(i<len(program1) or j<len(program2)):
if pos1==pos2:
#if both positions are same then return position
return pos1
if i<len(program1):
#Execute current program of vehicle 1
if program1[i]=='Drive':
#if program is drive then move forward in current direction
if direction1=='N':
pos1=(pos1[0],pos1[1]+1)
elif direction1=='S':
pos1=(pos1[0],pos1[1]-1)
elif direction1=='E':
pos1=(pos1[0]+1,pos1[1])
elif direction1=='W':
pos1=(pos1[0]-1,pos1[1])
elif program1[i]=='TurnR':
#if program is TurnR then change direction to right
if direction1=='N':
direction1='E'
elif direction1=='E':
direction1='S'
elif direction1=='S':
direction1='W'
elif direction1=='W':
direction1='N'
elif program1[i]=='TurnL':
#if program is TurnL then change direction to left
if direction1=='N':
direction1='W'
elif direction1=='W':
direction1='S'
elif direction1=='S':
direction1='E'
elif direction1=='E':
direction1='N'
i+=1
if(j<len(program2)):
#Execute current program of vehicle 2
if program2[j]=='Drive':
#if program is drive then move forward in current direction
if direction2=='N':
pos2=(pos2[0],pos2[1]+1)
elif direction2=='S':
pos2=(pos2[0],pos2[1]-1)
elif direction2=='E':
pos2=(pos2[0]+1,pos2[1])
elif direction2=='W':
pos2=(pos2[0]-1,pos2[1])
elif program2[j]=='TurnR':
#if program is TurnR then change direction to right
if direction2=='N':
direction2='E'
elif direction2=='E':
direction2='S'
elif direction2=='S':
direction2='W'
elif direction2=='W':
direction2='N'
elif program2[j]=='TurnL':
#if program is TurnL then change direction to left
if direction2=='N':
direction2='W'
elif direction2=='W':
direction2='S'
elif direction2=='S':
direction2='E'
elif direction2=='E':
direction2='N'
j+=1
return None
print(collision(['Drive','TurnR','Drive'],(0,0,'E'),['Drive','Drive'],(1,1,'S')))
print(collision(['Drive','Drive','Drive'],(2,2,'N'),['Drive','Drive','Drive'],(2,1,'N')))
print(collision(['Drive','Drive','Drive'],(2,2,'N'),['Drive','Drive','Drive'],(2,2,'N')))
print(collision(['Drive','TurnR','Drive'],(2,2,'N'),['Drive','Drive','Drive'],(2,1,'N')))