Last active
April 23, 2017 09:48
-
-
Save feugy/c4d2bc09c065739a340237fa792323e8 to your computer and use it in GitHub Desktop.
Solution for coding game "Elevator" - https://www.codingame.com/ide/puzzle/elevator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const [floors, up, down, start, target] = readline().split(' ').map(n => +n) | |
| const move = (start, target, up, down, floors) => { | |
| if (target === start) { | |
| return 0 | |
| } | |
| let count = 0 | |
| let current = start | |
| const stack = [] | |
| while(current !== target) { | |
| if (stack.includes(current)) { | |
| return null | |
| } | |
| stack.push(current) | |
| const goDown = current - target > 0 | |
| if (goDown && current - down >= 0) { | |
| current -= down | |
| count++ | |
| } else if (current + up <= floors) { | |
| current += up | |
| count++ | |
| } | |
| } | |
| return count | |
| } | |
| const count = move(start, target, up, down, floors) | |
| print(count === null ? 'IMPOSSIBLE' : count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment