Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.8 kB
2
Indexable
Never
# Copyright (c) 2022 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com
import random
from typing import List, Dict
from collections import Counter

from src.common.game import ResultElement


def get_best_guess(possible_words: List[str], freq_dict: Dict[str, int]) -> str:
    max_freq_word, max_freq = '', 0
    for word in possible_words:
        if word in freq_dict:
            if freq_dict[word] > max_freq:
                max_freq_word = word
                max_freq = freq_dict[word]
    # if max_freq_word == '':
    #     max_freq_word = random.choice(possible_words)
    return max_freq_word


def guess_flag(guess, result):
    guess_flag_freq = {}
    for n, element in enumerate(guess):
        if element not in guess_flag_freq:
            guess_flag_freq[element] = [result[n]]
        else:
            if result[n] not in guess_flag_freq[element]:
                guess_flag_freq[element].append(result[n])
    return guess_flag_freq


def apply_guess(current_words: List[str], guess: str, result: List[ResultElement]) -> List[str]:
# guess - stunt
# word - fatsy
# goal - towel
# result - [b, y, b, b, b]

# t -> [y,y,b]
# freq of t in guess -> 3
# number of t with black assign -> 1

# if not ((guess[n] not in word) or (guess_freq[guess[n]] > word_freq[guess[n]])):
#     return False

    letter_black_freq = {}
    guess_freq = Counter(guess)
    for n, element in enumerate(result):
        if element == ResultElement.black:
            if guess[n] in letter_black_freq:
                letter_black_freq[guess[n]] = letter_black_freq[guess[n]] + 1
            else:
                letter_black_freq[guess[n]] = 1
    # print(letter_black_freq)
    def is_possible(word: str) -> bool:
        word_freq = Counter(word)
        for n, element in enumerate(result):
            if element == ResultElement.green:
                if word[n] != guess[n]:
                    return False
            elif element == ResultElement.yellow:
                if not (guess[n] in word_freq and word[n] != guess[n]):
                    return False
            else:
                # guess - stunt
                # word - fatty
                # goal - towel
                # result - [b, y, b, b, b]
                if not (guess_freq[guess[n]] == word_freq[guess[n]] + letter_black_freq[guess[n]]):
                    # guess_freq[guess[n]] > word_freq[guess[n]]

                    # a = b + c , c > 0
                    # a > b
                    return False
        return True
    return [w for w in current_words if is_possible(w) and w != '']
Leave a Comment