Main Menu |
|
|
Forums |
|
|
Programming
Contest |
|
|
Documentation
|
|
|
Partner
Sites |
|
|
Sponsors |
|
|
|
Code of glassjaw
<?php error_reporting(E_ALL);
$max_time = ini_get('max_execution_time') - 1; $time = mktime();
$fp = fopen('deck.txt', 'r'); $deck_u = ''; while ($deck_u == '') $deck_u = chop(fgets($fp)); fclose($fp);
$len = strlen($deck_u); $mid = $len / 2;
for ($s_max = 0; $s_max < $len; $s_max++) { $lenf = $len + 1; $pow = pow(2, $s_max + 1); $mod = $pow - (floor($pow / $lenf) * $lenf); if ($mod == 1) break; }
if ($mid == 1 || $mid == 2 || $mid == 4 || $mid == 5 || $mid == 6 || $mid == 8 || $mid == 9 || $mid == 12 || $mid == 13 || $mid == 14 || $mid == 16 || $mid == 18 || $mid == 20 || $mid == 21 || $mid == 26) $s_max = (($s_max + 1) / 2) - 1;
for ($cs_max = 1; $cs_max <= $len; $cs_max++) { $lenf = $len - 1; $pow = pow(2, $cs_max); $mod = ($pow - (floor($pow / $lenf) * $lenf)); if ($mod == 1) break; } $cs_max *= 2;
if ($mid == 1 || $mid == 2 || $mid == 4 || $mid == 5 || $mid == 6 || $mid == 8 || $mid == 9 || $mid == 12 || $mid == 13 || $mid == 14 || $mid == 16 || $mid == 18 || $mid == 20 || $mid == 21 || $mid == 26) $s_max = ($s_max + 1) / 2 - 1;
$deck_s = substr('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 0, $mid) . substr('abcdefghijklmnopqrstuvwxyz', 0, $mid); $deck_a = array();
$comb_a[0] = ''; $deck_a[0] = $deck_u; $comb_a[1] = '3'; $deck_a[1] = strrev($deck_u);
if ($deck_s === $deck_a[1]) $comb = 3; else { $c_i = 0; $m_i = 1; $c_i_o = 0; $m_i_o = 0; $comb = 1; $loops = 0; $muts_app = 0; $comb_app = ''; $muts = 0; $key = 0; $step = -1; $method = 0;
do { if ($method === 1 || ($deck_a[$m_i][0] == 'A' && $deck_a[$m_i][$len-1] == chr($mid+96)) || ($deck_a[$m_i][0] == 'a' && $deck_a[$m_i][$len-1] == chr($mid+64))) { if ($method === 0) { $c_i_o = $c_i; $m_i_o = $m_i; $method = 1; $method_steps = 0; }
if ($method_steps === $cs_max) { for ($m = $m_i_o + 1; $m < $m_i; $m++) { unset($deck_a[$m]); unset($comb_a[$m]); } $c_i = $c_i_o; $m_i = $m_i_o; $method = 0; } else { $c_i = $m_i++; $key = &$comb_a[$c_i]; $step = ($step) ? 2 : 1; $method_steps++; } } if ($method === 0) { $m_i++; do { if ($step === 0) $c_i++;
$key = &$comb_a[$c_i];
if ($step < 1 && substr($key, -$s_max) != str_repeat('1', $s_max)) $step = 1; elseif ($step < 2 && substr($key, -1) !== '2') $step = 2; else $step = 0;
} while ($step === 0); }
$comb_a[$m_i] = $key . $step; $comb = &$comb_a[$m_i]; $deck_a[$m_i] = ''; $deck = &$deck_a[$m_i]; $deck_src = &$deck_a[$c_i];
switch ($step) { case 1: for($i = 0; $i < $len; $i++) $deck .= $deck_src[(1 - $i % 2) * $mid + floor($i/2)]; $step = 1; break; case 2: $deck = substr($deck_src, $mid) . substr($deck_src, 0, $mid); $step = 0; break; }
//echo $comb . ' - ' . $deck . '<br>';
$muts = 0;
for ($i = 0; $i < $len; $i++) $muts += intval($deck[$i] === $deck_s[$i]);
if ($muts === $len) break; else { if ($muts > $muts_app) { $muts_app = $muts; $comb_app = $comb; } if (mktime()-$time >= $max_time) { $comb = $comb_app; break; } }
$loops++;
} while (1); }
echo '<html><body><pre>' . str_replace(array('1', '2', '3'), array('S', 'C', 'F'), $comb) . strlen($comb) . '</pre></body></html>'; ?>
Back to results
|
|
|