Le Monde puzzle [#1146]
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The weekly puzzle from Le Monde is once more disappointing.
Everyday of the month, take 0, 1 or 2 units. If one unit taken past day, next day none can be taken. If two units taken two day ago, none can be taken the current day. What is the strategy maximising the number of units for February? March? April? Generalise to 0,..,3 units taken each day with 0 compulsory three days after taking 3 units.
as taking 2-1-0 (or 3-2-1-0) sounds like the optimal strategy. Except at the final step when 2, 2-2, 2-2-0, and 1-0-2-2 are best. But then why would one distinguish between the three months..?! Because the outcome differ, as 30, 32, and 33, resp. (or 45, 48 and 51). With an average increase of 1 in the first case and 1.5 in the second.
Another puzzle took too much of my time, namely a code golf challenge to devise a code taking as input a matrix of moves over an n x x grid and returning as input the number of nodes and transient tributaries for each loop (or irreducible set) of the moves. Which I solved by running Markov chains from each starting point long enough to reach stationarity. Entering the moves as
n=3;M=matrix(sample(1:4,n^2,rep=T),n)
and returning the result via 390 bytes
j=cbind;l=sum;a=apply m=l(!!M);n=m^.5 g=function(A,r=M[A])A+c((r<2)*(1-n*(A[,1]==n))-(r==2)*(1-n*(A[,1]<2)), (r==3)*(1-n*(A[,2]==n))-(r>3)*(1-n*(A[,2]<2))) I=c() for(i in d<-1:n)I=rbind(I,j(i*d/d,d)) for(t in b<-1:m)I=g(I) p=function(i)i[,1]+n*i[,2]-n-1 K=matrix(0,m,m) for(t in b)K[b+m*p(I<-g(I))]=1 s=o=a(u<-unique(K),1,l) for(k in 1:l(!!s))s[k]=l(!a(!!sweep(K,2,u[k,],'-'),1,l)) j(o,s-o)
which could be much shorter if only matrices could be indexed from 0 as in C.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.