This reminds me of a game from years ago - it was called MasterMind or something like that. Years ago. Mathematically it's simple, but I won't give away the algorithm, especially as I got caught by the second safe :-)