Search code examples
algorithmpython-3.xpuzzle

Word guessing game, what algorithm to use?


I have a list of number [1,2,3,4,5,6,7,8,9,10] and now i make some combination out of them. Let's say [2,2,4,5,1,1].

Now the program has to guess what combinations did I think.
Then the program will generate a random list with the same length as was my combination. Let's say, it will be [1,2,5,5,4,3].

Now I have to tell how many numbers where at the correct location and how many numbers where correct but at the wrong place. So, at the correct are two numbers in this case (2 and 5). Also 1 and 3 where correct but at the wrong place.

Initially, the program should guess the password using my hints. Can someone please help me figure out or give hints to an algorithm that is used to solve this kind of problem.


Solution

  • "Then the program will generate a random list with the same length as was my combination" - is this part of the problem? You say later that the program uses your hints, so I assume you are generating random lists of numbers to try and trying to figure out the password while using only random lists of numbers and not using the information you have to make better guesses that can narrow down the results more quickly.

    In that case, you need to keep track of your guesses and for each one figure out what information you can glean from them. Each time you get new information from a guess, you can go back to the results of your previous guesses and see if you get any new information. For your example, let's say it goes like this including my made up guesses after the first:

    PASSWORD: [2,2,4,5,1,1]
    GUESS 1:  [1,2,5,5,4,3] - 4 correct, 2 right position
    GUESS 2:  [5,2,1,7,3,8] - 3 correct, 1 right position
    GUESS 3:  [7,7,8,3,7,9] - 0 correct, 0 right position
    GUESS 4:  [5,6,5,3,8,9] - 1 correct, 0 right position
    GUESS 5:  [8,2,5,6,7,3] - 2 correct, 1 right position
    

    After guess 3 you know that you have no 3s, 7s, 8s or 9s. Going back to your second guess, you know that three of the numbers are 1,2,5 because you have no 3,7,8. Going back to your first guess with that knowledge you know that the answer has 1,2,5 as well as either a 4 or 5. Looking at the 4th guess you now know there is only one 5, so four of your numbers are 1,2,4,5. Continue with this algorithm until you figure out all 6 numbers, not caring what position they are in.

    Once you have all 6 numbers, you switch algorithms to figure out what positions the numbers are in. Go back through all your previous guesses each time you learn new information and continue guessing random passwords if you are stuck. For the initial run, you know that 5 is not in positions one or three because of guess 4. Guess 2 - you know the 5 isn't in position 1, so either the 2 or the 1 is in the right position [X,2,1,X,X,X]. Guess 1 you know that the 5 isn't in position 3, so 2 of the following are in the right position: [1,2,X,5,4,X]. Guess 5 tells you that either the 2 or the 5 is in the right position, but you know the 5 is in the wrong position because of Guess 4, so now you know the 2 is in position 2. Combining this information with guess 2 tells you that the 1 is not in position 3 either, so you know it has to be 4 or 2.

    One way to represent your knowledge would be having arrays for min and max counts for each digit and an array of boolean for each spot telling you what numbers can go there. Once you have all the information you can get from a guess, you can remove it from the list so you don't have to look at it again. So after Guess 3, the max number of 3,7,8,9 are 0. You can also set those possibilities to false for each position. That is all the information you can get so you no longer have to look at Guess 3.

    You can also alter the guesses to simplify it, for instance once you know that 2 goes in position 2 and that there are no 3,7,8, you can remove those "knowns" and Guess 2 changes:

    GUESS 2:  [5,2,1,7,3,8] - 3 correct, 1 right position
    NEW   2:  [5,X,1,X,X,X] - 2 correct, 0 right position
    

    But once you know all the numbers and have set 5 to false for position 1 and 1 to false for position 3, this can't give you more information and you can remove this guess from your list.