Code of eugene
<?PHP
// dejumbler.php
// By Eugene Otto May - June 2003 for PHP-Editors.com Contest B3
////////////////////
// Start input stuff
// Read lists.txt into character array
$handle = fopen ('./lists.txt', 'r');
while (!feof ($handle)) {
$file .= fgetc ($handle);
}
fclose ($handle);
// Split character string into array of word list / scrambled sentence Pairs
$Pairs = explode ('.', $file);
// Pop off last element of pair list (it's empty)
array_pop ($Pairs);
// Split pair strings into word list and scrambled sentence array
$SizeOfPairs = sizeOf ($Pairs);
for ($i = 0; $i < $SizeOfPairs; $i ++) {
$Pairs [$i] = split (',', trim ($Pairs [$i])); // the trim () is there to cut off the space that's after every '.'
}
// Split word list into array
for ($i = 0; $i < $SizeOfPairs; $i ++) {
$Pairs [$i][0] = split (' ', $Pairs [$i][0]);
}
// End input stuff
//////////////////
// Pairs structure:
// Array Pairs (
// Array Wordlist (word1, word2, word3, word4, ...),
// String ScrambledWords
// ),
// ...
function BuildMatrix4 ($Words, $Letters) {
global $_MaxArray, $_NumWords;
$SizeOfWords = sizeOf ($Words);
$MaxString = '';
for ($i = 0; $i < $SizeOfWords; $i ++) {
$StrLenWordsI = strlen ($Words [$i]);
$LetterMask = $Letters;
for ($j = 0; $j < $StrLenWordsI; $j ++) {
if (($LetterMask [$Words [$i]{$j}] == -1) || (($LetterMask [$Words [$i]{$j}] == 0) && ($LetterMask ['*'] == 2))) {
break;
}
else if ($LetterMask [$Words [$i]{$j}] == 0) {
$LetterMask [$Words [$i]{$j}] = -1;
$LetterMask ['*'] ++;
}
else {
$LetterMask [$Words [$i]{$j}] --;
}
}
if ($j == $StrLenWordsI) {
$StringX = $Words [$i] . ' ' . BuildMatrix4 (CutElement ($Words, $i), $LetterMask);
if (strlen (str_replace (' ', '', $StringX)) > strlen (str_replace (' ', '', $MaxString))) {
$MaxString = $StringX;
}
}
}
return $MaxString;
}
// Removes element of index $Index from array $List
function CutElement (&$List, &$Index) {
for ($i = 0; $i < $Index; $i ++) {
$NewArray [$i] = $List [$i];
}
$SizeOfList = sizeOf ($List);
for ($i = $Index + 1; $i < $SizeOfList; $i ++) {
$NewArray [$i - 1] = $List [$i];
}
return $NewArray;
}
// Puts letters into associative array
function BuildLetterRow (&$Letters) {
$StrLenLetters = strlen ($Letters);
for ($i = 0; $i < $StrLenLetters; $i ++) {
$LetterMask [$Letters {$i}] ++;
}
return $LetterMask;
}
function PrintSolution ($MaxString, $LetterMask) {
$SizeOfMaxString = sizeOf ($MaxString);
for ($i = 0; $i < $SizeOfMaxString; $i ++) {
$StrLenMaxString = strlen ($MaxString [$i]);
for ($j = 0; $j < $StrLenMaxString; $j ++) {
if ($LetterMask [$MaxString [$i]{$j}] == 0) {
echo '<FONT color=#FF0000>', $MaxString [$i]{$j}, '</FONT>';
}
else {
echo $MaxString [$i]{$j};
$LetterMask [$MaxString [$i]{$j}] --;
}
}
echo ' ';
}
}
// Swaps values of a and b
function Swap (&$a, &$b) {
$c = $a;
$a = $b;
$b = $c;
}
// Sort list of strings in descending order
function MySort (&$List) {
$SizeOfList = sizeOf ($List);
for ($i = 0; $i < $SizeOfList; $i ++) {
$StrLenI = strlen ($List [$i]);
for ($j = 0; $j < $SizeOfList; $j ++) {
if ($StrLenI > strlen ($List [$j])) {
Swap ($List [$i], $List [$j]);
}
}
}
}
//////////////////
// METHOD 2 STUFF
function InitWords (&$Words) {
global $_NumWords;
for ($i = 0; $i < $_NumWords; $i ++) {
$Temp [$i] = Array ( "Word" => $Words [$i],
"WordMask" => $Words [$i],
"FinalWord" => $Words [$i],
"Size" => strlen ($Words [$i]),
"Open" => strlen ($Words [$i]),
"Weight" => @((100 / $_NumWords) / strlen ($Words [$i]))
);
}
$Words = $Temp;
unset ($Temp);
}
function FillWords (&$Words, &$Letters, &$SortNum) {
global $_NumWords;
$StrLenLetters = strlen ($Letters);
for ($i = 0; $i < $StrLenLetters; $i ++) {
for ($j = 0; $j < $_NumWords; $j ++) {
$Pos = strpos ($Words [$j]["WordMask"], $Letters {$i});
if ($Pos !== False) {
$Words [$j]["Open"] --;
$Words [$j]["WordMask"]{$Pos} = '*';
switch ($SortNum) {
case 0:
break;
case 1: MySort1 ($Words);
break;
case 2: MySort2 ($Words);
break;
case 3: MySort3 ($Words);
break;
case 4: MySort4 ($Words);
break;
case 5: MySort5 ($Words);
break;
case 6: MySort6 ($Words);
break;
default:
break;
}
break;
}
}
}
}
function PlugWilds (&$Words) {
global $_NumWords;
$UsedWildCards = Array ();
for ($i = 0; $i < $_NumWords; $i ++) {
if ($Words [$i]["Open"] && ($Words [$i]["Open"] <= (2 - sizeOf ($UsedWildCards)))) {
for ($j = 0; $j < strlen ($Words [$i]["WordMask"]); $j ++) {
if (($Words [$i]["WordMask"]{$j} != '*') && ($Words [$i]["WordMask"]{$j} != '<') && ($Words [$i]["WordMask"]{$j} != '>') && ($UsedWildCards [$Words [$i]["WordMask"]{$j}] != 1)) {
$Pos = strpos ($Words [$i]["WordMask"], $Words [$i]["WordMask"]{$j});
$UsedWildCards [$Words [$i]["WordMask"]{$j}] = 1;
$Words [$i]["FinalWord"] = substr_replace ($Words [$i]["FinalWord"], "<FONT color=#FF0000>" . $Words [$i]["WordMask"]{$j} . "</FONT>", $Pos, 1);
$Words [$i]["WordMask"] = substr_replace ($Words [$i]["WordMask"], "<******************>*<*****>", $Pos, 1);
$Words [$i]["Open"] --;
}
}
}
}
}
function MySort1 (&$Words) {
global $_NumLetters, $_NumWords;
for ($i = 0; $i < $_NumWords; $i ++) {
for ($j = 0; $j < $_NumWords; $j ++) {
if (@($Words [$i]["Open"] / $Words [$i]["Size"]) < @($Words [$j]["Open"] / $Words [$j]["Size"])) {
Swap ($Words [$i], $Words [$j]);
}
}
}
}
function MySort2 (&$Words) {
global $_NumLetters, $_NumWords;
for ($i = 0; $i < $_NumWords; $i ++) {
for ($j = 0; $j < $_NumWords; $j ++) {
if ($Words [$i]["Open"] < $Words [$j]["Open"]) {
Swap ($Words [$i], $Words [$j]);
}
}
}
}
function MySort3 (&$Words) {
global $_NumLetters, $_NumWords;
for ($i = 0; $i < $_NumWords; $i ++) {
for ($j = 0; $j < $_NumWords; $j ++) {
if (($Words [$i]["Open"] / $_NumLetters) < ($Words [$j]["Open"] / $_NumLetters)) {
Swap ($Words [$i], $Words [$j]);
}
}
}
}
function MySort4 (&$Words) {
global $_NumLetters, $_NumWords;
for ($i = 0; $i < $_NumWords; $i ++) {
for ($j = 0; $j < $_NumWords; $j ++) {
if (($Words [$i]["Open"] / $_NumLetters) > ($Words [$j]["Open"] / $_NumLetters)) {
Swap ($Words [$i], $Words [$j]);
}
}
}
}
function MySort5 (&$Words) {
global $_NumLetters, $_NumWords;
for ($i = 0; $i < $_NumWords; $i ++) {
for ($j = 0; $j < $_NumWords; $j ++) {
if ($Words [$i]["Size"] > $Words [$j]["Size"]) {
Swap ($Words [$i], $Words [$j]);
}
}
}
}
function MySort6 (&$Words) {
global $_NumLetters, $_NumWords;
for ($i = 0; $i < $_NumWords; $i ++) {
for ($j = 0; $j < $_NumWords; $j ++) {
if ($Words [$i]["Size"] < $Words [$j]["Size"]) {
Swap ($Words [$i], $Words [$j]);
}
}
}
}
function CountUsed (&$Words) {
global $_NumWords;
$Total = 0;
for ($i = 0; $i < $_NumWords; $i ++) {
if ($Words [$i]["Open"] == 0) {
$Total += $Words [$i]["Size"];
}
}
return $Total;
}
function StrongButSlow (&$Words, &$Letters) {
$LetterMask = BuildLetterRow ($Letters);
$MaxString = BuildMatrix4 ($Words, $LetterMask);
$NumLetters += strlen (trim (str_replace (' ', '', $MaxString)));
$MaxString = explode (' ', trim ($MaxString));
MySort ($MaxString);
PrintSolution ($MaxString, $LetterMask);
echo '<BR>';
return $NumLetters;
}
function FastButWeak (&$WordsX, &$Letters) {
global $_NumWords;
$MaxLetters = 0;
$MaxWordArray = Array ();
for ($j = 0; $j < 7; $j ++) {
$Words = $WordsX;
InitWords ($Words);
FillWords ($Words, $Letters, $j);
PlugWilds ($Words);
$Total = CountUsed ($Words);
if ($Total > $MaxLetters) {
$MaxLetters = $Total;
$MaxWordArray = $Words;
}
}
MySort5 ($MaxWordArray);
for ($k = 0; $k < $_NumWords; $k ++) {
if ($MaxWordArray [$k]["Open"] == 0)
print $MaxWordArray [$k]["FinalWord"] . " ";
}
print "<BR>";
return $MaxLetters;
}
$TotalLettersUsed = 0;
$SizeOfPairs = sizeOf ($Pairs);
$NumLetters = 0;
for ($i = 0; $i < $SizeOfPairs; $i ++) {
$Words = $Pairs [$i][0];
$_NumWords = sizeOf ($Words);
$_NumLetters = 0;
for ($j = 0; $j < sizeOf ($Words); $j ++) {
$_NumLetters += strlen ($Words [$j]);
}
$Letters = str_replace (' ', '', trim ($Pairs [$i][1]));
if ($_NumWords <= 9) {
$TotalLettersUsed += StrongButSlow ($Words, $Letters);
}
else {
$TotalLettersUsed += FastButWeak ($Words, $Letters);
}
}
echo $TotalLettersUsed;
?>
Back to results
|
|