Reduce steps in this simple maze puzzle?

Question:

Puzzle

Video Puzzle

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.

Asked By: RFL Studio

||

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.

Answered By: Raida

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)
Answered By: guest
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.