Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $colors = array(
- 1 => 'R',
- 2 => 'O',
- 4 => 'Y',
- 8 => 'G',
- 16 => 'B',
- 32 => 'P',
- false => 'X',
- );
- $used_towers = array_fill(0, 6, 0);
- $cube = array(
- array(1,3,4,5,2,0),
- array(2,5,1,4,1,3),
- array(0,1,3,2,5,4),
- array(5,4,0,3,0,2),
- array(4,2,5,0,3,1),
- array(3,0,2,1,4,5),
- );
- $solution = array_fill(0, 6, array_fill(0, 6, false));
- $solutions = array( );
- function test_color($pos, $solution, $used_towers, $color, $size) {
- if (36 <= $pos) {
- return false;
- }
- // check if piece exists in solution at this location
- if (false !== $solution[floor($pos / 6)][$pos % 6]) {
- return false;
- }
- // check if tower of this size and color has already been used
- if ($color & $used_towers[$size]) {
- return false;
- }
- // check if tower has already been used in row or column:
- for ($i = 0; $i < 6; ++$i) {
- if ($color == $solution[floor($pos / 6)][$i]) {
- return false;
- }
- if ($color == $solution[$i][$pos % 6]) {
- return false;
- }
- }
- // special conditions for the two special towers
- if (array(1, 2) == (array(floor($pos / 6), $pos % 6)) && (4 != $color)) {
- return false;
- }
- if (array(3, 2) == (array(floor($pos / 6), $pos % 6)) && (2 != $color)) {
- return false;
- }
- return true;
- }
- function filter_color($used, $color) {
- foreach ($used as & $row) {
- $row = (bool) ($color & $row);
- }
- unset($row);
- return array_filter($used);
- }
- function solve_color_by_pieces($color, $solution, $used_towers, $continue_colors = false) {
- global $colors, $cube, $MASTER;
- $orig = $solution;
- if (32 < $color) {
- // convert all numbers to letters based on position and flatten
- list($converted, $key) = convert_solution($solution);
- if ( ! in_array($converted, $MASTER['solution'])) {
- $MASTER['solution'][] = $converted;
- $MASTER['key'][] = $key;
- }
- return $solution;
- }
- if (6 <= count(filter_color($used_towers, $color))) {
- return solve_color_by_pieces($color << 1, $solution, $used_towers, $continue_colors);;
- }
- $sols = array( );
- $size = 0;
- while (5 >= $size) {
- if ($color & $used_towers[$size]) {
- $size++;
- continue;
- }
- // find ALL possible solutions for this size
- for ($pos = 0; $pos < 36; ++$pos) {
- if ($size != $cube[floor($pos / 6)][$pos % 6]) {
- continue;
- }
- if ( ! test_color($pos, $solution, $used_towers, $color, $size)) {
- continue;
- }
- // add to this solution path and continue...
- $solution[floor($pos / 6)][$pos % 6] = $color;
- $used_towers[$size] |= $color;
- // if we're not done with this color
- if (6 > count(filter_color($used_towers, $color))) {
- $return = solve_color_by_pieces($color, $solution, $used_towers, $continue_colors);
- }
- elseif ($continue_colors) {
- $return = solve_color_by_pieces($color << 1, $solution, $used_towers, $continue_colors);
- }
- else {
- $return = $solution;
- }
- if ($return) {
- $sols[] = $return;
- }
- // remove this solution and continue searching this level
- $solution[floor($pos / 6)][$pos % 6] = false;
- $used_towers[$size] ^= $color;
- }
- break;
- }
- return $sols;
- }
- function convert_solution($sol) {
- global $colors;
- $letters = array('A','B','C','D','E','F');
- $output = '';
- foreach ($sol as $i => & $row) {
- foreach ($row as $j => & $piece) {
- if ( ! $i) {
- $conv[$piece] = $letters[$j];
- }
- $output .= $conv[$piece];
- }
- }
- $key = array( );
- foreach ($conv as $color => $letter) {
- $key[$letter] = $colors[$color];
- }
- return array($output, $key);
- }
- // solve for red
- $solution[1][2] = 4; // yellow
- $size = $cube[1][2];
- $used_towers[$size] |= 4;
- $solution[3][2] = 2; // orange
- $size = $cube[3][2];
- $used_towers[$size] |= 2;
- $color = 1;
- $MASTER = array('solution' => array( ), 'key' => array( ));
- solve_color_by_pieces($color, $solution, $used_towers, true);
- debug($MASTER);
- /** function call [dump] [debug]
- * This function is for debugging only
- * Outputs given var to screen
- * or, if no var given, outputs stars to note position
- *
- * @param mixed optional var to output
- * @param bool optional bypass debug value and output anyway
- * @action outputs var to screen
- * @return void
- */
- function call($var = 'Th&F=xUFucreSp2*ezAhe=ApuPR*$axe', $bypass = false, $show_from = true, $new_window = false, $error = false)
- {
- if ((( ! defined('DEBUG') || ! DEBUG) || ! empty($GLOBALS['NODEBUG'])) && ! (bool) $bypass) {
- return false;
- }
- if ('Th&F=xUFucreSp2*ezAhe=ApuPR*$axe' === $var) {
- $contents = '<span style="font-size:larger;font-weight:bold;color:red;">--==((OO88OO))==--</span>';
- }
- else {
- // begin output buffering so we can escape any html
- // and print_r is better at catching recursion than var_export
- ob_start( );
- if ((is_string($var) && ! preg_match('/^\\s*$/', $var))) { // non-whitespace strings
- print_r($var);
- }
- else {
- if ( ! function_exists('xdebug_disable')) {
- if (is_array($var) || is_object($var)) {
- print_r($var);
- }
- else {
- var_dump($var);
- }
- }
- else {
- var_dump($var);
- }
- }
- // end output buffering and output the result
- if ( ! function_exists('xdebug_disable')) {
- $contents = htmlentities(ob_get_contents( ));
- }
- else {
- $contents = ob_get_contents( );
- }
- ob_end_clean( );
- }
- $j = 0;
- $html = '';
- $debug_funcs = array('dump', 'debug');
- if ((bool) $show_from) {
- $called_from = debug_backtrace( );
- if (isset($called_from[$j + 1]) && in_array($called_from[$j + 1]['function'], $debug_funcs)) {
- ++$j;
- }
- $file0 = substr($called_from[$j]['file'], strlen($_SERVER['DOCUMENT_ROOT']));
- $line0 = $called_from[$j]['line'];
- $called = '';
- if (isset($called_from[$j + 1]['file'])) {
- $file1 = substr($called_from[$j + 1]['file'], strlen($_SERVER['DOCUMENT_ROOT']));
- $line1 = $called_from[$j + 1]['line'];
- $called = "{$file1} : {$line1} called ";
- }
- elseif (isset($called_from[$j + 1]['class'])) {
- $called = $called_from[$j + 1]['class'].$called_from[$j + 1]['type'].$called_from[$j + 1]['function'].' called ';
- }
- $html = "<strong>{$called}{$file0} : {$line0}</strong>\n";
- }
- if ( ! $new_window) {
- $color = '#000';
- if ($error) {
- $color = '#F00';
- }
- echo "\n\n<pre style=\"background:#FFF;color:{$color};font-size:larger;\">{$html}{$contents}\n<hr /></pre>\n\n";
- }
- else { ?>
- <script language="javascript">
- myRef = window.open('','debugWindow');
- myRef.document.write('\n\n<pre style="background:#FFF;color:#000;font-size:larger;">');
- myRef.document.write('<?php echo str_replace("'", "\'", str_replace("\n", "<br />", "{$html}{$contents}")); ?>');
- myRef.document.write('\n<hr /></pre>\n\n');
- </script>
- <?php }
- }
- function dump($var = 'Th&F=xUFucreSp2*ezAhe=ApuPR*$axe', $bypass = false, $show_from = true, $new_window = false, $error = false) { call($var, $bypass, $show_from, $new_window, $error); }
- function debug($var = 'Th&F=xUFucreSp2*ezAhe=ApuPR*$axe', $bypass = true, $show_from = true, $new_window = false, $error = false) { call($var, $bypass, $show_from, $new_window, $error); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement