ただのメモ

他の人に見せても良い方のメモ

numeron

import math
"""
https://y.honkakuha.success-games.net/game/su-115-hitandblow/
"""

N = int(input("what is N?"))
number = [i for i in range(10)]
#number = [i for i in range(26)]
#alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

def nlp(x):
    integer_string =
    for i in range(len(x)):
        integer_string.append(number[alphabet.index(x[i])])

    return integer_string

def inv_nlp(x):
    str_string =
    for i in range(len(x)):
        str_string.append(alphabet[number.index(x[i])])

    return str_string


def making_numeron(N):
    temp = [[i] for i in range(len(number))]
    for i in range(N-1):
        next_temp =
        for j in range(len(number)):
            for k in range(len(temp)):
                if j not in temp[k]:
                    next_temp.append(temp[k] + [j])

        temp = next_temp
    return temp


numeron = making_numeron(N)
#print(len(numeron))

def check(a,b,c,d):
    eat = 0
    bite = 0
    #a equal to candidate number, b equal to guess
    for i in range(N):
        if a[i] in b:
            if a[i] == b[i]:
                eat += 1
            else:
                bite += 1
    if c == eat and d == bite:
        return 1
    else:
        return 0

 

def candidate(temp, b, c, d):
    next =
    for i in temp:
        if check(i, b, c, d):
            next.append(i)

    return next
    #calculate probable candidate

 

def hint(a,b):
    eat = 0
    bite = 0
    for i in range(N):
        if a[i] in b:
            if a[i] == b[i]:
                eat += 1
            else:
                bite += 1
    return (eat, bite)

def predict(temp):
    #calculate least damage solution
    #temp equal to set of possible solutions
    minimum = 10**8
    get_answer = 0
    predicted_answer = [-1] * N
    #answer = [0] * (N ** 2)
    #print(len(temp))

    for i in numeron:
        #if get_answer == 1:
        #    break
        if i in temp:
            priority = 1
        else:
            priority = 0

        answer = [0] * (N ** 2+ 2 * N+1)

        for j in temp:
            hint_eat, hint_bite = hint(i,j)
            #print("ok")
            #if hint_eat == N:
            #    get_answer = 1
            #    true_answer = j
            #    break
            #else:
            answer[N*hint_eat + hint_bite] = answer[N*hint_eat + hint_bite]+ 1
        else:
            if sum(answer) == 1:
                if answer[N*N] == 1:
                    get_answer = 1
                    true_answer = i
        if minimum > max(answer):
            predicted_answer = i
            #print(answer,get_answer,i,)
            minimum = min(minimum, max(answer))
        elif minimum == max(answer) - priority:
            if priority == 1:
                predicted_answer = i

    #print(max(answer))
    if get_answer == 1:
        return true_answer
        pass
    else:
        return predicted_answer

 

def main():
    now = numeron
    print(len(now),"start")
    guess = number[:N]
    while True:
        #q = input("game continue? (y/n):")
        #if q == "y":
        #    pass
        #else:
        #    print("see you again!")
        #    break

        #guess_str = list(str(input("what is your guess?")))
        #guess = nlp(guess_str)
        #guess = [int(element) for element in guess_integers]

        try:
            eat_now = int(input("how many eat?"))
            bite_now = int(input("how many bite?"))
        except:
            print("See you!")
            break
        """
        try:
            eat_now = nlp(input("how many eat?"))
            bite_now = nlp(input("how many bite?"))
        except:
            print("See you!")
            break
        """
        if eat_now == N:
            print("Clear!")
            break
        now = candidate(now, guess, eat_now, bite_now)
        #print(len(now),now)

        prediction = predict(now)
        guess = prediction
        print(f"guess is {prediction}")
        print(f"nubmer of candidate answer :{len(now)}")
        #print(inv_nlp(prediction))

main()