Skip to content

Instantly share code, notes, and snippets.

@feugy
Last active April 23, 2017 09:48
Show Gist options
  • Select an option

  • Save feugy/c4d2bc09c065739a340237fa792323e8 to your computer and use it in GitHub Desktop.

Select an option

Save feugy/c4d2bc09c065739a340237fa792323e8 to your computer and use it in GitHub Desktop.
Solution for coding game "Elevator" - https://www.codingame.com/ide/puzzle/elevator
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