Setup
Jump is a simple one-player game:
You are initially at the first cell of an array of cells containing non-negative integers;
At each step you can jump ahead in the array as far as the integer at the current cell, or any smaller number of cells. You win if there is a path that allows you to jump from one cell to another, eventually jumping past the end of the array, otherwise you lose.
Examples
For instance, if the array contains the integers
[2, 0, 3, 5, 0, 0, 3, 0, 0, 3, 1, 0],
you can win by jumping from 2, to 3, to 5, to 3, to 3, then past the end of the array.
You can also directly jump from from the initial cell(first cell) past the end of the array if they are integers to the right of that cell.
E.g
[6, 1, 1] is winnable
[6] is not winnable
Note: You can not jump from the last cell!
[1, 1, 3] is not winnable
Tests
canJump([5])
canJump([2, 5])
canJump([3, 0, 2, 3])
canJump([4, 1, 2, 0, 1])
canJump([5, 0, 0, 0])
canJump([1, 1])
Thanks for reading! Have fun.
This challenge comes from kodejuice on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (1)
Not sure I understand the rules - why is
[6]
not winnable? Because you can't jump from the last cell, or because the last jump must be exact? The note says "You can not jump from the last cell!" but it can be interpreted as either "the last cell can never be jumped from" or "the last cell in this case is 6, so you can't do a one cell jump over the edge".Is
[3, 0]
winnable? The last jump needs to be of 2, which is allowed from a 3 unless the rule is last-jump-needs-to-be-exact.Is
[1]
winnable? You need to do a 1-jump from the 1, which is OK unless the rule is can't-jump-from-last-cell.