Code of tanel
<?php
#######################################################
# Script made by Tanel 'Zeth' Võsumaa for php-editors #
#.com contest. 12:11 - 15:43 (23.03.2003). #
#######################################################
################################### tanel@inkblue.net #
#####################
global $deck;
$file = file("deck.txt");
$deck = $file[0];
/*function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();
*/
function flip($string){
return strrev($string);
}
function shuff($string){
$strlen = strlen($string);
for($i = 0;$i <= $strlen;$i++){
$array[$string{$i}] = $string{$i};
}
$strlen = strlen($string);
$half = ($strlen / 2);
$last = substr($string, $half);
$first = substr($string, 0, $half);
$strlen = strlen($first);
for($x=0;$x<=($strlen-1);$x++){
$return .= $last{$x}.$first{$x};
}
return $return;
}
function cut($string){
$strlen = strlen($string);
$half = ($strlen / 2);
$last = substr($string, $half);
$first = substr($string, 0, $half);
return $last.$first;
}
function getright($deck){
$deckstrlen = strlen($deck);
for($i = 0;$i <= $deckstrlen;$i++){
$array[$deck{$i}] = $deck{$i};
}
ksort($array);
//print_r(array_values($array));
while (list ($key, $val) = each ($array)) {
$newvar .= $key;
}
return $newvar;
}
function compare($deck, $current){
//echo "Compare: $deck $current";
$count = 0;
$deckstrlen = (strlen($deck) -1);
for($i = 0;$i <= $deckstrlen;$i++){
if($deck{$i} == $current{$i}){
//echo "X";
$count++;
} else {
//echo "-";
}
}
//echo " | ".$count."<br>";
return $count;
}
function getbest($number, $walks, $current){
//echo "number: ".$number;
if($GLOBALS['number'] < $number){
$GLOBALS['number'] = $number;
$GLOBALS['walks'] = $walks;
$GLOBALS['current'] = $current;
$a = 1;
}
// echo $current."<br>\n";
return $a;
}
$correct = getright($deck);
function random($deck){
$switch = rand(1,3);
switch($switch){
case 1:
$current = flip($deck);
$GLOBALS['tempwalks'] .= "F";
getbest(compare($GLOBALS['correct'], $current), $GLOBALS['tempwalks'], $current);
//getlucky($deck, $current);
$GLOBALS['lastswitch'] = "F";
break;
case 2:
$current = shuff($deck);
$GLOBALS['tempwalks'] .= "S";
getbest(compare($GLOBALS['correct'], $current), $GLOBALS['tempwalks'], $current);
//getlucky($deck, $current);
$GLOBALS['lastswitch'] = "S";
break;
case 3:
//if($GLOBALS['tempwalks'] != "C"){
$current = cut($deck);
$GLOBALS['tempwalks'] .= "C";
getbest(compare($GLOBALS['correct'], $current), $GLOBALS['tempwalks'], $current);
//getlucky($deck, $current);
$GLOBALS['lastswitch'] = "C";
//} else {
//random($deck);
//}
break;
}
return $current;
}
function main($deck){
$GLOBALS['number'] = 0;
$GLOBALS['correct'] = getright($deck);
for($ximes = 1;$ximes <= 20;$ximes++){
$xeck = $deck;
for($times = 1;$times <= 50;$times++){
$xeck = random($xeck);
}
}
//echo "We found ".$GLOBALS['number']." correct cards with ".$GLOBALS['walks']." and the ending deck was ".$GLOBALS['current']." and the ending deck should have been ".$GLOBALS['correct']."<br>";
echo $GLOBALS['walks'].strlen($GLOBALS['walks']);
}
main($deck);
/*$time_end = getmicrotime();
echo "<br>The script executed for ".$time = ($time_end - $time_start)." seconds."; */
?>
Back to results
|
|