I see, the disabled play button in the beginning is not optimal. Replacing "parse" with "parse & run" is an interesting option. I'm not sure yet, how it would affect the other buttons, though. I somehow like the debugging-style "Step" feature. Would it be better to have the example program parsed already such that "Play" is enabled on page load?
Answering your question, maybe a popover with text "Your program is not parsed" would help?
Also somehow I am used to "build" (instead of "parse") terminology, so my eyes just have not caught "parse" keyword. Though, I guess here nothing is built, only parsed. ;)
All in all some information, about parsing before running would add extra points to the UX.
To watch them run around is so cool! Cool puzzle. Nicely created site. I am trying to give them a pendulum like swing instruction right now, make them dance :P
edit: had two unnecessary instruction in the gist (because each instruction takes same amount of time to execute), and, the robots disappointed me- they can't dance
Additional suggestion: ability to grade code. I see solutions that are a lot shorter than mine in code (2 labels, 5 instructions), but they take about 87 instruction cycles to get there whereas without labels you can do it in about 30 cycles.
Yeah, I was about to delete my comment (but now someone already replied) because I discovered the randomize button. My bad!
The solution with a "speeding label" is the only solution I see. I also recognize the parallel with infinite loop detection now, like sugarcube commented already: https://news.ycombinator.com/item?id=10477109
At first I thought there couldn't be a solution, since both robots always do the same thing. You can imagine the "duh!" in my head after seeing the comments here…
With your code they end up right where they started? I see 'random locations' in the text above but the location is always the same here. Adding a couple of 'left's works, though, and is the same as my solution.
Edit: randomize button. I see. It also makes this no solution because they could start anywhere.
start: left
skipNext
goto start
fast: left
goto fast
Red and Blue must take three cycles to move one space until Blue hits Red's parachute. Then, Red take three cycles to move one space while Blue takes two cycles to move one space where Blue then catches Red shortly after.
This problem was posed to me for real in a Microsoft interview in the late 90s. At first I was like "How hard could this be?" but quickly discovered that it wasn't so obvious. If I hadn't already seen the famous circular-list detection method, there's no way I would've gotten it, not during the interview at least.
You don't need the 'goto double' line, execution is anyway going skip to 'double: left' making the line redundant
Also say each step takes one unit of time, so one 'start' loop requires 3 units to go one unit of distance, whereas 'double' takes 3 units to go 2 units of distance, even if you use one left statement in the 'double' loop, robot will still move faster than the other as it will require only two units of time to go one unit of distance.
slow: left
skipNext
goto slow
fast: left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
left
goto fast
The best set for making it as fast as possible within the bounds of the javascript parser.
Took me ages to realise the twist though, because I was somehow set on making them oscillate further on every iteration and couldn't understand how I was to remember how far I went without a stack.
Basically what I wanted to do was: x left, if its not there, return. Next time x * 2 left, return to base if its not there, ...
I kept thinking,how would robots know when to switch directions? That is when I realized that robots do not have to crash head on but overtaking is sufficient.
This is one case where I am glad that I did not read HN comments before solving. My solution is the same as moftz and other left leaners. :) First iteration I made the speed be four times the slow robot but of course double speed is fine.
no, because they don't have any information about the other robot and they can not tell whether they are walking towards the other parachute or walking away from both parachutes because they are somewhere on an infinite line.
I don't think there is without a counter and an if statement. I had this question once and came up with the two robots oscillating around their parachute until they came across another parachute at which point it would stop and wait for the other to return.
Here is slight twist on the problem: the new robot has multiple cores with shared cache and very accurate branch prediction so branching instructions do not waste any cycles. So "left, skipNext, goto" loop performs exactly as efficiently as "left, left, goto" loop. Can you still solve this?
Yeah, I'm assuming the interviewers either tell you how many cycles each instruction takes, or expect you to ask them. I had to tinker a bit to figure out which instructions cost a cycle and which didn't. Would certainly be much harder under pressure and without a cycle accurate simulator.
i did this in about 30 seconds because this remedial challenge was no match for my highly superior brain. jk it took me and 2 other people about 30 minutes to complete this using efficient code. much fun 10/10 would recommend too a friend. if i had any
57 comments
[ 4.1 ms ] story [ 121 ms ] threadAlso great implementation and beautiful site.
Though, IMHO there could be a button named "parse & run", my eyes have missed a "parse" button, and I thought that something was broken.
I see, the disabled play button in the beginning is not optimal. Replacing "parse" with "parse & run" is an interesting option. I'm not sure yet, how it would affect the other buttons, though. I somehow like the debugging-style "Step" feature. Would it be better to have the example program parsed already such that "Play" is enabled on page load?
Also somehow I am used to "build" (instead of "parse") terminology, so my eyes just have not caught "parse" keyword. Though, I guess here nothing is built, only parsed. ;)
All in all some information, about parsing before running would add extra points to the UX.
Suggestion: Add ability to share the code.
(and even that could be done in two as well as I see now)
I guess my interview wouldn't go that well :)
[0] https://gist.github.com/kanche/92c92c5e926ebc66da68
To watch them run around is so cool! Cool puzzle. Nicely created site. I am trying to give them a pendulum like swing instruction right now, make them dance :P
edit: had two unnecessary instruction in the gist (because each instruction takes same amount of time to execute), and, the robots disappointed me- they can't dance
The solution with a "speeding label" is the only solution I see. I also recognize the parallel with infinite loop detection now, like sugarcube commented already: https://news.ycombinator.com/item?id=10477109
2 labels, and 5 statements
http://pastebin.com/ZcLdJhzN
Also "loop" is not a good label. They are both loops.
Much appreciated entertainment on a lazy friday at work :)
Spoiler!
Edit: randomize button. I see. It also makes this no solution because they could start anywhere.
That's as far as I got in 5 mins [but I'm not a coder].
Using inefficiency to solve problems, I'm not surprised.
One pointer moves twice as fast the as the other one.
Also say each step takes one unit of time, so one 'start' loop requires 3 units to go one unit of distance, whereas 'double' takes 3 units to go 2 units of distance, even if you use one left statement in the 'double' loop, robot will still move faster than the other as it will require only two units of time to go one unit of distance.
:)
The best set for making it as fast as possible within the bounds of the javascript parser.
Took me ages to realise the twist though, because I was somehow set on making them oscillate further on every iteration and couldn't understand how I was to remember how far I went without a stack.
Basically what I wanted to do was: x left, if its not there, return. Next time x * 2 left, return to base if its not there, ...
This is one case where I am glad that I did not read HN comments before solving. My solution is the same as moftz and other left leaners. :) First iteration I made the speed be four times the slow robot but of course double speed is fine.
start: left left right skipNext goto start continue: left goto continue
Then you should get something that is equivalent to boolfuck (1-bit brainfuck) which is Turing complete.
hell:left left goto hell
Clue - You will need to use both "left" and "right" movement.