7 ms·
Can this program be made any shorter? http://www.dodaj.rs/f/1K/vW/2gLz63rl/blockly.png http://www.dodaj.rs/f/1K/vW/2gLz63rl/blockly.png
by grakic 14y ago
Can this program be made any shorter? http://www.dodaj.rs/f/1K/vW/2gLz63rl/blockly.png http://www.dodaj.rs/f/1K/vW/2gLz63rl/blockly.png
- JoshTriplett 14y agoAn "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)
- bockris 14y agoYou can add an 'else' clause to the 'if' block by clicking the '+' which launches a sub-editor.
- emp_ 14y agohttp://i.imgur.com/ct17R.png http://i.imgur.com/ct17R.png it takes a while, but gets there.
- poppysan 14y agohttp://i.imgur.com/8D2G9.jpg http://i.imgur.com/8D2G9.jpg
- jules 14y agohttp://i.imgur.com/HTvOx.png http://i.imgur.com/HTvOx.png Edit: I didn't know that an else was possible, so I had to do it this way.
- cnlwsu 14y agotakes awhile... hard to tell whats "shorter" while !wall(AHEAD): move forward turn left while wall(AHEAD): turn right
- saurik 14y agoIf 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.
- radarsat1 14y agoWeird, 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.
- eigenbom 14y agoIt visually represents the grammar of the language with the jigsaw patterns of the keywords, and prohibits connecting bad sequences, etc.
- ZeroGravitas 14y agoLooping left, forward, right, forward works for this particular maze, though it can get caught in potential corners types.
- WiseWeasel 14y agoHere'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 http://i.imgur.com/gNaRA.png
- WiseWeasel 14y agoAnd a much shorter and dumber version that does the same thing: http://i.imgur.com/TgS8I.png http://i.imgur.com/TgS8I.png
- true_religion 14y agoNot 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.1024x0.JPG http://cdn-images.9cloud.us/716/best-solution-kay_1568014138...
- xarien 14y agohttp://imgur.com/Nvy8u 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