A Baseball Game

179 Views Asked by At

You and your friend decide to play a game. They think of a four-digit number, call it $k$. So $k$ is an integer from the range $[0000,9999]$. We can refer to the individual digits of $k$ as $k[1], k[2], k[3],k[4]$ as the first, second, third, and fourth digits of $k$, respectively. They want you to try to figure out $k$ through a series of guesses.

A guess $g_{i}$ ($i$ referring to the $i$th guess) is also a four-digit integer drawn from the range $[0000,9999]$. Your friend takes a guess $g_{i}$, and in response, gives you the number of strikes $s_{i}$ and the number of balls $b_{i}$ that the number yields.

A strike refers to a digit in your guess $g[i]$ that has the correct value and position as a digit in your friend's number $k[i]$.

A ball refers to a digit $g[i]$ that is the same value as another digit $k[j]$ in $k$ but is not in the correct position. A digit would only be a ball if the corresponding $k[j]$ is not taken up by a strike or ball.

Examples:

For example, with $k=1234$ and a guess $g=1243$, your friend would respond with $2$ strikes and $2$ balls.

Another example, with $k=1234$ and a guess $g=1010$, your friend would respond with $1$ strike and no balls. $g[3]$ wouldn't count as a ball as $k[1]$ is already taken by $g[1]$ as a strike. For any guess $g_{i}$, the corresponding $s_{i} + b_{i} \leq 4$.

We can go through a game:

$k = 4567$
$g_{1} = 1111\,\,\,\,\,\,\,\,\,b_{1}=0\,\,\,\,\, s_{1}=0$
$g_{2} = 2345\,\,\,\,\,\,\,\,\,b_{2}=2\,\,\,\,\, s_{2}=0$
$g_{3} = 3425\,\,\,\,\,\,\,\,\,b_{3}=2\,\,\,\,\, s_{3}=0$
$g_{4} = 4325\,\,\,\,\,\,\,\,\,b_{4}=1\,\,\,\,\, s_{4}=1$
$g_{5} = 4532\,\,\,\,\,\,\,\,\,b_{5}=0\,\,\,\,\, s_{5}=2$
$g_{6} = 4567\,\,\,\,\,\,\,\,\,b_{6}=0\,\,\,\,\, s_{6}=4$

By the fourth guess, you know that the first place, $k[1]$, is $4$ and I think you would know that the ball for the fourth guess is either $2$ or $5$ based on the previous three guesses. The friend that I was playing with got lucky by guessing $5$ for $k[2]$ by the fifth guess, and so they knew that the first two digits of $k$ were $4$ and $5$. They just guessed $4567$ after figuring out the first two. Note that the game ends when a guess $g_{i}$ has an $s_{i}=4$, because you've guessed all the digits correctly and so you know $k$. Thus, by $g_{6}$ the game ends and it would have taken $6$ guesses.

The question:
Given an ideal algorithm/player that plays this game optimally, what is the maximum (worst-case) number of guesses that you would need in order to win the game?

Note, again, that this would be equivalent to finding $k$ and getting four strikes. Also, in order for one to answer this question they first would need to find the optimal algorithm/player, so please elaborate on that in your response. Thanks.