Main Menu |
|
|
Forums |
|
|
Programming
Contest |
|
|
Documentation
|
|
|
Partner
Sites |
|
|
Sponsors |
|
|
|
Code of jorgen
<?php
/**
* Editor: DzSoft PHP Editor 1.2 BlockIndent: 1, TabStops: 4
* Contest: Flipping Shuffles at www.php-editors.com
* Programmer: Jorgen Horstink, jorgen@webstores.nl
*
* Notes: This script will only return a value if a solution is found
* Last update: April 4th, 19:30 Programming solution
*
* Function list:
* - string sCut(string sDeck) -> Cut a deck of cards
* - string sShuffle(string sDeck) -> Shuffle a deck of cards
* - string sFlip(string sDeck) -> Flip a deck of cards
* - string sSolution(int iLength) -> Generates the string that is the solution
*/
error_reporting(E_ALL); // Report all warnings, errors and notices
set_time_limit(60); // Set the maximum script execution time
ini_set("memory_limit", "1024M"); // Set the maximum memory limit to one gig
/**
* SETTING UP THE ENVIRONNEMENT
*/
$gsFile = "deck.txt"; // Name of the input file
$gaFile = file($gsFile); // Retrieve the content of the input file
$gsDeck = $gaFile[0]; // Get the deck of cards
$gsSolution = sSolution(strlen($gsDeck)); // Generate the string that is the solution
$gbFoundSolution = false; // Have we found the solution?
/**
* CALCULATING THE SOLUTION
*/
$giKey = 0; // Current Key of the Array that will be analysed
$gaDecks[0] = $gsDeck; // Global Array, contains all decks
$gaMoves[0] = "S";
$gaParentMove[0] = 0; // Needed for calculating all moves
if ($gsDeck == $gsSolution) echo "FF2";
while ($gbFoundSolution == false) // While the solution is not found yet...
{
switch ($gaMoves[$giKey]) {
case "C":
$gaDecks[] = sShuffle($gaDecks[$giKey]);
$gaMoves[] = "S";
$gaParentMove[] = $giKey;
if ($gaDecks[$giKey + 1] == $gsSolution) {
$gbFoundSolution = true;
break;
}
$gaDecks[] = sFlip($gaDecks[$giKey]);
$gaMoves[] = "F";
$gaParentMove[] = $giKey;
if ($gaDecks[$giKey + 1] == $gsSolution) {
$gbFoundSolution = true;
break;
}
break;
case "S":
$gaDecks[] = sCut($gaDecks[$giKey]);
$gaMoves[] = "C";
$gaParentMove[] = $giKey;
if ($gaDecks[$giKey + 1] == $gsSolution) {
$gbFoundSolution = true;
break;
}
$gaDecks[] = sShuffle($gaDecks[$giKey]);
$gaMoves[] = "S";
$gaParentMove[] = $giKey;
if ($gaDecks[$giKey + 1] == $gsSolution) {
$gbFoundSolution = true;
break;
}
$gaDecks[] = sFlip($gaDecks[$giKey]);
$gaMoves[] = "F";
$gaParentMove[] = $giKey;
if ($gaDecks[$giKey + 1] == $gsSolution) {
$gbFoundSolution = true;
break;
}
break;
case "F":
$gaDecks[] = sCut($gaDecks[$giKey]);
$gaMoves[] = "C";
$gaParentMove[] = $giKey;
if ($gaDecks[$giKey + 1] == $gsSolution) {
$gbFoundSolution = true;
break;
}
$gaDecks[] = sShuffle($gaDecks[$giKey]);
$gaMoves[] = "S";
$gaParentMove[] = $giKey;
if ($gaDecks[$giKey + 1] == $gsSolution) {
$gbFoundSolution = true;
break;
}
break;
}
$giKey++;
}
$moves = $gaMoves[$giKey]; // Analyse all data and build the Solution String
$parent = $giKey;
while ($parent != 0) {
$parent = $gaParentMove[$parent];
$moves .= $gaMoves[$parent];
}
$gsFinalSolution = strrev(substr($moves, 0, strlen($moves) - 1)); // Skip the last move and reverse the string
$giFinalMoves = strlen($gsFinalSolution); // The num of needed moves
echo $gsFinalSolution . $giFinalMoves; // Display the solution
/* Just for testing the result
for ($i = 0; $i < $giFinalMoves; $i++) {
switch ($gsFinalSolution[$i]) {
case "S":
$gsDeck = sShuffle($gsDeck);
break;
case "F":
$gsDeck = sFlip($gsDeck);
break;
case "C":
$gsDeck = sCut($gsDeck);
}
}
echo "<br>";
echo $gsDeck;
*/
/**
* FUNCTIONS
*/
/* Cut a deck */
function sCut($sDeck) {
return (substr($sDeck, strlen($sDeck) / 2, strlen($sDeck) / 2) . substr($sDeck, 0, strlen($sDeck) / 2));
}
/* Shuffle a deck */
function sShuffle($sDeck) {
$sBuffer = '';
for ($i = 0; $i < (strlen($sDeck) / 2); $i++) {
$sBuffer .= $sDeck[$i + (strlen($sDeck) / 2)] . $sDeck[$i];
}
return ($sBuffer);
}
/* Flip a deck */
function sFlip($sDeck) {
return (strrev($sDeck));
}
/* Generates the string that is the solution */
function sSolution($iLength) {
$sBuffer = '';
for ($i = 0; $i < ($iLength / 2); $i++) {
$sBuffer .= chr(65 + $i);
}
return ($sBuffer . strtolower($sBuffer));
}
?>
Back to results
|
|
|