Optimize move_to logic in utils.py to minimize movement steps using modular arithmetic.
This commit is contained in:
27
utils.py
27
utils.py
@@ -3,15 +3,26 @@ from __builtins__ import *
|
|||||||
world_size = get_world_size()
|
world_size = get_world_size()
|
||||||
|
|
||||||
def move_to(x, y):
|
def move_to(x, y):
|
||||||
while x > get_pos_x():
|
cx = get_pos_x()
|
||||||
move(East)
|
cy = get_pos_y()
|
||||||
while x < get_pos_x():
|
|
||||||
move(West)
|
|
||||||
|
|
||||||
while y > get_pos_y():
|
east_steps = (x - cx) % world_size
|
||||||
move(North)
|
west_steps = (cx - x) % world_size
|
||||||
while y < get_pos_y():
|
if east_steps <= west_steps:
|
||||||
move(South)
|
for _ in range(east_steps):
|
||||||
|
move(East)
|
||||||
|
else:
|
||||||
|
for _ in range(west_steps):
|
||||||
|
move(West)
|
||||||
|
|
||||||
|
north_steps = (y - cy) % world_size
|
||||||
|
south_steps = (cy - y) % world_size
|
||||||
|
if north_steps <= south_steps:
|
||||||
|
for _ in range(north_steps):
|
||||||
|
move(North)
|
||||||
|
else:
|
||||||
|
for _ in range(south_steps):
|
||||||
|
move(South)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user