One of my friends suggested me to play a game. It is a turn-based math game that requires 2 players. Here are the rules:
- Both players propose a 4-digits number.
- Then, using a coin, they decide who goes first.
- First players is trying to guess the number of the second one.
If he didn't guess it, the second player says two numbers to the first player:
- The first number describes how many digits first player guessed in total (any positions counts).
The second number describes how many digits you have guessed are in the right positions.
Example: The first player's proposed number is
2737Second player is trying to guess number. He says2274The first player will respond with2, 1
Then players switch.
- It lasts until on of players finally guess the proposed number of his opponent.
I won this game using the first algorithm that came on my mind:
My solution
Imagine that our opponent's number is 4856
First, we try four different digits.
1234Then we look at the answer. In our case, it's
1, 0On the next turn, we increase or number by one from the highest number we have tried so far. So it will be five.
5234 #=> [2, 0]Now since we discovered the new number, we move it on the left, replacing the one we have on that position. And also increase the first one.
6534 #=> [3, 0]7654 #=> [3, 1]We got the first number it is either 6 or 5 since 7 didn't increase or first meter.8654 #=> [4, 1]Yay! We found all numbers, now we just need to find right places for them. So we try to replace them starting from the left one.6854 #=> [4, 2]5864 #=> [4, 1]Here we see that we lost one digit, which means it was on the right place, we bring it back, and ignore in the future.4856 #=> [4, 4]We won.
We did it in 8 turns. I tried this algorithm with some other people, and it mostly did work, but honestly, I think my algorithm is super dumb. There must be faster and more universal solution which will provide high chances to win this game. And I am asking you to help me to find this solution, guys. Any ideas?