[Math] Minimum number of attempts to guess a PIN code, given constraints

probabilitystatistics

I'm playing a video game at the moment called Sleeping Dogs, in which some of the mini-missions are to 'hack' a security camera, by guessing a four-digit PIN code.

Here are the rules:

1) You are allowed 6 attempts to enter a four-digit PIN code. After 6 attempts, the PIN code resets to a random (other) one.

2) Repeated digits are not allowed (e.g. $9981, 1131, 5555,$ etc. are not allowed).

3)
If the correct digit is in the correct place, that digit will be green.

If the correct digit (i.e. a digit that is in the actual PIN) is in the incorrect place, that digit will be amber.

If an incorrect digit is entered (i.e. a digit that is not in the actual PIN), that digit will be red.

e.g. Suppose that the actual code is $\boxed{1234}.$

If I entered $1427$, it would show up as

$$\color{green}1\color{orange}4\color{orange}2\color{red}7.$$

My question is this:

What is the minimum number of attempts in order to guarantee entry to the system, (can it be achieved with certainty in fewer than six attempts)?

There seem to be so many factors that I can't come up with a quick solution. Any hints/tips would be welcome.


(Background info– I'm familiar with elementary probability and statistics).

Best Answer

Once you know the correct numbers it should take you at most 3 more guesses to find their positions (since any number in wrong position can be corrected in at most 3 guesses).

Also you can find the four correct numbers by guessing 1234; 5678; 9012.

So this algorithm is perhaps not the most efficient, but always gives the answer within 6 tries.