Compare commits
8 Commits
78586d42f0
...
cb17864265
Author | SHA1 | Date | |
---|---|---|---|
cb17864265 | |||
0a8c29de74 | |||
8b9175454b | |||
ebea271d49 | |||
90aab55b75 | |||
1563e1bf1d | |||
5e60b32352 | |||
ba5600e65f |
330
wgamesolv.py
330
wgamesolv.py
@ -4,103 +4,19 @@
|
||||
import json
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Cheating with SUTOM'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-d',
|
||||
'--dictionary',
|
||||
help='Dictionnaire des mots du jeu',
|
||||
required=True
|
||||
)
|
||||
parser.add_argument(
|
||||
'-fl',
|
||||
'--firstLetter',
|
||||
help='Première lettre',
|
||||
default=False,
|
||||
required=True
|
||||
)
|
||||
parser.add_argument(
|
||||
'-nb',
|
||||
'--nbLetters',
|
||||
type=int,
|
||||
help='Nombre de lettres, un entier',
|
||||
required=True
|
||||
)
|
||||
parser.add_argument(
|
||||
'-kl',
|
||||
'--knownLetters',
|
||||
default=False,
|
||||
help='Lettres connues, hors première, sans espaces : -kl BE'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-bl',
|
||||
'--badLetters',
|
||||
default=False,
|
||||
help='Lettres non valides, sans espaces : -bl AKL'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-p',
|
||||
'--pattern',
|
||||
default=False,
|
||||
help='Placement, avec des "." pour les inconnues : -p A...T.I.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-nd',
|
||||
'--noDoubleLetters',
|
||||
help="Option pour ne chercher que des mots ne contenant qu'une occurence de chaque lettre",
|
||||
default=False,
|
||||
action="store_true"
|
||||
)
|
||||
parser.add_argument(
|
||||
'-ft',
|
||||
'--firstTry',
|
||||
default=False,
|
||||
type=int,
|
||||
help="Sélectionne des mots commençants par firstLetter et composés de ft voyelles différentes: -ft 4",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Dictionary
|
||||
if args.dictionary:
|
||||
def load_dict(dict_file):
|
||||
try :
|
||||
with open(args.dictionary, "r") as dico:
|
||||
with open(dict_file, "r") as dico:
|
||||
liste = json.load(dico)
|
||||
return liste
|
||||
except:
|
||||
print("Impossible d'ouvrir le fichier" + fp)
|
||||
exit(2)
|
||||
|
||||
# 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:
|
||||
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
|
||||
|
||||
patternFilter = []
|
||||
klFilter = []
|
||||
blFilter = []
|
||||
ftFilter = []
|
||||
|
||||
# Fonction d'élimination de mots contenant plusieurs occurences de lettres
|
||||
def noDouble(mots):
|
||||
# Fonction d'élimination de mots contenant plusieurs occurences de lettres
|
||||
ndList = []
|
||||
for mot in mots:
|
||||
llist = []
|
||||
@ -111,17 +27,20 @@ def noDouble(mots):
|
||||
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']
|
||||
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:
|
||||
vCount = 0
|
||||
for l in mot:
|
||||
if l in vLetters:
|
||||
vCount +=1
|
||||
if vCount >= ft:
|
||||
bestWords.append(mot)
|
||||
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
|
||||
@ -140,83 +59,156 @@ def patternCheck(pattern, mot, l):
|
||||
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)]
|
||||
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]
|
||||
|
||||
# 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]
|
||||
else:
|
||||
goodLen = [mot for mot in liste if len(mot) == nb]
|
||||
|
||||
# First Try
|
||||
if ft:
|
||||
bestWords = noDouble(firstTryFilter(goodLen, ft))
|
||||
#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)
|
||||
|
||||
#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)
|
||||
#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 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" )
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Cheating with SUTOM'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-d',
|
||||
'--dictionary',
|
||||
help='Dictionnaire des mots du jeu',
|
||||
required=True
|
||||
)
|
||||
parser.add_argument(
|
||||
'-fl',
|
||||
'--firstLetter',
|
||||
help='Première lettre',
|
||||
default=False,
|
||||
required=True
|
||||
)
|
||||
parser.add_argument(
|
||||
'-nb',
|
||||
'--nbLetters',
|
||||
type=int,
|
||||
help='Nombre de lettres, un entier',
|
||||
default=False,
|
||||
required=True
|
||||
)
|
||||
parser.add_argument(
|
||||
'-kl',
|
||||
'--knownLetters',
|
||||
default=False,
|
||||
help='Lettres connues, hors première, sans espaces : -kl BE'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-bl',
|
||||
'--badLetters',
|
||||
default=False,
|
||||
help='Lettres non valides, sans espaces : -bl AKL'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-p',
|
||||
'--pattern',
|
||||
default=False,
|
||||
help='Placement, avec des "." pour les inconnues : -p A...T.I.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-nd',
|
||||
'--noDoubleLetters',
|
||||
help="Option pour ne chercher que des mots ne contenant qu'une occurence de chaque lettre",
|
||||
default=False,
|
||||
action="store_true"
|
||||
)
|
||||
parser.add_argument(
|
||||
'-ft',
|
||||
'--firstTry',
|
||||
default=False,
|
||||
type=int,
|
||||
help="Sélectionne des mots commençants par firstLetter et composés de ft voyelles différentes: -ft 4",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.pattern and args.nbLetters and len(pattern) != nb:
|
||||
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)
|
||||
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:
|
||||
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:
|
||||
print("Hmmm... Je crois qu'on s'est perdu...")
|
||||
|
Loading…
Reference in New Issue
Block a user