Main Menu |
|
|
Forums |
|
|
Programming
Contest |
|
|
Documentation
|
|
|
Partner
Sites |
|
|
Sponsors |
|
|
|
Code of andrew
<?php $len = 0; $deck = ""; $windeck = ""; $windeckr = ""; $newdeck = ""; $decks = array(); $actions = array();
function docut() { global $len; global $deck;
return substr($deck, $len / 2) . substr($deck, 0, $len / 2); }
function doshuffle() { global $len; global $deck; $shuffled = ""; for ($i = 0; $i < $len / 2; $i++) $shuffled .= $deck[$len / 2 + $i] . $deck[$i]; return $shuffled; }
function readdata() { global $len; global $deck; global $windeck; global $windeckr; global $decks; global $actions; $f = fopen("deck.txt", "r"); $deck = trim(fgets($f, 100)); fclose($f); $len = strlen($deck); $letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $windeck = substr($letters, 0, $len / 2); $windeck .= strtolower($windeck); $windeckr = strrev($windeck); $decks[] = $deck; $actions[] = array("", -1); }
function getscore() { global $newdeck; global $windeck; global $len;
$score = 0; for ($i = 0; $i < $len; $i++) if ($newdeck[$i] == $windeck[$i]) $score++; return $score; }
function addturn($action, $prev) { global $newdeck; global $decks; global $actions; global $cur; global $bestscore; global $bestpos; $decks[] = $newdeck; $actions[] = array($action, $prev); $score = getscore(); if ($score > $bestscore) { $bestscore = $score; $bestpos = $cur; } }
$starttime = time();
readdata(); $cur = 0; $newdeck = $deck; $bestscore = getscore(); $bestpos = 0; $turnsadd = ""; while ($cur < count($decks) && time() - $starttime < 58) { $deck = $decks[$cur];
$newdeck = doshuffle($deck); addturn("S", $cur); if ($newdeck == $windeck || $newdeck == $windeckr) { $bestpos = count($decks) - 1; $bestscore = $len; if ($newdeck == $windeckr) $turnsadd = "F"; break; } if ($actions[$cur][0] == "C") { $decks[$cur] = null; $cur++; continue; } $newdeck = docut($deck); addturn("C", $cur); if ($newdeck == $windeck || $newdeck == $windeckr) { $bestpos = count($decks) - 1; $bestscore = $len; if ($newdeck == $windeckr) $turnsadd = "F"; break; } $decks[$cur] = null; $cur++; }
$turns = ""; $cur = $bestpos; while ($actions[$cur][1] != -1) { $turns .= $actions[$cur][0]; $cur = $actions[$cur][1]; } $turns = strrev($turns); $turns .= $turnsadd; echo $turns . strlen($turns) . "\n"; ?>
Back to results
|
|
|