## Copyright (C) 2017 Dominic Walden
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program. If not, see .
locksNeeded <- function(P, p) {
# Initialise list
pirates <- list()
for(i in 1:P) {
# Have to use 0 rather than c() as cannot append to an empty vector
pirates[[i]] <- 0
}
# First value of the unique key
uKey <- 1
# All combinations of p - 1 pirates
smallComb <- data.frame(combn(P, p - 1))
# For each of the p - 1 pirate combinations
for(comb in smallComb) {
# For each pirate in P
for(i in 1:P) {
# If pirate is not in the combination
if(!is.element(i, comb)) {
if(all(pirates[[i]] == 0)) {
pirates[[i]] <- uKey
} else {
# Give pirate the unique key for that combination
pirates[[i]] <- append(pirates[[i]], uKey)
}
}
}
# New unique key for next combination of pirates
uKey <- uKey + 1
}
pirates
}