Live data from Hacker News

Elevator Saga – An elevator programming game

play.elevatorsaga.com

61–70 of 108 posts

Re: Elevator Saga – An elevator programming game

#62
post #42

I wonder how much more efficient you could be, if people didn't just call for going up/down, but their destination floor.

They have that at work and it takes far longer to get an elevator. It's always annoying going to those buildings and using those elevators.

I like it much more. Yes, getting an elevator might take longer, but total travel time should be reduced. (Otherwise, the system is programmed wrong.)

Re: Elevator Saga – An elevator programming game

#63
post #53
post #41

My attempt at a basic smart algorithm. Not so easy... https://gist.github.com/kballenegger/e275a99d50de2ee07f97

here mine: https://gist.github.com/eridal/7ce55190837801811067 side note: why are you using underscore? I wanted to try your code, but doesn't work out-of-the-box.

LoDash actually, not Underscore. Mostly because it's included by the game already. The code should run as-is. That said, it does occasionally get stuck. I think I have a bug with the direction indicators.

Re: Elevator Saga – An elevator programming game

#64
post #54

Earlier quoted context omitted.

Why do you have conditionals written like this: if (elevator.goingUpIndicator() == true)? Why not just: if (elevator.goingUpIndicator())?

I believe some people really need think in term of comparations and, for those, adding a "==" symbol make sense, even if the resultant code doesn't make any sense at all!! Also, I can never understand why people write .. if (foo()) { return true; } else { return false; } .. or things like extra parens on conditionals, or weird styles like: return (false); I strongly believe people write as they talk, and talk as they…

Responding to GP; I think it's a tiny bit clearer with `== true`, but I don't feel particularly strongly about it. I wrote this code without caring too much about style and cleanliness.

However, I don't agree that the shortest code is always the cleanest. In JS for example, I've found that wrapping every conditional body in braces even if it's a single statement helps avoid bugs and clear out ambiguity.

Also, this code:

    if (foo()) {
        return true;
    } else {
        return false;
    }
is not equivalent to

    return foo();
because foo() could return something that isn't boolean. The correct short version would be:

    return !!foo();
It's far less obvious what is going on here. That said, I'd probably go with:

    return foo() ? true : false;

Re: Elevator Saga – An elevator programming game

#66
post #65

Is it just me, or is programming more than one elevator buggy? I used a for( var i =0;i < elevators.length;++i) statement to apply my code to each elevator, but people only keep using the last one. Could someone give me a hint? ;)

You're probably hitting an issue with the way that closures work in javascript (and many other languages).

    for(var i = 0; i 
Doesn't do what one might expect. When the anonymous function is invoked, it looks up the value of the 'i' identifier, which will have changed it's value to elevators.length by the end of the loop. To get the behavior you want, I've seen people do

    for(var i = 0; i 
This creates a new scope, which ensures that 'i' has the value that was passed in. I'm afraid I'm a little too tired to look up the parts of the spec that make the semantics clear.

Re: Elevator Saga – An elevator programming game

#67

Here's a fairly simple strategy for beating challenges 1 - 5. Basically drop off people in the elevator according to whoever's floor is closest. Then once the elevator is empty go to the closest floor that has people waiting to be picked up. https://gist.github.com/cspags/93f18a2fe505bf4f1686

[deleted]

Re: Elevator Saga – An elevator programming game

#68
post #65

Is it just me, or is programming more than one elevator buggy? I used a for( var i =0;i < elevators.length;++i) statement to apply my code to each elevator, but people only keep using the last one. Could someone give me a hint? ;)

You're probably hitting an issue with the way that closures work in javascript (and many other languages). for(var i = 0; i Doesn't do what one might expect. When the anonymous function is invoked, it looks up the value of the 'i' identifier, which will have changed it's value to elevators.length by the end of the loop. To get the behavior you want, I've seen people do for(var i = 0; i This creates a new scope, which…

Thank you! Your suggestion makes sense indeed and works fine!

Re: Elevator Saga – An elevator programming game

#70
post #42

I wonder how much more efficient you could be, if people didn't just call for going up/down, but their destination floor.

They have that at work and it takes far longer to get an elevator. It's always annoying going to those buildings and using those elevators.

In more than a few buildings I've worked in, these 'destination controlled' elevators shine the moment after a fire-drill and a couple of hundred people attempt to get back upstairs. It must be quicker, how else to justify what I'd imagine to be a far higher cost of operation.
Post reply on HN