| 
| Code of simon 
<?php
Back to results/*
 
 Code by Simon Robinson (simonr@uk2k.com / http://imafish.com)
 as a solution to the problem given at
 https://www.php-editors.com/contest_b_1.htm.
 
 Instructions: Place this script and a file called deck.txt
 that contains the mixed deck, then navigate to the script
 in a web browser. The output is <solution to problem><no. of moves>
 
 Finished apart from including timeout function to display current
 solution if time > 60 secs (or whatever the timeout is set
 to)... is this possible in php?
 
 I should probably have entered into the beginner contest
 as i am a bit of a newbie, but this looked more fun :p
 
 */
 
 $filename = "deck.txt";
 $fp = @fopen($filename, "r");
 $contents = @fread($fp, filesize($filename));
 @fclose($fp);
 
 function sortthem($scftype, $file){
 $scftype = strtoupper($scftype);
 for ($k = 0; $k < strlen($scftype); $k++){
 $onemove = $scftype[$k];
 $strlen = strlen($file);
 if($onemove == "C"){ //Cut
 $file = substr($file,($strlen / 2),$strlen) . substr($file,0,($strlen / 2));
 }elseif($onemove == "S"){ //Shuffle
 $filetemp1 = substr($file,0,($strlen / 2));
 $filetemp2 = substr($file,($strlen / 2),$strlen);
 $filetemp3 = "";
 for ($g = 0; $g < (strlen($file) / 2); $g++){
 @$filetemp3 = $filetemp3 . $filetemp2[$g] . $filetemp1[$g];
 }
 $file = $filetemp3;
 }elseif($onemove == "F"){ //Flip
 $file = strrev($file);
 }
 }
 return $file;
 }
 
 /*
 
 The problem is modelled as a binary tree with 3 nodes, each having
 3, each having 3 etc etc. This is tackled row by row instead of the usual
 binary tree searching (traversal). Each row has 3^(rownumber)
 number of combinations to search through.
 
 */
 
 if(isset($contents)){
 $contents = ltrim(rtrim($contents)); // Must remove spaces etc to stop errors
 $trv = array(0 => "F", 1 => "C", 2 => "S");
 $tempsolve = "";
 $solution = "";
 if(strlen($contents) < 52){ //substr needs this, doesn't like substr("string",0,-0) for some reason
 $aim = substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ",0,-( 26 - (strlen($contents)/2) ));
 }else{
 $aim = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 }
 $aim = $aim . strtolower($aim);
 if($contents == $aim){
 echo "0";
 }else{
 while($tempsolve != $aim){
 foreach($trv as $ind => $solution){
 if ($solution != ""){
 $tempsolve = sortthem($solution, $contents);
 if ($tempsolve == $aim){
 $solution = str_replace("CC","",$solution); //Replace any unnecessary values not already done
 $solution = str_replace("FF","",$solution);
 echo $solution . strlen($solution); //Display answer
 //break; //Exits this foreach, but still performs the next one,
 // so use exit to save time. Re-ordering would lose single-char solutions.
 exit;
 }
 }
 }
 $max = count($trv);
 $i = 0;
 foreach($trv as $ind => $solution){
 if($i <= $max){
 //echo $ind . " " .  $solution . "<br>"; // For testing purposes
 
 $count = count($trv);
 $str = $trv[$i];
 
 if ($str[strlen($str) - 1] != "F"){ // Detect unnecessary F values
 $trv[$count] = $str . "F";
 }else{
 $count = $count - 1;
 }
 
 if ($str[strlen($str) - 1] != "C"){ // Detect unnecessary C values
 $trv[$count + 1] = $str . "C";
 }
 
 $trv[$i] = $str . "S"; //S doesn't cancel itself out unless in large numbers, and number different for length of $contents
 
 $i++;
 } //End of if($i <= $max){
 } //End of foreach($trv as $ind => $solution){
 } //End of while($tempsolve != $aim){
 } //End of if($contents == $aim){
 }else{
 echo "File not found. Either a) Doesn't exist or b) Wrong directory - must be in the same directory as this file";
 }
 
 ?>
 |  |