Reduce steps in this simple maze puzzle?
Question:
Current code:
for i in range(6,0,-2):
Spaceship.step(2)
Dev.step(i)
for idk in range(3):
Dev.turnRight()
Dev.step(i*2)
Dev.turnRight()
Dev.step(i)
In this puzzle the objective is to get all the item (blue thing). With 6 line of code, and I’m currently at 8 line of code. I don’t know how to minimalize the line of code.
Note:
Dev.step() is the robot, it can go backward by set the value by negative.
Spaceship.step() is the spaceship, it can not go backward.
Answers:
This is a possible solution in only 6 lines:
for i in range(6, 0, -2):
Spaceship.step(2)
for k, j in enumerate([1, 2, 2, 2, 1]):
Dev.step(i * j)
if k != 4:
Dev.turnRight()
The idea is to group all steps of the robot in a list in order to do a nested loop and turn only if it is not the last element of the list.
You can avoid pythonesque code like so:
for i in range(6,0,-2):
Spaceship.step(2)
for idk in range(4):
Dev.step(i)
Dev.turnRight()
Dev.step(i)
Current code:
for i in range(6,0,-2):
Spaceship.step(2)
Dev.step(i)
for idk in range(3):
Dev.turnRight()
Dev.step(i*2)
Dev.turnRight()
Dev.step(i)
In this puzzle the objective is to get all the item (blue thing). With 6 line of code, and I’m currently at 8 line of code. I don’t know how to minimalize the line of code.
Note:
Dev.step() is the robot, it can go backward by set the value by negative.
Spaceship.step() is the spaceship, it can not go backward.
This is a possible solution in only 6 lines:
for i in range(6, 0, -2):
Spaceship.step(2)
for k, j in enumerate([1, 2, 2, 2, 1]):
Dev.step(i * j)
if k != 4:
Dev.turnRight()
The idea is to group all steps of the robot in a list in order to do a nested loop and turn only if it is not the last element of the list.
You can avoid pythonesque code like so:
for i in range(6,0,-2):
Spaceship.step(2)
for idk in range(4):
Dev.step(i)
Dev.turnRight()
Dev.step(i)