Skip to content

Instantly share code, notes, and snippets.

@ceciliaelena
Created February 1, 2021 14:11
Show Gist options
  • Save ceciliaelena/798c10e54c7a0bb7fed4a84e145f078e to your computer and use it in GitHub Desktop.
Save ceciliaelena/798c10e54c7a0bb7fed4a84e145f078e to your computer and use it in GitHub Desktop.
blablacar_test
5 5
1 2 N
LFLFLFLFF
3 3 E
FFRFFRFRRF
lines = []
with open('blablacar.txt') as file:
lines = [l.rstrip('\n') for l in file]
def change_direction(mwr,i):
if mwr[2] == 'N':
mwr[2] = 'E' if (i == 'R') else 'W'
elif mwr[2] == 'E':
mwr[2] = 'S' if (i == 'R') else 'N'
elif mwr[2] == 'W':
mwr[2] = 'N' if (i == 'R') else 'S'
elif mwr[2] == 'S':
mwr[2] = 'W' if (i == 'R') else 'E'
def move(mwr, size):
z = mwr[2]
if z == 'N':
if mwr[1] < size[1]:
mwr[1] += 1
elif z == 'E':
if mwr[0] < size[0]:
mwr[0] += 1
elif z == 'W':
if mwr[0] > 0:
mwr[0] -= 1
elif z == 'S':
if mwr[1] > 0:
mwr[1] -= 1
def update_coordinates(mwrs, num, grid_size):
# TODO copy mwrs here and compare after for validation
for m in mwrs:
if len(m[1]) > num:
action = m[1][num]
if action == 'R' or action == 'L':
change_direction(m[0],action)
elif action == 'F':
move(m[0], grid_size)
x, y = lines[0].split(' ')
grid_size = (int(x), int(y))
coordinates = []
for l in lines[1::2]:
e1, e2, e3 = l.split(' ')
coordinates.append([int(e1), int(e2), e3])
actions = lines[2::2]
mowers = list(zip(coordinates,actions))
max_num_action = len(max(actions,key=len))
step = 0
while step <= max_num_action:
update_coordinates(mowers, step, grid_size)
step += 1
print(coordinates)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment