Ladder Bump Race
MediumOnline Assessment
Replay a dice race on a board with snakes, ladders and bumping, and report where everyone ends up.
The board has squares 0 to N. Every player starts on square 0 (off the board, shared by everyone) and the first to reach square N wins. jumps is a list of [start, end] pairs: a player who finishes a move on start is moved to end (a ladder if end > start, a snake otherwise). A jump is taken once; if end is itself the start of another jump, nothing more happens. end may be 0 or N.
Players 0 .. players-1 take turns in order, and the dice results arrive as the list rolls, each roll used by whoever is playing. A turn works like this:
- The player moves forward by the roll. If that would pass
N, the player bounces back: the new square isN - (position + roll - N). - If the new square is the start of a jump, the player takes it.
- If the player is now on
N, they win and the game stops; any remaining rolls are ignored. - Otherwise, if the player is on a square from
1toN - 1where another player stands, that player is bumped back to square0. (Square0never bumps anyone.)
Sixes: a roll of 6 earns the same player another turn immediately after the move. But if a player would take a third turn in a row because of two consecutive sixes, and the roll is again a 6, that roll is used up without moving the player, and the turn passes to the next player. (A third roll that is not a 6 is played normally.)
If the rolls run out before anyone wins, the game simply ends where it stands.
Input: n, the finishing square N; players, the number of players; jumps, a list of [start, end] pairs with distinct starts; rolls, a list of integers from 1 to 6.
Output: a list of players + 1 integers: each player's final square, in player order, followed by the winner's index, or -1 if nobody won.
Example 1
- Input:
- [20,2,[[3,12],[14,5]],[3,4,2,1]]
- Output:
- [0,5,-1]
- Explanation
- On a 20-square board with a ladder 3 to 12 and a snake 14 to 5, player 0 rolls 3 and climbs to 12. Player 1 rolls 4 and lands on 4. Player 0 rolls 2 and reaches 14, a snake head, and slides to 5. Player 1 rolls 1 and lands on 5, where player 0 stands, so player 0 is bumped back to 0. Nobody has won: the final squares are 0 and 5, winner -1.
Example 2
- Input:
- [20,2,[],[6,6,6,5,4,3,6,2]]
- Output:
- [20,8,0]
- Explanation
- Player 0 rolls 6 (to 6) and gets an extra turn, rolls 6 again (to 12) and gets another; the third roll is a 6 as well, so it is used up without moving and the turn passes. Player 1 rolls 5, player 0 rolls 4 (to 16), player 1 rolls 3 (to 8). Player 0 rolls 6, which would reach 22 on a 20-square board, so it bounces back to 18; being a six it earns another roll, and a 2 lands exactly on 20 and wins. Player 1 stays on 8 and player 2 never moved.
Example 3
- Input:
- [30,3,[[6,9],[9,3],[12,3]],[3,6,3,4]]
- Output:
- [0,3,4,-1]
- Explanation
- Player 0 rolls 3 and stands on 3. Player 1 rolls 6, lands on 6 and takes the ladder to 9; it is not followed again even though 9 is a snake head. The six gives player 1 another roll: a 3 reaches 12, whose snake leads to 3, where player 0 stands, so player 0 is bumped to 0. Player 2 rolls 4 and lands on 4. Final squares 0, 3, 4; no winner.
Constraints
- 2 ≤ n ≤ 100
- 2 ≤ players ≤ 4
- 0 ≤ jumps.length ≤ 15; starts are distinct and between 1 and n - 1; ends are between 0 and n and differ from the start
- 0 ≤ rolls.length ≤ 200; every roll is between 1 and 6