So, I and my friends have a game named "Strike Ball". It's basically number guessing game. There are minimum 2 player. Both of them think a n digit number where every digit is different (ex. 1210 isn't allowed). Then they guess the opponent's number, the opponent will answer "k Strikes l Balls" where:
Strikes happened when the digit match the position and the number. So, if opponent's number is 1284 and you guess 1385, it's 2 Strikes.
Balls happened when the digit match the number, but the position isn't right. So, if opponent's number is 1284 and you guess 2173, it's 2 Balls.
The players take turn until someone finally get n Strikes (the player guess the number right) and the player who gets n Strikes wins.
My question is, is there any mathematical calculation to guess the number? Because all I can do is brute force by saying every digit possibly and it's kinda not effective.
I got motivated to write a close-to-optimal solver for you. This solver only works once you have already figured out exactly which digits you need, and only need to guess their correct permutation. It is a little brute-force: it remembers all possible permutations, and every time you ask a question and get a certain number of strikes $= M$, it selects only those permutations that have exactly $M$ digits in the same positions as the permutation in question, and deletes all the others. The permutation in question is selected at random out of those that are still considered possible. Surprisingly, for 10 digits it only requires approximately 10 questions to arrive at the correct permutation. It can still be optimized by considering which permutation could potentially exclude most of the remaining possible permutations, but such an algorithm would be at least $O(N_P^2)$ where $N_P$ is the number of possible permutations, which would take a really long time to compute. While I hate to have an algorithm scaling with the number of permutations (which is 3 millions for 10 digits), I don't think it is possible to easily reduce the dimension of this problem - it is equivalent to cutting out hyperspheres in from from a convex polytope in 10D space. I don't think there is a compact representation of all the points remaining after a few such cuts. So any efficient strategy for solving this problem is most likely impossible to execute in your head or on a piece of paper.
Note: In my code I have used a notion of distance. Distance is $D - M$, where $D$ is the number of digits and $M$ is number of strikes
Here is the code in Python:
Here is an example output of the code