Le Monde puzzle [#1092]
This article is originally published at https://xianblog.wordpress.com
A Latin square Le Monde mathematical puzzle that I found rather dreary:
A hidden 3×3 board contains all numbers from 1 to 9. Anselm wants to guess the board and makes two proposals. Berenicke tells him how many entries are in the right rows and colums for each proposal, along with the information that no entry is at the right location. Anselm deduces the right board.
Which I solved by brute force and not even simulated annealing, first defining a target
ordoku1=ordoku2=matrix(1,9,2) ordoku1[,1]=c(1,1,1,2,2,2,3,3,3) ordoku1[,2]=rep(1:3,3) ordoku2[,1]=c(3,2,3,1,2,3,2,1,1) ordoku2[,2]=c(2,2,3,2,3,1,1,3,1) fitz=function(ordo){ (sum(ordo[c(1,4,7),2]==1)==1)+(sum(ordo[c(2,5,8),2]==2)==1)+ (sum(ordo[c(3,6,9),2]==3)==0)+(sum(ordo[c(1,2,3),1]==1)==1)+ (sum(ordo[c(4,5,6),1]==2)==1)+(sum(ordo[c(7,8,9),1]==3)==2)+ (sum(ordo[c(6,7,9),2]==1)==2)+(sum(ordo[c(1,2,4),2]==2)==1)+ (sum(ordo[c(3,5,8),2]==3)==2)+(sum(ordo[c(4,8,9),1]==1)==1)+ (sum(ordo[c(7,2,5),1]==2)==1)+(sum(ordo[c(1,3,6),1]==3)==0)+ (!(0%in%apply((ordo-ordoku1)^2,1,sum)))+(!(0%in%apply((ordo-ordoku2)^2,1,sum))) }
on a 9×9 board entry reproducing all items of information given by Berenicke. If all constraints are met, the function returns 14. And then searched for a solution at random:
temp=1 randw=function(ordo){ for (t in 1:1e6){ chlg=sample(1:9,2) temp=ordo[chlg[1],] ordo[chlg[1],]=ordo[chlg[2],] ordo[chlg[2],]=temp if (fitz(ordo)==14){ print(ordo);break()}}}
which produces the correct board
4 3 5 6 7 1 9 2 8
Thanks for visiting r-craft.org
This article is originally published at https://xianblog.wordpress.com
Please visit source website for post related comments.