Skip to content

Instantly share code, notes, and snippets.

@dgrtwo
Last active January 5, 2020 16:32
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 dgrtwo/e8ca5c8655f4f70cf1db8d52b7736048 to your computer and use it in GitHub Desktop.
Save dgrtwo/e8ca5c8655f4f70cf1db8d52b7736048 to your computer and use it in GitHub Desktop.
Simulate scoring games of Dice 10,000
# 100,000 simulations
n <- 1e5
dice <- t(rmultinom(n, 5, rep(1, 6) / 6))
# First rules for 1s and 5s
ones_score <- c(0, 100, 200, 1000, 5000, 10000)[dice[, 1] + 1L]
fives_score <- c(0, 50, 100, 0, 0, 0)[dice[, 5] + 1L]
# 100 x n score
most_common <- apply(dice, 1, which.max)
maxes <- dice[cbind(seq_len(n), most_common)]
max_multiplier <- c(0, 0, 100, 200, 300)[maxes]
# Note that 1s are a special case, see above
multiple_score <- (most_common != 1) * most_common * max_multiplier
# Straights happen with c(1, 1, 1, 1, 1, 0) or c(0, 1, 1, 1, 1, 1)
is_one <- dice == 1L
is_straight <- (rowSums(is_one[, 1:5]) == 5 | rowSums(is_one[, 2:6]) == 5)
# A straight is just 1000; no other scores
pmax(ones_score + fives_score + multiple_score, 1000 * is_straight)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment