Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can this program be made any shorter? http://www.dodaj.rs/f/1K/vW/2gLz63rl/blockly.png


http://imgur.com/Nvy8u

Here's one with same amount of code and optimal logic (for this map) without hard coding. It also ensures that there is a forward step in every iteration of the while loop.

Basically it's just an order of preference:

Turn right > turn left > go straight


An "else" would help; you can add one by hitting the + symbol on the if. With the else, you can get the shortest program that'll solve the maze (transcribing using approximate notation in lieu of a screenshot):

    while True:
        if wall(AHEAD):
            turn(LEFT)
        else:
            move(FORWARD)
            turn(RIGHT)
However, the addition of one more if makes the solution much faster and look more sensible, by not turning to the right and immediately back to the left:

    while True:
        if wall(AHEAD):
            turn(LEFT)
        else:
            move(FORWARD)
            if not wall(RIGHT):
                turn(RIGHT)


You can add an 'else' clause to the 'if' block by clicking the '+' which launches a sub-editor.


Here's one that runs the fastest through this particular maze (I noticed there's always a wall on the right side when we need to make a left turn):

http://i.imgur.com/gNaRA.png


And a much shorter and dumber version that does the same thing:

http://i.imgur.com/TgS8I.png


Weird, that looks like you're manipulating blocks of text with the mouse. How is that a "visual programming language"? It just seems to be a fancy text editor.


It visually represents the grammar of the language with the jigsaw patterns of the keywords, and prohibits connecting bad sequences, etc.


http://i.imgur.com/HTvOx.png

Edit: I didn't know that an else was possible, so I had to do it this way.


Not the fastest but its just about as short as yours (counting all blocks as one): http://cdn-images.9cloud.us/716/best-solution-kay_1568014138...


I would blur out those browser favorites, if I were you.


indeed



Looping left, forward, right, forward works for this particular maze, though it can get caught in potential corners types.


http://i.imgur.com/ct17R.png it takes a while, but gets there.


takes awhile... hard to tell whats "shorter"

  while !wall(AHEAD):
    move forward
    turn left
    while wall(AHEAD):
      turn right


If you started off facing a wall your code would just not do anything. You can both simplify it and fix this bug by reorganizing the loop.

    while true:
        turn left
        while wall(AHEAD):
            turn right
        move forward
However, while this algorithm happens to work on this maze, it will not work against other mazes (if you care).

Imagine a 5x5 square race track, with spokes coming from the outside of the wheel to the center, where there was a flag. Your algorithm (and my modified one) would just spin clockwise around the track without ever realizing it should take a right towards the center.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: