Compare commits
8 Commits
78586d42f0
...
cb17864265
Author | SHA1 | Date | |
---|---|---|---|
cb17864265 | |||
0a8c29de74 | |||
8b9175454b | |||
ebea271d49 | |||
90aab55b75 | |||
1563e1bf1d | |||
5e60b32352 | |||
ba5600e65f |
280
wgamesolv.py
280
wgamesolv.py
@ -4,6 +4,121 @@
|
||||
import json
|
||||
import argparse
|
||||
|
||||
|
||||
def load_dict(dict_file):
|
||||
try :
|
||||
with open(dict_file, "r") as dico:
|
||||
liste = json.load(dico)
|
||||
return liste
|
||||
except:
|
||||
print("Impossible d'ouvrir le fichier" + fp)
|
||||
exit(2)
|
||||
|
||||
|
||||
def noDouble(mots):
|
||||
# Fonction d'élimination de mots contenant plusieurs occurences de lettres
|
||||
ndList = []
|
||||
for mot in mots:
|
||||
llist = []
|
||||
for l in mot:
|
||||
if not l in llist:
|
||||
llist.append(l)
|
||||
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
|
||||
l'une des lettres les plus utilisée en français
|
||||
'''
|
||||
bestLetters = ['E', 'A', 'I', 'R', 'S', 'N', 'T', 'O', 'L', 'U']
|
||||
bestWords = []
|
||||
for mot in mots:
|
||||
for bestLetter in bestLetters:
|
||||
if bestLetter in mot:
|
||||
bestWords.append(mot)
|
||||
bestWords = list(set(bestWords))
|
||||
bestWords = noDouble(bestWords) # Autant maximiser les chances
|
||||
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
|
||||
return [mot for mot in liste if mot[0] == firstletter]
|
||||
|
||||
|
||||
def resolv_len(liste, lenght):
|
||||
# On ne garde que les mots qui ont la bonne longueur
|
||||
return [mot for mot in liste if len(mot) == lenght]
|
||||
|
||||
|
||||
# First Try
|
||||
#if ft:
|
||||
# bestWords = noDouble(firstTryFilter(goodLen, ft))
|
||||
|
||||
# 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)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Cheating with SUTOM'
|
||||
)
|
||||
@ -25,6 +140,7 @@ parser.add_argument(
|
||||
'--nbLetters',
|
||||
type=int,
|
||||
help='Nombre de lettres, un entier',
|
||||
default=False,
|
||||
required=True
|
||||
)
|
||||
parser.add_argument(
|
||||
@ -62,26 +178,7 @@ parser.add_argument(
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Dictionary
|
||||
if args.dictionary:
|
||||
try :
|
||||
with open(args.dictionary, "r") as dico:
|
||||
liste = json.load(dico)
|
||||
except:
|
||||
print("Impossible d'ouvrir le fichier" + fp)
|
||||
|
||||
# First letter
|
||||
fl = args.firstLetter
|
||||
|
||||
# Nbr Letters
|
||||
if args.firstLetter:
|
||||
nb = args.nbLetters
|
||||
else:
|
||||
nbLetters = False
|
||||
|
||||
# Pattern
|
||||
pattern = args.pattern
|
||||
if pattern and args.nbLetters and len(pattern) != nb:
|
||||
if args.pattern and args.nbLetters and len(pattern) != nb:
|
||||
print("Tu as merdé ta pattern petit scarabé !")
|
||||
exit(2)
|
||||
|
||||
@ -94,129 +191,24 @@ bl = args.badLetters
|
||||
# First Try
|
||||
ft = args.firstTry
|
||||
|
||||
patternFilter = []
|
||||
klFilter = []
|
||||
blFilter = []
|
||||
ftFilter = []
|
||||
|
||||
# Fonction d'élimination de mots contenant plusieurs occurences de lettres
|
||||
def noDouble(mots):
|
||||
ndList = []
|
||||
for mot in mots:
|
||||
llist = []
|
||||
for l in mot:
|
||||
if not l in llist:
|
||||
llist.append(l)
|
||||
if len(mot) == len(llist):
|
||||
ndList.append(mot)
|
||||
return(ndList)
|
||||
|
||||
# First Try : sélectionne des mots ayant des chances de valider ou invalider des voyelles
|
||||
def firstTryFilter(mots, ft):
|
||||
vLetters = ['A', 'E', 'I', 'O', 'U', 'Y']
|
||||
bestWords = []
|
||||
for mot in mots:
|
||||
vCount = 0
|
||||
for l in mot:
|
||||
if l in vLetters:
|
||||
vCount +=1
|
||||
if vCount >= ft:
|
||||
bestWords.append(mot)
|
||||
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)
|
||||
liste = load_dict(args.dictionary)
|
||||
liste = resolv_len(liste, args.nbLetters)
|
||||
liste = resolv_fl(liste, args.firstLetter)
|
||||
if args.noDoubleLetters:
|
||||
liste = noDouble(liste)
|
||||
if args.firstTry:
|
||||
liste = firstTryFilter(liste, args.firstTry)
|
||||
if len(liste) == 1:
|
||||
print(f"Essaie : \"{liste[0]}\"")
|
||||
elif len(liste) > 1:
|
||||
print(f"Mots à tester : {liste}")
|
||||
else:
|
||||
return(1)
|
||||
'''
|
||||
|
||||
# First Letter : élimination des mots ne commençant pas par FirstLetter
|
||||
if fl:
|
||||
goodFl = [mot for mot in liste if mot.startswith(fl)]
|
||||
|
||||
# Nb Lettres : élimination des mots n'ayant pas le bon nombre de lettres
|
||||
if fl:
|
||||
goodLen = [mot for mot in goodFl if len(mot) == nb]
|
||||
print("Hmmm... Je crois qu'on s'est perdu...")
|
||||
exit(0)
|
||||
if len(liste) > 1:
|
||||
print(liste)
|
||||
print(f"Il y a {len(liste)} mots dans la liste")
|
||||
elif len(liste) == 1:
|
||||
print(f"La solution est \"{liste[0]}\".")
|
||||
else:
|
||||
goodLen = [mot for mot in liste if len(mot) == nb]
|
||||
|
||||
# First Try
|
||||
if ft:
|
||||
bestWords = noDouble(firstTryFilter(goodLen, ft))
|
||||
|
||||
# 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)
|
||||
|
||||
# Affiche du résultat
|
||||
if pattern and kl and bl:
|
||||
print(", ".join(blFilter))
|
||||
print(str(len(blFilter)) + " mots dans la liste" )
|
||||
elif pattern and kl:
|
||||
print(", ".join(klFilter))
|
||||
print(str(len(klFilter)) + " mots dans la liste" )
|
||||
elif pattern:
|
||||
print(", ".join(patternFilter))
|
||||
print(str(len(patternFilter)) + " mots dans la liste" )
|
||||
elif kl and bl:
|
||||
print(", ".join(blFilter))
|
||||
print(str(len(blFilter)) + " mots dans la liste" )
|
||||
elif bl and pattern:
|
||||
print(", ".join(blFilter))
|
||||
print(str(len(blFilter)) + " mots dans la liste" )
|
||||
elif ft:
|
||||
print(", ".join(bestWords))
|
||||
print(str(len(bestWords)) + " mots dans la liste" )
|
||||
else:
|
||||
print(", ".join(goodLen))
|
||||
print(str(len(goodLen)) + " mots dans la liste" )
|
||||
print("Hmmm... Je crois qu'on s'est perdu...")
|
||||
|
Loading…
Reference in New Issue
Block a user