## puzzles & riddles

**A** rather simplistic game on the Riddler of 18 December:

…two players, each of whom starts with a whole number of points. Players take turns “attacking” each other, which involves subtracting their own number of points from their opponent’s until one of the players is out of points.

Easy to code in R:

g=function(N,M)ifelse(N<M,-g(M-N,N),1) f=function(N,M=N){ while(g(N,M)>0)M=M+1 return(M)}

which converges to the separating ratio 1.618. If decomposing the actions until one player wins, one gets a sequence of upper and lower bounds associated with the Fibonacci sequence: 1⁻, 2⁺, 3/2⁻, 5/3⁺, 8/5⁻, &tc, converging to the “golden ratio” φ.

As an aside, I also solved a relatively quick codegolf challenge, where the question was to find the sum of all possible binary values from a bitmask. Meaning that for a binary input, e.g., 101X0XX0…01X, with some entries masked by X’s, one had to find the sum of all binary numbers compatible with the input. Which can be solved succinctly by counting the number of X’s, k, and adding the visible bits times and replacing the invisible ones by . With some help, and using 2 instead of X, my R code moved from 158 bytes to 50:

function(x)2^sum(a<-x>1)*rev(x/4^a)%*%2^(seq(x)-1)

January 3, 2021 at 7:51 pm

That was some hefty reduction! I love the collaborative nature of CCGC, how people try to help each other

January 3, 2021 at 10:09 pm

Yes, this is a very collaborative forum, compared with others. Maybe because the population there is way smaller. And seeing the challenges as a game…

January 3, 2021 at 4:47 pm

one more bit gone

2^sum(a1)*rev(x/4^a)%*%2^(seq(x)-1)

January 3, 2021 at 6:07 pm

Thanks!

January 3, 2021 at 8:15 am

[…] article was first published on R – Xi'an's Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) […]

January 3, 2021 at 4:05 am

[…] article was first published on R – Xi’an’s Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

January 3, 2021 at 4:02 am

[…] article was first published on R – Xi’an’s Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

January 3, 2021 at 2:13 am

[…] by data_admin [This article was first published on R – Xi’an’s Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]