Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Created August 15, 2016 11:18
Show Gist options
  • Save explodecomputer/63cdb7bde1de49571f8c9c134873e6bc to your computer and use it in GitHub Desktop.
Save explodecomputer/63cdb7bde1de49571f8c9c134873e6bc to your computer and use it in GitHub Desktop.
puzzle.R
library(gtools)
library(dplyr)
func <- function(g, s, b)
{
l <- permutations(3, 3, v=c(g,s,b))
return(l)
}
l <- func(5, 2, 1)
func2 <- function(l, n, obj=c(22,9,9))
{
P <- permutations(6, n)
for(i in 1:nrow(P))
{
a <- l[P[i,], ]
colSums(a)
if(all(colSums(a) %in% obj)) return(a)
}
return(NULL)
}
get_combinations <- function(n)
{
t <- 40 / n
stopifnot(t %%1 == 0)
gmin <- 3
l <- list()
j <- 1
for(g in c(3:(t-3)))
{
r1 <- t - g
for(s in c(2:(r1-1)))
{
b <- t - g - s
if(g > s & s > b & (g + s + b) == t & all(c(g,s,b) > 0))
{
l[[j]] <- c(g, s, b)
j <- j + 1
}
}
}
return(do.call(rbind, l))
}
get_combinations(20)
values <- list()
j <- 1
for(i in c(2,4,5,10,20))
{
values[[j]] <- get_combinations(i)
j <- j + 1
}
values <- do.call(rbind, values)
for(i in 1:nrow(values))
{
print(func2(func(values[i,1], values[i,2], values[i,3]), 4))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment