Skip to content

Instantly share code, notes, and snippets.

@stulacy
Created July 18, 2023 16:14
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 stulacy/a15e20f878f834cf1d2154271e6d7c36 to your computer and use it in GitHub Desktop.
Save stulacy/a15e20f878f834cf1d2154271e6d7c36 to your computer and use it in GitHub Desktop.
Solves the puzzle in COD Warzone
library(stringr)
solve <- function(code) {
# Extract the num
nums <- as.numeric(strsplit(gsub("[A-Z]", "", code), '')[[1]])
letters <- unique(strsplit(gsub("[0-9]", "", code), "")[[1]])
miss_nums <- setdiff(seq(1, 9), nums)
# Generate all permutations of possible solution numbers
# No permutation function in R so make my own
perms <- miss_nums
for (i in 1:(length(letters) - 1)) {
perms <- cbind(perms, miss_nums)
}
perms <- expand.grid(as.data.frame(perms))
# Remove all entries where number appears more than once
n_unique <- apply(perms, 1, function(x) length(unique(x)))
perms <- perms[n_unique == ncol(perms), ]
perms
for (i in 1:nrow(perms)) {
attempt <- str_replace_all(code, setNames(as.character(perms[i, ]), letters))
attempt_pt1 <- substr(attempt, 1, 4)
attempt_pt2 <- substr(attempt, 5, 8)
cat(sprintf("Attempt %d/%d: %s\t%s\n\n", i, nrow(perms), attempt_pt1, attempt_pt2))
readline()
}
}
code <- 'H5NN6C4H'
solve('H5NN6C4H')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment