on commence par le début

This commit is contained in:
Adrien MALINGREY 2025-04-24 04:10:25 +02:00
parent cb71fbe68a
commit b70b8cdc56
7 changed files with 630 additions and 242 deletions

139
Grille.php Normal file
View File

@ -0,0 +1,139 @@
<?php
include_once "dico.php";
const MIN_LETTRES = 1;
const MAX_ESSAIS = 10000;
class Grille {
public $grille;
private $grilles;
private $hauteur;
private $largeur;
private $mots_commencant_par;
private $mots_utilises = [];
public function __construct($hauteur, $largeur) {
$this->hauteur = $hauteur;
$this->largeur = $largeur;
$this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '.'));
if ($hauteur == $largeur) {
$dimensions = [$hauteur];
} else {
$dimensions = [$hauteur, $largeur];
}
$this->mots_commencant_par = [];
foreach ($dimensions as $dimension) {
$this->mots_commencant_par[$dimension] = [];
// $nb_mots = 1;
foreach(mots_espaces($dimension, MIN_LETTRES) as $mot) {
for ($i = 0; $i <= $dimension; $i++) {
$debut = substr($mot, 0, $i);
if (!isset($this->mots_commencant_par[$dimension][$debut])) {
$this->mots_commencant_par[$dimension][$debut] = [];
}
$this->mots_commencant_par[$dimension][$debut][] = $mot;
}
/* $nb_mots++;
if ($nb_mots > MAX_ESSAIS) {
break;
} */
}
}
$this->grilles = $this->generateur();
$this->grilles->current();
}
public function get_ligne($l, $max = 100) {
$ligne = "";
$min = min($this->largeur, $max);
for ($i = 0; $i < $min; $i++) {
$ligne .= $this->grille[$l][$i];
}
return $ligne;
}
public function set_ligne($l, $mot) {
for ($i = 0; $i < strlen($mot); $i++) {
$this->grille[$l][$i] = $mot[$i];
}
}
public function get_colonne($c, $max = 100) {
$colonne = "";
$min = min($this->hauteur, $max);
for ($i = 0; $i < $min; $i++) {
$colonne .= $this->grille[$i][$c];
}
return $colonne;
}
public function set_colonne($c, $mot) {
for ($i = 0; $i < strlen($mot); $i++) {
$this->grille[$i][$c] = $mot[$i];
}
}
public function generateur() {
yield from $this->trouve_une_ligne(0);
$this->grille = array_fill(0, $this->hauteur, array_fill(0, $this->largeur, ' '));
}
private function trouve_une_ligne($l) {
global $mots_de_n_lettres;
foreach ($this->mots_commencant_par[$this->largeur][$this->get_ligne($l, $l)] as $mot_lig) {
$this->set_ligne($l, $mot_lig);
$ok = true;
for ($c = $l; $c < $this->largeur; $c++) {
if (!isset($this->mots_commencant_par[$this->hauteur][$this->get_colonne($c, $l+1)])) {
$ok = false;
break;
}
}
if (!$ok) {
continue;
}
$this->mots_utilises[$mot_lig] = true;
if ($l < $this->largeur) {
yield from $this->trouve_une_colonne($l);
} else if ($l + 1 < $this->hauteur) {
yield from $this->trouve_une_ligne($l + 1);
} else {
yield $this;
}
unset($this->mots_utilises[$mot_lig]);
}
}
private function trouve_une_colonne($c) {
global $mots_de_n_lettres;
foreach ($this->mots_commencant_par[$this->hauteur][$this->get_colonne($c, $c + 1)] as $mot_col) {
if (isset($this->mots_utilises[$mot_col])) {
continue;
}
$this->set_colonne($c, $mot_col);
$ok = true;
for ($l = $c; $l < $this->hauteur; $l++) {
if (!isset($this->mots_commencant_par[$this->largeur][$this->get_ligne($l, $c+1)])) {
$ok = false;
break;
}
}
if (!$ok) {
continue;
}
$this->mots_utilises[$mot_col] = true;
if ($c +1 < $this->hauteur) {
yield from $this->trouve_une_ligne($c + 1);
} else if ($c + 1 < $this->largeur) {
yield from $this->trouve_une_colonne($c + 1);
} else {
yield $this;
}
unset($this->mots_utilises[$mot_col]);
}
}
}

