Code of ingo
<?php

/*
* Contest B3 from https://www.php-editors.com/contests.php
* see rules.txt for the rules of the contest
*
* Author:
* Ingo Claro
* miclaro@gmx.de
* http://ingo.netred.cl
*
* "Hard work has future payoff. Laziness pays off now."
*
* Benchmark included from PEAR, just uncomment the proper lines
*/

/*
* file format:
* word1 word2 word3,
* jumbled1 jumbled2 jumbled3
* jumbled4.
* word1 .......
*/

// benchmark
//require 'Benchmark-1.2.1/Timer.php'; //PEAR benchmark
//$timer = new Benchmark_Timer();
//$timer->start();


// sort function
function lensort($a$b)
{
   return 
strlen($b)-strlen($a);
}


//START

// read input
$fp=fopen("lists.txt","r");
$contents fread ($fpfilesize ("lists.txt"));
fclose ($fp);
$contents=trim(str_replace("\r\n","",$contents));
$games=explode(".",$contents);
// check if last entry is empty
if($games[sizeof($games)]=="")
   
array_pop($games);


$globalScore=0;
$abc=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');

// solve the games
foreach($games as $game)
{

   
$numjumbled=array();

   list(
$words,$jumbled)=explode(",",$game);
   
$words=explode(" ",$words);
   
usort($words,"lensort");

   
// get word count for jumbled words
   
$jumbled=str_replace(" ","",$jumbled);
   foreach(
$abc as $letter)
   {
       
$count=-1;
       
$pos=-1;
       do
       {
           
$pos=strpos($jumbled,$letter,$pos+1);
           
$count++;
       } while(
$pos!==false);
       
$globalNumJumbled["$letter"]=$count;

       
// get word count for each word
       
foreach($words as $word)
       {
           
$count=-1;
           
$pos=-1;
           do
           {
               
$pos=strpos($word,$letter,$pos+1);
               
$count++;
           } while(
$pos!==false);
           
$wordCount["$word"]["$letter"]=$count;
       }

   }

// try all red letters combinations and keep the highest score.
   
$solution["score"]=0;
   
$solution["words"]=array();
   
$solution["red1"]="";
   
$solution["red2"]="";


   
// can't have 2 blanks with same letter
   
for($i=0;$i<count($abc);$i++)
   {
       for(
$j=$i+1;$j<count($abc);$j++)
       {
           
$red1=$abc[$i];
           
$red2=$abc[$j];
           
$numJumbled=$globalNumJumbled;
           
$numJumbled["$red1"]++;
           
$numJumbled["$red2"]++;

           
$score=0;
           
$wordList=array();

           foreach(
$words as $word)
           {
               
$ok=1;
               
$markR1=0;
               
$markR2=0;
               foreach(
$abc as $letter)
               {
                   
$count=$wordCount["$word"]["$letter"];
                   if(
$numJumbled["$letter"]<$count // we can't build the word
                   
{
                       
$ok=0;
                       break;
                   }
                   
// check red letters
                   
if($markR1==&& $red1==$letter && $numJumbled["$letter"]==$count)
                   {
                       
$markR1=1;
                       
$pos1=strpos($word,$letter,0);

                   }
                   if(
$markR2==&& $red2==$letter && $numJumbled["$letter"]==$count)
                   {
                       
$markR2=1;
                       
$pos2=strpos($word,$letter,0);
                   }

               } 
//foreach($abc as $letter)
               
if($ok==1)
               {
                   
$score+=strlen($word);
                   
// take letters out
                   
foreach($abc as $letter)
                   {
                       
$numJumbled["$letter"]-=$wordCount["$word"]["$letter"];
                   }

                   
// if i can use the red letters again, uncomment this
                /*   if($markR1==1 && $wordCount["$word"]["$red1"] >0)
                       $numJumbled["$red1"]++;
                   if($markR2==1 && $wordCount["$word"]["$red2"] >0)
                       $numJumbled["$red2"]++;
                */

                   // check red letters
                   
if($markR1==1)
                       
$word=substr($word,0,$pos1) . "<font color='ff0000'>$red1</font>" substr($word,$pos1+1);
                   if(
$markR2==1)
                       
$word=substr($word,0,$pos2) . "<font color='ff0000'>$red2</font>" substr($word,$pos2+1);
                   
$wordList[]=$word;
               } 
//if($ok==1)

           
}//foreach($words as $word)
           
if($score>$solution["score"])
           {
               
// this is the new solution
               
$solution["score"]=$score;
               
$solution["words"]=$wordList;
               
$solution["red1"]=$red1;
               
$solution["red2"]=$red2;
           }


           
// next try
           
$numJumbled["$red1"]--;
           
$numJumbled["$red2"]--;

       }
// foreach($abc as $red2)
   
}// foreach($abc as $red1)

   // print the solution, keep the score
   
foreach($solution["words"] as $sol)
       echo 
"$sol ";
   echo 
"<br>\n";

   
$globalScore+=$solution["score"];

// foreach($games as $game)

// print the score
echo $globalScore;

// benchmark
//$timer->stop();
//$timer->display();


?>


Back to results


© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.