Code of cody
<?php error_reporting(E_ALL); define('DEBUG', false); class Deck { var $deck = array(); var $moves = ''; function CheckRedundancy() { global $shufcount; while(strstr($this->moves, 'CC') || strstr($this->moves, 'FF') || strstr($this->moves, str_repeat('S', $shufcount))) $this->moves = str_replace(array('CC', 'FF', str_repeat('S', $shufcount)), array(null, null, null), $this->moves); } function Flip() { $this->deck = array_reverse($this->deck); $this->moves .= 'F'; } function Cut() { $this->deck = array_merge(array_slice($this->deck, count($this->deck) / 2), array_slice($this->deck, 0, count($this->deck) / 2)); $this->moves .= 'C'; } function Shuffle() { $first = array_slice($this->deck, 0, count($this->deck) / 2); $last = array_slice($this->deck, count($this->deck) / 2); $this->deck = array(); for($i = 0; $i < count($first); $i++) { $this->deck[] = $first[$i]; $this->deck[] = $last[$i]; } $this->moves .= 'S'; } function visualize() { foreach($this->deck as $card) echo $card, "\n"; print $this->moves; } function Test() { $correct = $this->deck; sort($correct); if($correct == $this->deck) return true; return false; } } $fp = implode(null, file('deck.txt')); $fpdeck = array(); for($i = 0; $i < strlen($fp); $i++) $fpdeck[] = $fp[$i]; $best = array(); $bestin = array(); $deck = new Deck(); $deck->deck = $fpdeck; $z = 1; while(true) { $deck->Shuffle(); if($deck->deck == $fpdeck) break; $z++; } $shufcount = $z; for($w = 1; $w <= 3; $w++) { if(DEBUG) print '$w = ' . $w . "\n"; for($x = 1; $x <= 3; $x++) { if(DEBUG) print "\t" . '$x = ' . $x . "\n"; for($y = 1; $y <= 3; $y++) { if(DEBUG) print "\t\t" . '$y =' . "\t" . $y . "\t" . '-' . "\t"; $bestcur = ''; for($a = 0; $a < 25; $a++) { $deck->deck = $fpdeck; $deck->moves = ''; $no = false; $z = 0; $last = 'S'; while(!$deck->Test()) { $rand = rand(1, $w + $x + $y); if($rand <= $w) { $deck->Cut(); if($last == 'C') { $deck->moves = substr($deck->moves, 0, -2); $last = 'S'; } else $last = 'C'; } elseif($rand > $w && $rand <= $x + $w) { $deck->Flip(); if($last == 'F') { $deck->moves = substr($deck->moves, 0, -2); $last = 'S'; } else $last = 'F'; } else { $deck->Shuffle(); $last = 'S'; } if($z++ == 100) { $no = true; break; } } if($no === false && (empty($bestcur) || strlen($deck->moves) < strlen($bestcur))) { $bestcur = $deck->moves; } // if($a == 99 && empty($best)) // $a = 0; } if(!empty($bestcur)) { $best[] = $bestcur; $bestin[] = array($w, $x, $y); } else { $bad[] = $bestcur; $badin[] = array($w, $x, $y); } if(DEBUG) print $bestcur . "\n"; } } } $temp = ''; $tempkey = null; if(count($best) > 0) { foreach($best as $key => $mov) { if(strlen($temp) == 0 || strlen($mov) < strlen($temp)) { $tempkey = $key; $temp = $mov; } } print $best[$tempkey] . strlen($best[$tempkey]); } else { foreach($bad as $key => $mov) { if(strlen($temp) == 0 || strlen($mov) < strlen($temp)) { $tempkey = $key; $temp = $mov; } } print $bad[$tempkey] . strlen($bad[$tempkey]); }
?>
Back to results
|
|