44
dico.php Normal file
View File

@ -0,0 +1,44 @@
<?php
$dico = [];
if (($handle = fopen("dico.csv", "r")) !== FALSE) {
$header = fgetcsv($handle, 0, "\t");
while (($ligne = fgetcsv($handle, 0, "\t")) !== FALSE) {
if (count($ligne) >= 2) {
$mot = $ligne[0];
$definition = $ligne[1];
$dico[$mot] = $definition;
}
}
fclose($handle);
}
$mots_de_n_lettres = [[]];
foreach ($dico as $mot => $definition) {
$n = strlen($mot);
if (!isset($mots_de_n_lettres[$n])) {
$mots_de_n_lettres[$n] = [];
}
$mots_de_n_lettres[$n][] = $mot;
}
foreach ($mots_de_n_lettres as $n => $mots) {
shuffle($mots_de_n_lettres[$n]);
}
function mots_espaces($max, $min=0) {
global $mots_de_n_lettres;
foreach($mots_de_n_lettres[$max] as $mot) {
yield $mot;
}
for ($i = ceil($max / 2); $max - $i -1 >= $min; $i++) {
foreach ($mots_de_n_lettres[$i] as $mot1) {
foreach (mots_espaces($max - $i -1, $min) as $mot2) {
if ($mot1 != $mot2) {
yield "$mot1 $mot2";
yield "$mot2 $mot1";
}
}
}
}
}

216
index.php
View File

