initial upload

This commit is contained in:
Brunus 2022-01-24 10:49:07 +00:00
parent 2f1029d4bb
commit dc340509f3

203
sutom.py Normal file
View File

@ -0,0 +1,203 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import json
import argparse
parser = argparse.ArgumentParser(
description='Cheating with SUTOM'
)
parser.add_argument(
'-d',
'--dictionary',
help='Dictionnaire des mots du jeu'
)
parser.add_argument(
'-fl',
'--firstLetter',
help='Première lettre'
)
parser.add_argument(
'-nb',
'--nbLetters',
type=int,
help='Nombre de lettres, un entier (default: %(default)s)',
default=5
)
parser.add_argument(
'-kl',
'--knownLetters',
help='Lettres connues, hors première, sans espaces : -kl BE'
)
parser.add_argument(
'-bl',
'--badLetters',
help='Lettres non valides, sans espaces : -bl AKL'
)
parser.add_argument(
'-p',
'--pattern',
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",
action="store_true"
)
parser.add_argument(
'-ft',
'--firstTry',
type=int,
help="Sélectionne des mots commençants par firstLetter et composés d'un maximum de voyelles lettres différentes: -ft 5",
)
if not len(sys.argv) > 1:
print("-h or --help for synthaxe and options")
exit(1)
args = parser.parse_args()
# First Letter
if args.firstLetter:
fl = args.firstLetter
else:
fl = False
# Nbr Letters
nb = args.nbLetters
pattern = False
# Pattern
if args.pattern:
pattern = args.pattern
if args.nbLetters and len(pattern) != nb:
print("Tu as merdé ta pattern petit scarabé !")
exit(2)
if args.knownLetters:
kl = args.knownLetters
else:
kl = False
if args.badLetters:
bl = args.badLetters
else:
bl = False
if args.noDoubleLetters:
nd = True
else: nd = False
if args.firstTry:
ft = args.firstTry
else: ft = False
fp = args.dictionary
f = open(fp)
liste = json.load(f)
noDoubleLetters = []
patternFilter = []
klFilter = []
blFilter = []
ftFilter = []
# Fonction d'élimination de mots contenant plusieurs occurences d'une ou plusieurs 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)
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)
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)
# Suppression de tous les mots ne commençants pas par FirstLetter
if fl:
goodFl = [mot for mot in liste if mot.startswith(fl)]
# Suppression 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]
if ft:
bestWords = noDouble(firstTryFilter(goodLen, ft))
print(bestWords)
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)
# print(len(patternFilter))
# print(patternFilter)
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)
# print(len(klFilter))
# print(klFilter)
if bl:
print(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:
invalidate += 1
if invalidate == 0:
blFilter.append(mot)
print(len(blFilter))
print(blFilter)