Compare commits
5 Commits
cb17864265
...
fc93178303
Author | SHA1 | Date | |
---|---|---|---|
fc93178303 | |||
e4b517168a | |||
e2452c1434 | |||
f9cf15d487 | |||
758d6bb0bb |
138
wgamesolv.py
138
wgamesolv.py
@ -6,12 +6,12 @@ import argparse
|
||||
|
||||
|
||||
def load_dict(dict_file):
|
||||
try :
|
||||
try:
|
||||
with open(dict_file, "r") as dico:
|
||||
liste = json.load(dico)
|
||||
return liste
|
||||
except:
|
||||
print("Impossible d'ouvrir le fichier" + fp)
|
||||
except Exception as error:
|
||||
print(f"Impossible d'ouvrir le fichier {dict_file} : {error}")
|
||||
exit(2)
|
||||
|
||||
|
||||
@ -20,13 +20,14 @@ def noDouble(mots):
|
||||
ndList = []
|
||||
for mot in mots:
|
||||
llist = []
|
||||
for l in mot:
|
||||
if not l in llist:
|
||||
llist.append(l)
|
||||
for lettre in mot:
|
||||
if lettre not in llist:
|
||||
llist.append(lettre)
|
||||
if len(mot) == len(llist):
|
||||
ndList.append(mot)
|
||||
return(ndList)
|
||||
|
||||
|
||||
def firstTryFilter(mots, max_mots):
|
||||
'''
|
||||
First Try : sélectionne des mots ayant des chances de valider ou invalider
|
||||
@ -43,21 +44,6 @@ def firstTryFilter(mots, max_mots):
|
||||
bestWords = bestWords[0:max_mots]
|
||||
return(bestWords)
|
||||
|
||||
# Fonction de comparaison des lettres d'un mot, avec celle de la pattern
|
||||
# Plus nécessaire actuellement mais peut être dans une prochaine vie
|
||||
'''
|
||||
def patternCheck(pattern, mot, l):
|
||||
goodPosCounter = 0
|
||||
for pos in range(len(pattern)):
|
||||
print("Checking : " + l + "against : " + pattern[pos] + " and " + mot[pos])
|
||||
if pattern[pos] == l and mot[pos] == l:
|
||||
goodPosCounter +=1
|
||||
print("checked")
|
||||
if goodPosCounter == pattern.count(l):
|
||||
return(0)
|
||||
else:
|
||||
return(1)
|
||||
'''
|
||||
|
||||
def resolv_fl(liste, firstletter):
|
||||
# On ne garde que les mots qui commence par FirstLetter
|
||||
@ -69,53 +55,54 @@ def resolv_len(liste, lenght):
|
||||
return [mot for mot in liste if len(mot) == lenght]
|
||||
|
||||
|
||||
# First Try
|
||||
#if ft:
|
||||
# bestWords = noDouble(firstTryFilter(goodLen, ft))
|
||||
def resolv_pattern(liste, pattern):
|
||||
# Pattern : élimination des mots ne satisfaisant pas la pattern
|
||||
pattern_dict = {}
|
||||
for lettre in pattern:
|
||||
if lettre != ".":
|
||||
pattern_dict.update({pattern.index(lettre): lettre})
|
||||
MotsOK, MotsKO = [], []
|
||||
for mot in liste:
|
||||
for key in pattern_dict:
|
||||
if mot[key] == pattern_dict[key]:
|
||||
MotsOK.append(mot)
|
||||
else:
|
||||
MotsKO.append(mot)
|
||||
liste = [mot for mot in MotsOK if mot not in MotsKO]
|
||||
liste = list(set(liste))
|
||||
return liste
|
||||
|
||||
# Pattern : élimination des mots ne satisfaisant pas la pattern
|
||||
#if pattern:
|
||||
# patternCount = len(pattern) - pattern.count(".")
|
||||
# for mot in goodLen:
|
||||
# patternEval = 0
|
||||
# for l in range(len(mot)):
|
||||
# if mot[l] == pattern[l]:
|
||||
# patternEval += 1
|
||||
# if patternEval == patternCount :
|
||||
# patternFilter.append(mot)
|
||||
#
|
||||
# Known Letters : élimination des mots ne comprenant pas les lettres validées (hors pattern)
|
||||
#if kl:
|
||||
# if patternFilter:
|
||||
# mots = patternFilter
|
||||
# else:
|
||||
# mots = goodLen
|
||||
# for mot in mots:
|
||||
# validate = 0
|
||||
# for l in kl:
|
||||
# if l in mot:
|
||||
# validate += 1
|
||||
# if validate == len(kl):
|
||||
# klFilter.append(mot)
|
||||
|
||||
# Bad Letters : élimination des mots contenant des lettres invalidées (hors pattern)
|
||||
#if bl:
|
||||
# if klFilter:
|
||||
# mots = klFilter
|
||||
# elif patternFilter:
|
||||
# mots = patternFilter
|
||||
# else:
|
||||
# mots = goodLen
|
||||
# for mot in mots:
|
||||
# invalidate = 0
|
||||
# for l in bl:
|
||||
# if l in mot and l in pattern:
|
||||
# if pattern.count(l) != mot.count(l):
|
||||
# invalidate += 1
|
||||
# elif l in mot:
|
||||
# invalidate += 1
|
||||
# if invalidate == 0:
|
||||
# blFilter.append(mot)
|
||||
def resolv_kl(liste, lettres):
|
||||
'''
|
||||
Known Letters : élimination des mots ne comprenant pas
|
||||
les lettres validées (hors pattern)
|
||||
'''
|
||||
lettres = list(set(list(lettres)))
|
||||
MotsRestants = []
|
||||
for mot in liste:
|
||||
if len(set(lettres).intersection(mot)) == len(lettres):
|
||||
MotsRestants.append(mot)
|
||||
return MotsRestants
|
||||
|
||||
|
||||
def resolv_bl(liste, lettres):
|
||||
'''
|
||||
Bad Letters : élimination des mots contenant
|
||||
des lettres invalidées (hors pattern)
|
||||
'''
|
||||
lettres = list(set(list(lettres)))
|
||||
MotsSuppr = []
|
||||
for mot in liste:
|
||||
for lettre in lettres:
|
||||
if lettre in mot:
|
||||
MotsSuppr.append(mot)
|
||||
for mot in MotsSuppr:
|
||||
try:
|
||||
liste.pop(liste.index(mot))
|
||||
except ValueError:
|
||||
pass
|
||||
return liste
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -177,19 +164,10 @@ if __name__ == "__main__":
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.pattern and args.nbLetters and len(pattern) != nb:
|
||||
|
||||
if args.pattern and args.nbLetters and len(args.pattern) != args.nbLetters:
|
||||
print("Tu as merdé ta pattern petit scarabé !")
|
||||
exit(2)
|
||||
|
||||
# Known Letters
|
||||
kl = args.knownLetters
|
||||
|
||||
# Bad Letters
|
||||
bl = args.badLetters
|
||||
|
||||
# First Try
|
||||
ft = args.firstTry
|
||||
|
||||
liste = load_dict(args.dictionary)
|
||||
liste = resolv_len(liste, args.nbLetters)
|
||||
@ -205,6 +183,12 @@ if __name__ == "__main__":
|
||||
else:
|
||||
print("Hmmm... Je crois qu'on s'est perdu...")
|
||||
exit(0)
|
||||
if args.knownLetters:
|
||||
liste = resolv_kl(liste, args.knownLetters)
|
||||
if args.badLetters:
|
||||
liste = resolv_bl(liste, args.badLetters)
|
||||
if args.pattern:
|
||||
liste = resolv_pattern(liste, args.pattern)
|
||||
if len(liste) > 1:
|
||||
print(liste)
|
||||
print(f"Il y a {len(liste)} mots dans la liste")
|
||||
|
Loading…
Reference in New Issue
Block a user