@ -3,217 +3,28 @@ ini_set('display_errors', 1);
ini_set('html_errors', 1); ini_set('html_errors', 1);
ini_set('error_reporting', E_ALL); ini_set('error_reporting', E_ALL);
$default_lignes = 3; include_once "dico.php";
$default_colonnes = 4; include_once "Grille.php";
const HAUTEUR_PAR_DEFAUT = 3;
const LARGEUR_PAR_DEFAUT = 4;
$hauteur = filter_input(INPUT_GET, 'lignes', FILTER_VALIDATE_INT, [ $hauteur = filter_input(INPUT_GET, 'lignes', FILTER_VALIDATE_INT, [
"options" => [ "options" => [
"default" => $default_lignes, "default" => HAUTEUR_PAR_DEFAUT,
"min_range" => 2, "min_range" => 2,
"max_range" => 10 "max_range" => 10
] ]
]); ]);
$largeur = filter_input(INPUT_GET, 'colonnes', FILTER_VALIDATE_INT, [ $largeur = filter_input(INPUT_GET, 'colonnes', FILTER_VALIDATE_INT, [
"options" => [ "options" => [
"default" => $default_colonnes, "default" => LARGEUR_PAR_DEFAUT,
"min_range" => 2, "min_range" => 2,
"max_range" => 10 "max_range" => 10
] ]
]); ]);
$dico = [];
if (($handle = fopen("dico.csv", "r")) !== FALSE) {
$header = fgetcsv($handle, 0, "\t");
while (($ligne = fgetcsv($handle, 0, "\t")) !== FALSE) {
if (count($ligne) >= 2) {
$mot = $ligne[0];
$definition = $ligne[1];
$dico[$mot] = $definition;
}
}
fclose($handle);
}
$mots_de_n_lettres = [];
foreach ($dico as $mot => $definition) {
$n = strlen($mot);
if (!isset($mots_de_n_lettres[$n])) {
$mots_de_n_lettres[$n] = [];
}
$mots_de_n_lettres[$n][] = $mot;
}
foreach ($mots_de_n_lettres as $n => $liste_mots) {
shuffle($mots_de_n_lettres[$n]);
}
$mots_par_position = [];
foreach ([$hauteur, $largeur] as $n) {
$mots_par_position[$n] = [];
foreach ($mots_de_n_lettres[$n] as $mot) {
foreach (str_split($mot) as $i => $lettre) {
if (!isset($mots_par_position[$n][$i])) {
$mots_par_position[$n][$i] = [];
}
if (!isset($mots_par_position[$n][$i][$lettre])) {
$mots_par_position[$n][$i][$lettre] = [];
}
$mots_par_position[$n][$i][$lettre][] = $mot;
}
}
}
function pire_contrainte($tests, $nb_lettres, $i, $mot)
{
global $mots_par_position;
$nb_mots_min = PHP_INT_MAX;
$pire_contrainte = 0;
foreach ($tests as $test) {
if (
!array_key_exists($i, $mots_par_position[$nb_lettres]) ||
!array_key_exists($mot[$test], $mots_par_position[$nb_lettres][$i])
) {
return -1;
} else {
$nb_mots = count($mots_par_position[$nb_lettres][$i][$mot[$test]]);
if ($nb_mots < $nb_mots_min) {
$pire_contrainte = $test;
$nb_mots_min = $nb_mots;
}
}
}
return $pire_contrainte;
}
class Grille
{
public $hauteur;
public $largeur;
public $grille;
public $lignes_restantes;
public $colonnes_restantes;
public function __construct($hauteur, $largeur)
{
$this->hauteur = $hauteur;
$this->largeur = $largeur;
$this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '.'));
$this->lignes_restantes = range(0, $hauteur - 1);
$this->colonnes_restantes = range(0, $largeur - 1);
}
public function get_ligne($l)
{
return implode("", $this->grille[$l]);
}
public function set_ligne($l, $mot)
{
for ($i = 0; $i < strlen($mot); $i++) {
$this->grille[$l][$i] = $mot[$i];
}
}
public function get_colonne($c)
{
$colonne = "";
for ($i = 0; $i < $this->hauteur; $i++) {
$colonne .= $this->grille[$i][$c];
}
return $colonne;
}
public function set_colonne($c, $mot)
{
for ($i = 0; $i < strlen($mot); $i++) {
$this->grille[$i][$c] = $mot[$i];
}
}
public function genere()
{
global $mots_de_n_lettres;
$l = $this->largeur / 2;
array_splice($this->lignes_restantes, $l, 1);
foreach ($mots_de_n_lettres[$this->largeur] as $mot_lig) {
$this->set_ligne($l, $mot_lig);
yield from $this->trouve_une_colonne($l, $mot_lig);
}
$this->lignes_restantes[] = $l;
$this->grille[$l] = array_fill(0, $this->largeur, '.');
}
public function trouve_une_colonne($l, $mot_lig)
{
global $mots_par_position;
$c = pire_contrainte($this->colonnes_restantes, $this->hauteur, $l, $mot_lig);
if ($c == -1) {
return;
}
$colonne = $this->get_colonne($c);
array_splice($this->colonnes_restantes, $c, 1);
foreach ($mots_par_position[$this->hauteur][$l][$mot_lig[$c]] as $mot_col) {
if ($mot_col == $colonne || preg_match("/^$colonne$/", $mot_col)) {
$this->set_colonne($c, $mot_col);
if (count($this->lignes_restantes)) {
yield from $this->trouve_une_ligne($c, $mot_col);
} else if (count($this->colonnes_restantes)) {
yield from $this->trouve_une_colonne($l, $mot_lig);
} else {
yield;
}
}
}
$this->colonnes_restantes[] = $c;
$this->set_colonne($c, $colonne);
}
public function trouve_une_ligne($c, $mot_col)
{
global $mots_par_position;
$l = pire_contrainte($this->lignes_restantes, $this->largeur, $c, $mot_col);
if ($l == -1) {
return;
}
$ligne = $this->get_ligne($l);
array_splice($this->lignes_restantes, $l, 1);
foreach ($mots_par_position[$this->largeur][$c][$mot_col[$l]] as $mot_lig) {
if ($mot_lig == $ligne || preg_match("/^$ligne$/", $mot_lig)) {
$this->set_ligne($l, $mot_lig);
if (count($this->colonnes_restantes)) {
yield from $this->trouve_une_colonne($l, $mot_lig);
} else if (count($this->lignes_restantes)) {
yield from $this->trouve_une_ligne($c, $mot_col);
} else {
yield;
}
}
}
$this->lignes_restantes[] = $l;
$this->set_ligne($l, $ligne);
}
public function affiche()
{
echo "<table>";
echo "<tr><th></th>";
for ($c = 0; $c < $this->largeur; $c++) {
echo "<th>" . chr($c + 65) . "</th>";
}
echo "</tr>";
for ($l = 0; $l < $this->hauteur; $l++) {
echo "<tr><th>" . $l . "</th>";
for ($c = 0; $c < $this->largeur; $c++) {
echo "<td>" . $this->grille[$l][$c] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}
$grille = new Grille($hauteur, $largeur); $grille = new Grille($hauteur, $largeur);
$grille->genere()->current();
?> ?>
<!DOCTYPE HTML> <!DOCTYPE HTML>
@ -227,17 +38,24 @@ $grille->genere()->current();
border-collapse: collapse; border-collapse: collapse;
} }
td { th, td {
width: 30px; width: 30px;
height: 30px; height: 30px;
text-align: center; text-align: center;
}
td {
border: 1px solid black; border: 1px solid black;
} }
.case.noire {
background-color: black;
}
</style> </style>
</head> </head>
<body> <body>
<table> <table class="grille">
<tr> <tr>
<th></th> <th></th>
<?php for ($c = 0; $c < $largeur; $c++): ?> <?php for ($c = 0; $c < $largeur; $c++): ?>
@ -248,7 +66,7 @@ $grille->genere()->current();
<tr> <tr>
<th><?= $l ?></th> <th><?= $l ?></th>
<?php for ($c = 0; $c < $largeur; $c++): ?> <?php for ($c = 0; $c < $largeur; $c++): ?>
<td><?= $grille->grille[$l][$c] ?></td> <td class="case <?= $grille->grille[$l][$c]==" "?"noire": "blanche" ?>"><?= $grille->grille[$l][$c] ?></td>
<?php endfor; ?> <?php endfor; ?>
</tr> </tr>
<?php endfor; ?> <?php endfor; ?>

