Created
July 18, 2023 16:14
-
-
Save stulacy/a15e20f878f834cf1d2154271e6d7c36 to your computer and use it in GitHub Desktop.
Solves the puzzle in COD Warzone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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