permutations

This commit is contained in:
Adrien MALINGREY 2025-05-03 14:14:56 +02:00
parent 4a19252a94
commit 91db11cc2d
2 changed files with 23 additions and 9 deletions

View File

@ -25,7 +25,7 @@ class Grille implements Iterator, ArrayAccess {
$this->lettres_suivantes = [];
foreach ($hauteur == $largeur ? [$hauteur] : [$hauteur, $largeur] as $longueur) {
$this->lettres_suivantes[$longueur] = [];
foreach (mots_espaces($longueur, $hauteur == $largeur ? MAX_MOTS : MAX_MOTS/2) as $mots) {
foreach (mots_permutes($longueur) as $mots) {
$mot = implode(" ", $mots);
$ref = &$this->lettres_suivantes[$longueur];
for ($i = 0; $i < $longueur; $i++) {

View File

@ -3,9 +3,8 @@
const MIN_LETTRES_MOT_1 = 2;
const MIN_LETTRES_MOT_2 = 1;
const MAX_MOTS = 1000000;
$dico = [[]];
$dico = [[""]];
if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
$header = fgetcsv($lecteur, 0, "\t");
while (($ligne = fgetcsv($lecteur, 0, "\t")) !== FALSE) {
@ -32,24 +31,39 @@ if (($lecteur = fopen("dico.csv", "r")) !== FALSE) {
fclose($lecteur);
}
function mots_espaces($longueur, $nb_mots_restants=MAX_MOTS)
function mots_espaces($longueur)
{
global $dico;
foreach ($dico[$longueur] as $mot => $definition) {
yield [$mot];
if (--$nb_mots_restants <= 0) return;
}
for ($i = MIN_LETTRES_MOT_1; ($j = $longueur - $i - 1) >= MIN_LETTRES_MOT_2; $i++) {
foreach ($dico[$i] as $mot => $definition) {
foreach (mots_espaces($j, $nb_mots_restants) as $mots) {
foreach (mots_espaces($j) as $mots) {
if (!in_array($mot, $mots)) {
yield [$mot, ...$mots];
if (--$nb_mots_restants <= 0) return;
yield [...$mots, $mot];
if (--$nb_mots_restants <= 0) return;
}
}
}
}
}
function permutations(array $elements)
{
if (count($elements) <= 1) {
yield $elements;
} else {
foreach (permutations(array_slice($elements, 1)) as $permutation) {
foreach (range(0, count($elements) - 1) as $i) {
yield [...array_slice($permutation, 0, $i), $elements[0], ...array_slice($permutation, $i)];
}
}
}
}
function mots_permutes($longueur) {
foreach (mots_espaces($longueur) as $mots) {
yield from permutations($mots);
}
}