258
index0.php Normal file
View File

@ -0,0 +1,258 @@
<?php
ini_set('display_errors', 1);
ini_set('html_errors', 1);
ini_set('error_reporting', E_ALL);
$default_lignes = 3;
$default_colonnes = 4;
$hauteur = filter_input(INPUT_GET, 'lignes', FILTER_VALIDATE_INT, [
"options" => [
"default" => $default_lignes,
"min_range" => 2,
"max_range" => 10
]
]);
$largeur = filter_input(INPUT_GET, 'colonnes', FILTER_VALIDATE_INT, [
"options" => [
"default" => $default_colonnes,
"min_range" => 2,
"max_range" => 10
]
]);
$dico = [];
if (($handle = fopen("dico.csv", "r")) !== FALSE) {
$header = fgetcsv($handle, 0, "\t");
while (($ligne = fgetcsv($handle, 0, "\t")) !== FALSE) {
if (count($ligne) >= 2) {
$mot = $ligne[0];
$definition = $ligne[1];
$dico[$mot] = $definition;
}
}
fclose($handle);
}
$mots_de_n_lettres = [];
foreach ($dico as $mot => $definition) {
$n = strlen($mot);
if (!isset($mots_de_n_lettres[$n])) {
$mots_de_n_lettres[$n] = [];
}
$mots_de_n_lettres[$n][] = $mot;
}
foreach ($mots_de_n_lettres as $n => $liste_mots) {
shuffle($mots_de_n_lettres[$n]);
}
$mots_par_position = [];
foreach ([$hauteur, $largeur] as $n) {
$mots_par_position[$n] = [];
foreach ($mots_de_n_lettres[$n] as $mot) {
foreach (str_split($mot) as $i => $lettre) {
if (!isset($mots_par_position[$n][$i])) {
$mots_par_position[$n][$i] = [];
}
if (!isset($mots_par_position[$n][$i][$lettre])) {
$mots_par_position[$n][$i][$lettre] = [];
}
$mots_par_position[$n][$i][$lettre][] = $mot;
}
}
}
function pire_contrainte($tests, $nb_lettres, $i, $mot)
{
global $mots_par_position;
$nb_mots_min = PHP_INT_MAX;
$pire_contrainte = 0;
foreach ($tests as $test) {
if (
!array_key_exists($i, $mots_par_position[$nb_lettres]) ||
!array_key_exists($mot[$test], $mots_par_position[$nb_lettres][$i])
) {
return -1;
} else {
$nb_mots = count($mots_par_position[$nb_lettres][$i][$mot[$test]]);
if ($nb_mots < $nb_mots_min) {
$pire_contrainte = $test;
$nb_mots_min = $nb_mots;
}
}
}
return $pire_contrainte;
}
class Grille
{
public $hauteur;
public $largeur;
public $grille;
public $lignes_restantes;
public $colonnes_restantes;
public function __construct($hauteur, $largeur)
{
$this->hauteur = $hauteur;
$this->largeur = $largeur;
$this->grille = array_fill(0, $hauteur, array_fill(0, $largeur, '.'));
$this->lignes_restantes = range(0, $hauteur - 1);
$this->colonnes_restantes = range(0, $largeur - 1);
}
public function get_ligne($l)
{
return implode("", $this->grille[$l]);
}
public function set_ligne($l, $mot)
{
for ($i = 0; $i < strlen($mot); $i++) {
$this->grille[$l][$i] = $mot[$i];
}
}
public function get_colonne($c)
{
$colonne = "";
for ($i = 0; $i < $this->hauteur; $i++) {
$colonne .= $this->grille[$i][$c];
}
return $colonne;
}
public function set_colonne($c, $mot)
{
for ($i = 0; $i < strlen($mot); $i++) {
$this->grille[$i][$c] = $mot[$i];
}
}
public function genere()
{
global $mots_de_n_lettres;
$l = $this->largeur / 2;
array_splice($this->lignes_restantes, $l, 1);
foreach ($mots_de_n_lettres[$this->largeur] as $mot_lig) {
$this->set_ligne($l, $mot_lig);
yield from $this->trouve_une_colonne($l, $mot_lig);
}
$this->lignes_restantes[] = $l;
$this->grille[$l] = array_fill(0, $this->largeur, '.');
}
public function trouve_une_colonne($l, $mot_lig)
{
global $mots_par_position;
$c = pire_contrainte($this->colonnes_restantes, $this->hauteur, $l, $mot_lig);
if ($c == -1) {
return;
}
$colonne = $this->get_colonne($c);
array_splice($this->colonnes_restantes, $c, 1);
foreach ($mots_par_position[$this->hauteur][$l][$mot_lig[$c]] as $mot_col) {
if ($mot_col == $colonne || preg_match("/^$colonne$/", $mot_col)) {
$this->set_colonne($c, $mot_col);
if (count($this->lignes_restantes)) {
yield from $this->trouve_une_ligne($c, $mot_col);
} else if (count($this->colonnes_restantes)) {
yield from $this->trouve_une_colonne($l, $mot_lig);
} else {
yield;
}
}
}
$this->colonnes_restantes[] = $c;
$this->set_colonne($c, $colonne);
}
public function trouve_une_ligne($c, $mot_col)
{
global $mots_par_position;
$l = pire_contrainte($this->lignes_restantes, $this->largeur, $c, $mot_col);
if ($l == -1) {
return;
}
$ligne = $this->get_ligne($l);
array_splice($this->lignes_restantes, $l, 1);
foreach ($mots_par_position[$this->largeur][$c][$mot_col[$l]] as $mot_lig) {
if ($mot_lig == $ligne || preg_match("/^$ligne$/", $mot_lig)) {
$this->set_ligne($l, $mot_lig);
if (count($this->colonnes_restantes)) {
yield from $this->trouve_une_colonne($l, $mot_lig);
} else if (count($this->lignes_restantes)) {
yield from $this->trouve_une_ligne($c, $mot_col);
} else {
yield;
}
}
}
$this->lignes_restantes[] = $l;
$this->set_ligne($l, $ligne);
}
public function affiche()
{
echo "<table>";
echo "<tr><th></th>";
for ($c = 0; $c < $this->largeur; $c++) {
echo "<th>" . chr($c + 65) . "</th>";
}
echo "</tr>";
for ($l = 0; $l < $this->hauteur; $l++) {
echo "<tr><th>" . $l . "</th>";
for ($c = 0; $c < $this->largeur; $c++) {
echo "<td>" . $this->grille[$l][$c] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}
$grille = new Grille($hauteur, $largeur);
$grille->genere()->current();
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Mots croisés</title>
<style>
table {
border-collapse: collapse;
}
td {
width: 30px;
height: 30px;
text-align: center;
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th></th>
<?php for ($c = 0; $c < $largeur; $c++): ?>
<th><?= chr($c + 65) ?></th>
<?php endfor; ?>
</tr>
<?php for ($l = 0; $l < $hauteur; $l++): ?>
<tr>
<th><?= $l ?></th>
<?php for ($c = 0; $c < $largeur; $c++): ?>
<td><?= $grille->grille[$l][$c] ?></td>
<?php endfor; ?>
</tr>
<?php endfor; ?>
</table>
</html>

View File

@ -23,19 +23,19 @@ def melange(iterable):
def mots_de_n_lettres(n): def mots_de_n_lettres(n):
for mot in mots[n]: for mot in mots[n]:
yield mot yield mot
for mot in mots[n-1]: # for mot in mots[n-1]:
yield f"{mot} " # yield f"{mot} "
yield f" {mot}" # yield f" {mot}"
for i in range(2, ceil(n / 2)): for i in range(2, ceil(n / 2)):
for mot1, mot2 in product(mots[i], mots_de_n_lettres(n - i - 1)): for mot1, mot2 in product(mots[i], mots_de_n_lettres(n - i - 1)):
yield f"{mot1} {mot2}" yield f"{mot1} {mot2}"
yield f"{mot2} {mot1}" yield f"{mot2} {mot1}"
for mot1, mot2 in product(mots[i], mots_de_n_lettres(n - i - 2)): # for mot1, mot2 in product(mots[i], mots_de_n_lettres(n - i - 2)):
yield f" {mot1} {mot2}" # yield f" {mot1} {mot2}"
yield f"{mot2} {mot1} " # yield f"{mot2} {mot1} "
for mot1, mot2 in product(mots[i-1], mots_de_n_lettres(n - i - 1)): # for mot1, mot2 in product(mots[i-1], mots_de_n_lettres(n - i - 1)):
yield f" {mot1} {mot2}" # yield f" {mot1} {mot2}"
yield f"{mot2} {mot1} " # yield f"{mot2} {mot1} "
class Ligne: class Ligne:
@ -70,14 +70,14 @@ class Grille:
self.colonne = Colonne(self.grille) self.colonne = Colonne(self.grille)
self.mots_de_n_lettres = { self.mots_de_n_lettres = {
hauteur: melange(mots_de_n_lettres(hauteur)), hauteur: set(mots_de_n_lettres(hauteur)),
largeur: melange(mots_de_n_lettres(largeur)), largeur: set(mots_de_n_lettres(largeur)),
} }
self.mots_par_position = defaultdict(lambda: defaultdict(set)) self.mots_par_position = defaultdict(lambda: defaultdict(list))
for nb_lettres in (self.largeur, self.hauteur): for nb_lettres in (self.largeur, self.hauteur):
for mot in self.mots_de_n_lettres[nb_lettres]: for mot in self.mots_de_n_lettres[nb_lettres]:
for i, lettre in enumerate(mot): for i, lettre in enumerate(mot):
self.mots_par_position[nb_lettres][(i, lettre)].add(mot) self.mots_par_position[nb_lettres][(i, lettre)].append(mot)
self.generations = self.genere() self.generations = self.genere()
try: try:
@ -98,12 +98,12 @@ class Grille:
l = 0 l = 0
self.lignes_restantes.remove(l) self.lignes_restantes.remove(l)
for mot_lig in self.mots_de_n_lettres[self.largeur]: for mot_lig in self.mots_de_n_lettres[self.largeur]:
# if ' ' in mot_lig: if ' ' in mot_lig:
# continue continue
self.ligne[l] = mot_lig self.ligne[l] = mot_lig
yield from self.trouve_une_colonne(l, mot_lig) yield from self.trouve_une_colonne(l, mot_lig)
self.ligne[l] = "." * self.largeur #self.ligne[l] = "." * self.largeur
self.lignes_restantes.add(l) #self.lignes_restantes.add(l)
def trouve_une_colonne(self, l, mot_lig): def trouve_une_colonne(self, l, mot_lig):
#print((len(self.colonnes_restantes) + len(self.lignes_restantes)) / (self.largeur + self.hauteur)) #print((len(self.colonnes_restantes) + len(self.lignes_restantes)) / (self.largeur + self.hauteur))
@ -177,8 +177,5 @@ if __name__ == "__main__":
end = time.time() end = time.time()
print(f"Execution time: {end - self.start:.2f} seconds") print(f"Execution time: {end - self.start:.2f} seconds")
for n in range(3, 14):
with Timer(): with Timer():
print(Grille(n, n)) print(Grille(5, 5))
with Timer():
print(Grille(n, n+1))

135
test1.py Normal file
View File

@ -0,0 +1,135 @@
import csv
from re import compile, match
from random import choice, sample, randrange
from collections import defaultdict
from math import ceil
from itertools import product, chain
dico = defaultdict(list)
with open("dico.csv", "r", encoding="utf-8") as fichier:
for mot, definition in csv.reader(fichier, delimiter="\t"):
if not mot.startswith("#"):
dico[mot].append(definition)
mots_de_n_lettres = defaultdict(set)
for mot in dico:
mots_de_n_lettres[len(mot)].add(mot)
def mots_espaces(n):
for mot in mots_de_n_lettres[n]:
yield mot
# for mot in mots_de_n_lettres[n-1]:
# yield f"{mot} "
# yield f" {mot}"
for i in range(2, ceil(n / 2)):
for mot1, mot2 in product(mots_de_n_lettres[i], mots_espaces(n - i - 1)):
yield f"{mot1} {mot2}"
yield f"{mot2} {mot1}"
# for mot1, mot2 in product(mots_de_n_lettres[i], mots_espaces(n - i - 2)):
# yield f" {mot1} {mot2}"
# yield f"{mot2} {mot1} "
# for mot1, mot2 in product(mots_de_n_lettres[i-1], mots_espaces(n - i - 1)):
# yield f" {mot1} {mot2}"
# yield f"{mot2} {mot1} "
class Ligne:
def __init__(self, grille):
self.grille = grille
def __getitem__(self, n):
return "".join(self.grille[n][:n])
def __setitem__(self, n, mot):
self.grille[n] = list(mot)
class Colonne:
def __init__(self, grille):
self.grille = grille
def __getitem__(self, n):
return "".join(ligne[n] for ligne in self.grille[:n+1])
def __setitem__(self, n, mot):
for i, char in enumerate(mot):
self.grille[i][n] = char
class Grille:
def __init__(self, hauteur, largeur):
self.hauteur = hauteur
self.largeur = largeur
self.grille = [["." for _ in range(largeur)] for _ in range(hauteur)]
self.ligne = Ligne(self.grille)
self.colonne = Colonne(self.grille)
self.mot_commencant_par = defaultdict(lambda: defaultdict(list))
for dimension in (hauteur,) if hauteur == largeur else (hauteur, largeur):
for mot in mots_espaces(dimension):
for i in range(dimension+1):
self.mot_commencant_par[dimension][mot[:i]].append(mot)
self.grilles = self.genere_grilles()
next(self.grilles)
def __iter__(self):
return self
def __next__(self):
return next(self.grilles)
def genere_grilles(self):
print(f"Grille({self.hauteur}, {self.largeur})")
yield from self.trouve_une_ligne(0)
def trouve_une_ligne(self, i):
for mot in self.mot_commencant_par[self.largeur][self.ligne[i]]:
self.ligne[i] = mot
if i < self.largeur:
yield from self.trouve_une_colonne(i)
elif i + 1 < self.hauteur:
yield from self.trouve_une_ligne(i + 1)
else:
yield self
def trouve_une_colonne(self, i):
for mot in self.mot_commencant_par[self.hauteur][self.colonne[i]]:
self.colonne[i] = mot
if i + 1 < self.hauteur:
yield from self.trouve_une_ligne(i + 1)
elif i + 1 < self.largeur:
yield from self.trouve_une_colonne(i + 1)
else:
yield self
def __str__(self):
return (
" "
+ " ".join(chr(65 + i) for i in range(self.largeur))
+ "\n"
+ "\n".join(
f"{i + 1:2} " + " ".join(ligne) for i, ligne in enumerate(self.grille)
)
)
def __repr__(self):
return self.__str__()
if __name__ == "__main__":
import time
class Timer:
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *exc_info):
end = time.time()
print(f"Execution time: {end - self.start:.2f} seconds")
with Timer():
print(Grille(6, 6))

View File

@ -12,25 +12,25 @@ with open("dico.csv", "r", encoding="utf-8") as fichier:
if not mot.startswith("#"): if not mot.startswith("#"):
dico[mot].append(definition) dico[mot].append(definition)
mots = defaultdict(set) mots_de_n_lettres = defaultdict(set)
for mot in dico: for mot in dico:
mots[len(mot)].add(mot) mots_de_n_lettres[len(mot)].add(mot)
def mots_de_n_lettres(n): def mots_espaces(n):
for mot in mots[n]: for mot in mots_de_n_lettres[n]:
yield mot yield mot
# for mot in mots[n-1]: # for mot in mots_de_n_lettres[n-1]:
# yield f"{mot} " # yield f"{mot} "
# yield f" {mot}" # yield f" {mot}"
for i in range(2, ceil(n / 2)): for i in range(2, ceil(n / 2)):
for mot1, mot2 in product(mots[i], mots_de_n_lettres(n - i - 1)): for mot1, mot2 in product(mots_de_n_lettres[i], mots_espaces(n - i - 1)):
yield f"{mot1} {mot2}" yield f"{mot1} {mot2}"
yield f"{mot2} {mot1}" yield f"{mot2} {mot1}"
# for mot1, mot2 in product(mots[i], mots_de_n_lettres(n - i - 2)): # for mot1, mot2 in product(mots_de_n_lettres[i], mots_espaces(n - i - 2)):
# yield f" {mot1} {mot2}" # yield f" {mot1} {mot2}"
# yield f"{mot2} {mot1} " # yield f"{mot2} {mot1} "
# for mot1, mot2 in product(mots[i-1], mots_de_n_lettres(n - i - 1)): # for mot1, mot2 in product(mots_de_n_lettres[i-1], mots_espaces(n - i - 1)):
# yield f" {mot1} {mot2}" # yield f" {mot1} {mot2}"
# yield f"{mot2} {mot1} " # yield f"{mot2} {mot1} "
@ -69,7 +69,7 @@ class Grille:
self.mot_commencant_par = defaultdict(lambda: defaultdict(list)) self.mot_commencant_par = defaultdict(lambda: defaultdict(list))
self.mots_finissant_par = defaultdict(lambda: defaultdict(list)) self.mots_finissant_par = defaultdict(lambda: defaultdict(list))
for dimension in (hauteur,) if hauteur == largeur else (hauteur, largeur): for dimension in (hauteur,) if hauteur == largeur else (hauteur, largeur):
for mot in mots_de_n_lettres(dimension): for mot in mots_espaces(dimension):
for i in range(dimension+1): for i in range(dimension+1):
self.mot_commencant_par[dimension][mot[:i]].append(mot) self.mot_commencant_par[dimension][mot[:i]].append(mot)
self.mots_finissant_par[dimension][mot[i:]].append(mot) self.mots_finissant_par[dimension][mot[i:]].append(mot)
@ -130,10 +130,7 @@ if __name__ == "__main__":
def __exit__(self, *exc_info): def __exit__(self, *exc_info):
end = time.time() end = time.time()
print(f"Execution time: {end - self.start:.2f} seconds\n") print(f"Execution time: {end - self.start:.2f} seconds")
for i in range(2, 14):
with Timer(): with Timer():
print(Grille(i, i)) print(Grille(5, 5))
with Timer():
print(Grille(i, i+1))