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()
|
||||
|
||||
def move_to(x, y):
|
||||
while x > get_pos_x():
|
||||
move(East)
|
||||
while x < get_pos_x():
|
||||
move(West)
|
||||
cx = get_pos_x()
|
||||
cy = get_pos_y()
|
||||
|
||||
while y > get_pos_y():
|
||||
move(North)
|
||||
while y < get_pos_y():
|
||||
move(South)
|
||||
east_steps = (x - cx) % world_size
|
||||
west_steps = (cx - x) % world_size
|
||||
if east_steps <= west_steps:
|
||||
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