Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A collection of liars in this Le Monde mathematical puzzle:

1. A circle of 16 liars and truth-tellers is such that everyone states that their immediate neighbours are both liars. How many liars can there be?
2. A circle of 12 liars and truth-tellers is such that everyone state that their immediate neighbours are one liar plus one truth-teller. How many liars can there be?
3.  A circle of 8 liars and truth-tellers is such that four state that their immediate neighbours are one liar plus one truth-teller and four state that their immediate neighbours are both liars . How many liars can there be?

These questions can easily be solved by brute force simulation. For the first setting, using 1 to code truth-tellers and -1 liars, I simulate acceptable configurations as

tabz=rep(0,16)
tabz[1]=1 #at least one
tabz[2]=tabz[16]=-1
for (i in 3:15){
if (tabz[i-1]==1){
tabz[i]=-1}else{
if (tabz[i+1]==-1){
tabz[i]=1}else{
if (tabz[i+1]==1){
tabz[i]=-1}else{
if (tabz[i-2]==-1){
tabz[i]=1}else{
tabz[i]=sample(c(-1,1),1)
}}}}}


which produces 8, 9, and 10 as possible (and obvious) values.

The second puzzle is associated with the similar R code

tabz=sample(c(-1,1),12,rep=TRUE)
rong=FALSE
while (!rong){
for (i in sample(12)){
if (tabz[i-1+12*(i==1)]*tabz[i%%12+1]==-1){
tabz[i]=1}else{
tabz[i]=sample(c(-1,1),1)}
}
rong=TRUE
for (i in (1:12)[tabz==1])
rong=rong&(tabz[i-1+12*(i==1)]*tabz[i%%12+1]==-1)
if (rong){
for (i in (1:12)[tabz==-1])
rong=rong&(tabz[i-1+12*(i==1)]*tabz[i%%12+1]!=-1)
}}


with numbers of liars (-1) either 12 (obvious) or 4.

The final puzzle is more puzzling in that figuring out the validating function (is an allocation correct?) took me a while, the ride back home plus some. I ended up with the following code that samples liars (-1) and thruth-seekers (1) at random, plus forces wrong and right answers (in 0,1,2) on these, and check for the number of answers of both types:

rong=FALSE
while (!rong){
tabz=sample(c(-1,1),8,rep=TRUE) #truth
tabz[1]=1;tabz[sample(2:8,1)]=-1
tt=(1:8)[tabz==1];lr=(1:8)[tabz==-1]
statz=rep(0,8) #stmt
statz[tt]=(tabz[tt-1+8*(tt==1)]*tabz[tt%%8+1]==-1)+
2*(tabz[tt-1+8*(tt==1)]+tabz[tt%%8+1]==-2)
statz[lr]=2*(tabz[lr-1+8*(lr==1)]*tabz[lr%%8+1]==-1)+
(tabz[lr-1+8*(lr==1)]+tabz[lr%%8+1]==-2)+
sample(c(1,2),8,rep=TRUE)[lr]*
(tabz[lr-1+8*(lr==1)]+tabz[lr%%8+1]==2)
rong=(sum(statz==1)==4)&(sum(statz==2)==4)}


with only solutions 3 and 4.

Filed under: Books, Kids, R Tagged: competition, Le Monde, liar puzzle, mathematical puzzle