Problem 227: The Chase

"The Chase" is a game played with two dice and an even number of players.

The players sit around a table; the game begins with two opposite players having one die each.
On each turn, the two players with a die roll it.
If a player rolls a 1, he passes the die to his neighbour on the left; if he rolls a 6, he passes the die to his neighbour on the right;
otherwise, he keeps the die for the next turn.
The game ends when one player has both dice after they have been rolled and passed; that player has then lost.

In a game with 100 players, what is the expected number of turns the game lasts?

Give your answer rounded to ten significant digits.

My Algorithm

The expected number of turns can be computed as:1 * p(1) + 2 * p(2) + 3 * p(3) + ... + n * p(n)
where p(n) is the probability that the two dices arrive at the same player after n rounds.

My program tracks the distance between the two players with a dice.
The vector last[i] contains the probability that the dices are i units apart.
Initially they are 100 / 2 = 50 units away from each other therefore last[i] = 0 except last[50] = 1.

In the following iterations the program repeatedly looks at each last[i]:

in 1/36 of all cases player 1 rolls a 1 and player 2 rolls a 6 → the distance is reduced by 2

in 8/36 of all cases the distance is reduced by 1 (one player rolls 2/3/4/5, the other moves one unit closer)

in 18/36 of all cases the distance remains unchanged (both roll 2/3/4/5 or both dices move into the same direction)

in 8/36 of all cases the distance is increased by 1 (one player rolls 2/3/4/5, the other moves one unit away)

in 1/36 of all cases the distance is icnreased by 2 (roll 6 and 1)

There are a few edge cases:

the distance can't be more than 50: if the players were 49 units apart then moving two units apart actually means that the distance remains unchanged

the distance can't be less than 0: if the players were 1 unit apart then moving two units closer actually means that the distance remains unchanged

Alternative Approaches

My first code was a bit buggy (I didn't think through all edge cases) and therefore wrote a simpler algorithm that matched my Monte-Carlo results:
two nested loops just roll out all combinations which simplifies things a lot. It's about 4x slower but still finishes in less than 0.2 seconds.
Use the #define FAST to switch between my final code (FAST in enabled by default) and that simpler and somehow even more beautiful code.

Note

The program stops if sufficient precision has been reached. The value of Epsilon was chosen manually by observing when the result stabilized.
It's optimized for 100 players may be a bit off when running live tests with a different number of players.

Interactive test

You can submit your own input to my program and it will be instantly processed at my server:

Input data (separated by spaces or newlines):

This is equivalent toecho 10 | ./227

Output:

(please click 'Go !')

Note: the original problem's input 100cannot be enteredbecause just copying results is a soft skill reserved for idiots.

(this interactive test is still under development, computations will be aborted after one second)

My code

… was written in C++11 and can be compiled with G++, Clang++, Visual C++. You can download it, too.

#include<iostream>

#include<iomanip>

#include<vector>

// ---------- Monte Carlo simulation ----------

// note: not used anymore, I wrote it to generate a good approximation of the result

// a simple pseudo-random number generator

// (produces the same result no matter what compiler you have - unlike rand() from math.h)

unsignedintmyrand()

{

staticunsignedlonglong seed = 0;

seed = 6364136223846793005ULL * seed + 1;

return (unsignedint)(seed >> 30);

}

// estimate result with a Monte Carlo simulation

doublemonteCarlo(unsignedint players, unsignedint iterations)

{

// count rolls

unsignedint rolls = 0;

for (unsignedint i = 0; i < iterations; i++)

{

// initial positions: opposite sides of the table

unsignedint player1 = 0;

unsignedint player2 = players / 2;

// both dices at same position?

while (player1 != player2)

{

// roll first dice

auto dice1 = (myrand() % 6) + 1;

// and move the dice to a neighbor

if (dice1 == 1)

player1 = (player1 + players - 1) % players;

if (dice1 == 6)

player1 = (player1 + 1) % players;

// roll second dice

auto dice2 = (myrand() % 6) + 1;

// and move the dice to a neighbor

if (dice2 == 1)

player2 = (player2 + players - 1) % players;

if (dice2 == 6)

player2 = (player2 + 1) % players;

rolls++;

}

}

return rolls / double(iterations);

}

// I thought I needed long double but the standard double works perfectly (while float fails)

typedefdouble Number;

intmain()

{

// number of players

unsignedint players = 100;

std::cin>> players;

// abort if result stabilized

constauto Epsilon = 0.00000000001;

// before the first round, the dices are always players/2 units apart (always means 100%)

std::vector<Number>last(players / 2 + 1, 0);

last.back() = 1;

// expected = 1 * probability(finished after 1 iteration) +

// 2 * probability(finished after 2 iterations) +

// 3 * probability(finished after 3 iterations) +

// 4 * probability(finished after 4 iterations) + ...

Number expected = 0;

for (unsignedint iteration = 1; ; iteration++) // no abort condition

{

// probabilities after this iteration

std::vector<Number>next(last.size(), 0);

// analyze each distance except 0 (because those are already finished and not relevant for the next iteration)

Those links are just an unordered selection of source code I found with a semi-automatic search script on Google/Bing/GitHub/whatever.
You will probably stumble upon better solutions when searching on your own.
Maybe not all linked resources produce the correct result and/or exceed time/memory limits.

Heatmap

Please click on a problem's number to open my solution to that problem:

green

solutions solve the original Project Euler problem and have a perfect score of 100% at Hackerrank, too

yellow

solutions score less than 100% at Hackerrank (but still solve the original problem easily)

gray

problems are already solved but I haven't published my solution yet

blue

solutions are relevant for Project Euler only: there wasn't a Hackerrank version of it (at the time I solved it) or it differed too much

orange

problems are solved but exceed the time limit of one minute or the memory limit of 256 MByte

red

problems are not solved yet but I wrote a simulation to approximate the result or verified at least the given example - usually I sketched a few ideas, too

black

problems are solved but access to the solution is blocked for a few days until the next problem is published

[new]

the flashing problem is the one I solved most recently

I stopped working on Project Euler problems around the time they released 617.

The 310 solved problems (that's level 12) had an average difficulty of 32.6&percnt; at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of &approx;60000 in August 2017)
at Hackerrank's Project Euler+.

My username at Project Euler is stephanbrumme while it's stbrumme at Hackerrank.

Copyright

I hope you enjoy my code and learn something - or give me feedback how I can improve my solutions.All of my solutions can be used for any purpose and I am in no way liable for any damages caused.You can even remove my name and claim it's yours. But then you shall burn in hell.

The problems and most of the problems' images were created by Project Euler.Thanks for all their endless effort !!!

more about me can be found on my homepage,
especially in my coding blog.
some names mentioned on this site may be trademarks of their respective owners.
thanks to the KaTeX team for their great typesetting library !