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):
|
def load_dict(dict_file):
|
||||||
try :
|
try:
|
||||||
with open(dict_file, "r") as dico:
|
with open(dict_file, "r") as dico:
|
||||||
liste = json.load(dico)
|
liste = json.load(dico)
|
||||||
return liste
|
return liste
|
||||||
except:
|
except Exception as error:
|
||||||
print("Impossible d'ouvrir le fichier" + fp)
|
print(f"Impossible d'ouvrir le fichier {dict_file} : {error}")
|
||||||
exit(2)
|
exit(2)
|
||||||
|
|
||||||
|
|
||||||
@ -20,13 +20,14 @@ def noDouble(mots):
|
|||||||
ndList = []
|
ndList = []
|
||||||
for mot in mots:
|
for mot in mots:
|
||||||
llist = []
|
llist = []
|
||||||
for l in mot:
|
for lettre in mot:
|
||||||
if not l in llist:
|
if lettre not in llist:
|
||||||
llist.append(l)
|
llist.append(lettre)
|
||||||
if len(mot) == len(llist):
|
if len(mot) == len(llist):
|
||||||
ndList.append(mot)
|
ndList.append(mot)
|
||||||
return(ndList)
|
return(ndList)
|
||||||
|
|
||||||
|
|
||||||
def firstTryFilter(mots, max_mots):
|
def firstTryFilter(mots, max_mots):
|
||||||
'''
|
'''
|
||||||
First Try : sélectionne des mots ayant des chances de valider ou invalider
|
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]
|
bestWords = bestWords[0:max_mots]
|
||||||
return(bestWords)
|
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):
|
def resolv_fl(liste, firstletter):
|
||||||
# On ne garde que les mots qui commence par 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]
|
return [mot for mot in liste if len(mot) == lenght]
|
||||||
|
|
||||||
|
|
||||||
# First Try
|
def resolv_pattern(liste, pattern):
|
||||||
#if ft:
|
# Pattern : élimination des mots ne satisfaisant pas la pattern
|
||||||
# bestWords = noDouble(firstTryFilter(goodLen, ft))
|
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)
|
def resolv_kl(liste, lettres):
|
||||||
#if bl:
|
'''
|
||||||
# if klFilter:
|
Known Letters : élimination des mots ne comprenant pas
|
||||||
# mots = klFilter
|
les lettres validées (hors pattern)
|
||||||
# elif patternFilter:
|
'''
|
||||||
# mots = patternFilter
|
lettres = list(set(list(lettres)))
|
||||||
# else:
|
MotsRestants = []
|
||||||
# mots = goodLen
|
for mot in liste:
|
||||||
# for mot in mots:
|
if len(set(lettres).intersection(mot)) == len(lettres):
|
||||||
# invalidate = 0
|
MotsRestants.append(mot)
|
||||||
# for l in bl:
|
return MotsRestants
|
||||||
# if l in mot and l in pattern:
|
|
||||||
# if pattern.count(l) != mot.count(l):
|
|
||||||
# invalidate += 1
|
def resolv_bl(liste, lettres):
|
||||||
# elif l in mot:
|
'''
|
||||||
# invalidate += 1
|
Bad Letters : élimination des mots contenant
|
||||||
# if invalidate == 0:
|
des lettres invalidées (hors pattern)
|
||||||
# blFilter.append(mot)
|
'''
|
||||||
|
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__":
|
if __name__ == "__main__":
|
||||||
@ -177,19 +164,10 @@ if __name__ == "__main__":
|
|||||||
)
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
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é !")
|
print("Tu as merdé ta pattern petit scarabé !")
|
||||||
exit(2)
|
exit(2)
|
||||||
|
|
||||||
# Known Letters
|
|
||||||
kl = args.knownLetters
|
|
||||||
|
|
||||||
# Bad Letters
|
|
||||||
bl = args.badLetters
|
|
||||||
|
|
||||||
# First Try
|
|
||||||
ft = args.firstTry
|
|
||||||
|
|
||||||
liste = load_dict(args.dictionary)
|
liste = load_dict(args.dictionary)
|
||||||
liste = resolv_len(liste, args.nbLetters)
|
liste = resolv_len(liste, args.nbLetters)
|
||||||
@ -205,6 +183,12 @@ if __name__ == "__main__":
|
|||||||
else:
|
else:
|
||||||
print("Hmmm... Je crois qu'on s'est perdu...")
|
print("Hmmm... Je crois qu'on s'est perdu...")
|
||||||
exit(0)
|
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:
|
if len(liste) > 1:
|
||||||
print(liste)
|
print(liste)
|
||||||
print(f"Il y a {len(liste)} mots dans la liste")
|
print(f"Il y a {len(liste)} mots dans la liste")
|
||||||
|
Loading…
Reference in New Issue
Block a user