Consider the following problem:
Who drinks water, and who owns the zebra?
See also http://rosettacode.org/wiki/Zebra_puzzle and https://www.youtube.com/playlist?list=PLB470B7816A914D87.
suppressPackageStartupMessages(library(combinat))
suppressPackageStartupMessages(library(xtable))
col <- factor(c("red","green","ivory","yellow","blue"),
levels=c("red","green","ivory","yellow","blue"))
own <- factor(c("eng","spa","ukr","jap","nor"),
levels=c("eng","spa","ukr","jap","nor"))
pet <- factor(c("dog","snails","fox","horse","zebra"),
levels=c("dog","snails","fox","horse","zebra"))
drink <- factor(c("coffee","tea","milk","juice","water"),
levels=c("coffee","tea","milk","juice","water"))
smoke <- factor(c("winston", "kools", "chesterfield", "strike", "parliaments"),
levels=c("winston", "kools", "chesterfield", "strike", "parliaments"))
col_p <- permn(levels(col))
own_p <- permn(levels(own))
pet_p <- permn(levels(pet))
drink_p <- permn(levels(drink))
smoke_p <- permn(levels(smoke))
You’ll have to declare some helper functions
imright <- function(h1,h2){
return(h1-h2==1)
}
nextto <- function(h1,h2){
return(abs(h1-h2)==1)
}
house_of <- function(f,val){
return(which(levels(f)==val))
}
Now deal with the hints.
for (i in seq(length(col_p))){
col <- factor(col, levels=col_p[[i]])
if (imright(house_of(col,"green"),house_of(col,"ivory"))) {
for (j in seq(length(own_p))){
own <- factor(own, levels=own_p[[j]])
if(house_of(own,"eng") == house_of(col,"red")){
if(house_of(own,"nor") == 1){
if(nextto(house_of(own,"nor"),house_of(col,"blue"))){
for(k in seq(length(drink_p))){
drink <- factor(drink, levels=drink_p[[k]])
if(house_of(drink,"coffee") == house_of(col,"green")){
if(house_of(own,"ukr") == house_of(drink,"tea")){
if(house_of(drink,"milk") == 3){
for(l in seq(length(smoke_p))){
smoke <- factor(smoke, levels=smoke_p[[l]])
if(house_of(smoke,"kools") == house_of(col,"yellow")){
if(house_of(smoke,"strike") == house_of(drink,"juice")){
if(house_of(own,"jap") == house_of(smoke,"parliaments")){
for(m in seq(length(pet_p))){
pet <- factor(pet, levels=pet_p[[m]])
if(house_of(own,"spa") == house_of(pet,"dog")){
if(house_of(smoke,"winston") == house_of(pet,"snails")){
if(nextto(house_of(smoke,"chesterfield"),house_of(pet,"fox"))){
if(nextto(house_of(smoke,"kools"),house_of(pet,"horse"))){
res <- sapply(list(own,col,pet,smoke,drink),levels)
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
Here is the solution:
colnames(res) <- c("Owner","Color","Pet","Smoke","Drink")
print(xtable(res, align="cccccc"), type="html", html.table.attributes='class="table table-striped table-hover center"')
| Owner | Color | Pet | Smoke | Drink | |
|---|---|---|---|---|---|
| 1 | nor | yellow | fox | kools | water |
| 2 | ukr | blue | horse | chesterfield | tea |
| 3 | eng | red | snails | winston | milk |
| 4 | spa | ivory | dog | strike | juice |
| 5 | jap | green | zebra | parliaments | coffee |