## Le Monde puzzle [#1155]

Posted in Books, Kids, R with tags , , , , , , , on September 26, 2020 by xi'an The weekly puzzle from Le Monde is another Sudoku challenge:

Anahera and Wiremu play a game for T rounds. They successively pick a digit between 1 and 3, never repeating the previous one, and sum these digits. The last to play wins if the sum is a multiple of 3. Who is the winner for an optimal strategy?

By a simple dynamic programming of the optimal strategy at each step

```N=3
A=matrix(-1,20,N)
A[1,1:N]=1:N
for (T in 2:20)
for (i in 1:N) A[T,i]=i+ifelse(!T%%2, #parity check
max((i+A[T-1,-i])%%3), #avoid zero
min((i+A[T-1,-i])%%3)) #seek zero
```

the first to play can always win the game. Not fun!

## Le Monde puzzle [#1146]

Posted in Books, Kids, R with tags , , , , , , , , on June 5, 2020 by xi'an 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.

## one or two?

Posted in Books, Kids, R with tags , , , , , , on March 12, 2020 by xi'an

A superposition of two random walks from The Riddler:

Starting from zero, a random walk is produced by choosing moves between ±1 and ±2 at each step. If the choice between both is made towards maximising the probability of ending up positive after 100 steps, what is this probability?

Although the optimal path is not necessarily made of moves that optimise the probability of ending up positive after the remaining steps, I chose to follow a dynamic programming approach by picking between ±1 and ±2 at each step based on that probability:

```bs=matrix(0,405,101) #best stategy with value i-203 at time j-1
bs[204:405,101]=1
for (t in 100:1){
tt=2*t
bs[203+(-tt:tt),t]=.5*apply(cbind(
bs[204+(-tt:tt),t+1]+bs[202+(-tt:tt),t+1],
bs[201+(-tt:tt),t+1]+bs[205+(-tt:tt),t+1]),1,max)}
```

resulting in the probability

```> bs[203,1]
 0.6403174
```

Just checking that a simple strategy of picking ±1 above zero and ±2 below leads to the same value

```ga=rep(0,T)
for(v in 1:100) ga=ga+(1+(ga<1))*sample(c(-1,1),T,rep=TRUE)
```

or sort of

```> mean(ga>0)
 0.6403494
```

With highly similar probabilities when switching at ga<2

```> mean(ga>0)
 0.6403183
```

or ga<0

```> mean(ga>0)
 0.6403008
```

and too little difference to spot a significant improvement between the three boundaries.

## Le Monde puzzle [#1085]

Posted in Books, Kids, R with tags , , , , , on February 18, 2019 by xi'an

Given 13 arbitrary relative integers chosen by Bo, Abigail can select any subset of them to be drifted by plus or minus one by Bo, repeatedly until Abigail reaches the largest possible number N of multiples of 5. What is the minimal possible value of N under the assumption that Bo tries to minimise it?

I got stuck on that one, as building a recursive functiion led me nowhere: the potential for infinite loop (add one, subtract one, add one, …) rather than memory issues forced me into a finite horizon for the R function, which then did not return anything substantial in a manageable time. Over the week and the swimming sessions, I thought of simplifying the steps, like (a) work modulo 5, (b) bias moves towards 1 or 4, away from 2 and 3, by keeping only one entry in 2 and 3, and all but one at 1 and 4, but could only produce five 0’s upon a sequence of attempts… With the intuition that only 3 entries should remain in the end, which was comforted by Le Monde solution the week after.

## Le Monde puzzle [#1070]

Posted in Books, Kids, R, University life with tags , , , , , , , on October 11, 2018 by xi'an Rewording Le Monde mathematical puzzle  fifth competition problem

For the 3×3 tables below, what are the minimal number of steps to move from left to rights when the yellow tokens can only be move to an empty location surrounded by two other tokens? In the 4×4 table below, there are 6 green tokens. How many steps from left to right? Painful and moderately mathematical, once more… For the first question, a brute force simulation of random valid moves of length less than 100 returns solutions in 4 steps:

```     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
1    1    1    0    0    1    0    0    1
1    0    1    0    1    1    0    0    1
0    0    1    1    1    1    0    0    1
0    0    1    1    0    1    0    1    1
0    0    1    0    0    1    1    1    1
```

But this is not an acceptable move because of the “other” constraint. Imposing this constraint leads to a solution in 9 steps, but is this the lowest bound?! It actually took me most of the weekend (apart from a long drive to and from a short half-marathon!) to figure out a better strategy than brute force random exploration: the trick I eventually figured out is to start from the finishing (rightmost) value F of the grid and look at values with solutions available in 1,2,… steps. This is not exactly dynamic programming, but it keeps the running time under control if there is a solution associated with the starting (leftmost) value S. (Robin proceeded reverse-wise, which in retrospect is presumably equivalent, if faster!) The 3×3 grid has 9 choose 5, ie 126, possible configurations with 5 coins, which means the number of cases remains under control. And even so for the 4×4 grid with 6 coins, with 8008 configurations. This led to a 9 step solution for n=3 and the proposed starting grid in yellow:

``` 1 1 1 0 0 1 0 0 1
 1 1 0 0 1 1 0 0 1
 1 1 0 1 1 0 0 0 1
 0 1 0 1 1 1 0 0 1
 0 1 1 1 0 1 0 0 1
 1 1 1 1 0 0 0 0 1
 0 1 1 1 1 0 0 0 1
 0 0 1 1 1 0 0 1 1
 0 0 1 1 0 0 1 1 1
 0 0 1 0 0 1 1 1 1
```

and a 19 step solution for n=4:

``` 1 0 0 0 0 1 0 0 0 1 1 1 0 0 0 1
 1 0 0 0 0 1 0 0 0 0 1 1 0 0 1 1
 1 0 0 0 0 1 1 0 0 0 1 1 0 0 1 0
 1 1 0 0 0 1 1 0 0 0 0 1 0 0 1 0
 1 1 0 0 0 0 1 1 0 0 0 1 0 0 1 0
 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 0
 1 0 1 1 0 0 1 1 0 0 0 0 0 0 1 0
 1 1 1 1 0 0 1 0 0 0 0 0 0 0 1 0
 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0
 1 0 0 1 1 1 1 0 0 0 0 0 0 0 1 0
 0 0 0 1 1 1 1 0 0 0 1 0 0 0 1 0
 0 0 0 1 1 1 0 0 0 1 1 0 0 0 1 0
 0 0 0 1 1 1 0 0 1 1 0 0 0 0 1 0
 0 0 0 1 0 1 0 0 1 1 0 0 0 1 1 0
 0 0 0 1 0 1 0 0 1 0 0 0 1 1 1 0
 0 0 0 1 1 1 0 0 1 0 0 0 1 0 1 0
 0 0 0 1 0 1 0 0 1 1 0 0 1 0 1 0
 0 0 0 1 0 1 0 0 0 1 1 0 1 0 1 0
 0 0 0 1 0 1 1 0 0 1 1 0 1 0 0 0
```

The first resolution takes less than a minute and the second one just a few minutes (or less than my short morning run!). Surprisingly, using this approach does not require more work, which makes me wonder at the solution Le Monde journalists will propose. Given the (misguided) effort put into my resolution, seeing a larger number of points for this puzzle is no wonder.