Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Created May 8, 2020 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McLarenCollege/8d8d9a84e76f1c40291134e0d9238625 to your computer and use it in GitHub Desktop.
Save McLarenCollege/8d8d9a84e76f1c40291134e0d9238625 to your computer and use it in GitHub Desktop.

The Dice Game

Allowed Time: 1 hour

Four friends are playing a simple dice game (players are denoted p1, p2, p3, and p4). In each round, all players roll a pair of six-sided dice. The player with the lowest total score is removed. If the lowest score is shared by two or more players, the player in that group with the lowest score from their first die is removed. If the lowest score is still shared (i.e. two or more players have the same rolls in the same order), then all players roll again. This process continues until one player remains. Given an array of scores only (given in player order for each round), return the winning player.

Examples

diceGame([[6, 2], [4, 3], [3, 4], [5, 4], [3, 5], [1, 5], [4, 3], [1, 5], [1, 5], [5, 6], [2, 2]]) ➞ "p1"

// p1 p2 p3 p4 // Round 1 -> [6, 2], [4, 3], [3, 4], [5, 4] Player 3 removed. // Round 2 -> [3, 5], [1, 5], [4, 3] Player 2 removed. // Round 3 -> [1, 5], [1, 5] No lowest score, players roll again. // Round 4 -> [5, 6], [2, 2] Player 1 wins!

diceGame([[6, 2], [4, 3], [3, 4], [5, 4], [1, 5], [1, 5], [4, 3],[3, 6], [1, 2], [3, 6], [1, 5], [1, 5], [1, 6], [4, 5]]) ➞ "p4"

// p1 p2 p3 p4 // Round 1 -> [6, 2], [4, 3], [3, 4], [5, 4] Player 3 removed. // Round 2 -> [1, 5], [1, 5], [4, 3] Lowest score tie, players roll again. // Round 2 -> [3, 6], [1, 2], [3, 6] Player 2 removed. // Round 3 -> [1, 5], [1, 5] No lowest score, players roll again. // Round 4 -> [1, 6], [4, 5] Player 4 wins!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment