SHARE
TWEET

36 cube alternate solution

a guest Jan 7th, 2011 238 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $colors = array(
  4.          1 => 'R',
  5.          2 => 'O',
  6.          4 => 'Y',
  7.          8 => 'G',
  8.         16 => 'B',
  9.         32 => 'P',
  10.         false => 'X',
  11. );
  12.  
  13. $used_towers = array_fill(0, 6, 0);
  14.  
  15. $cube = array(
  16.         array(1,3,4,5,2,0),
  17.         array(2,5,1,4,1,3),
  18.         array(0,1,3,2,5,4),
  19.         array(5,4,0,3,0,2),
  20.         array(4,2,5,0,3,1),
  21.         array(3,0,2,1,4,5),
  22. );
  23.  
  24. $solution = array_fill(0, 6, array_fill(0, 6, false));
  25. $solutions = array( );
  26.  
  27. function test_color($pos, $solution, $used_towers, $color, $size) {
  28.         if (36 <= $pos) {
  29.                 return false;
  30.         }
  31.  
  32.         // check if piece exists in solution at this location
  33.         if (false !== $solution[floor($pos / 6)][$pos % 6]) {
  34.                 return false;
  35.         }
  36.  
  37.         // check if tower of this size and color has already been used
  38.         if ($color & $used_towers[$size]) {
  39.                 return false;
  40.         }
  41.  
  42.         // check if tower has already been used in row or column:
  43.         for ($i = 0; $i < 6; ++$i) {
  44.                 if ($color == $solution[floor($pos / 6)][$i]) {
  45.                         return false;
  46.                 }
  47.  
  48.                 if ($color == $solution[$i][$pos % 6]) {
  49.                         return false;
  50.                 }
  51.         }
  52.  
  53.         // special conditions for the two special towers
  54.         if (array(1, 2) == (array(floor($pos / 6), $pos % 6)) && (4 != $color)) {
  55.                 return false;
  56.         }
  57.  
  58.         if (array(3, 2) == (array(floor($pos / 6), $pos % 6)) && (2 != $color)) {
  59.                 return false;
  60.         }
  61.  
  62.         return true;
  63. }
  64.  
  65. function print_solution($solution) {
  66.         global $colors, $cube;
  67.  
  68.         echo "<br>\n";
  69.         for ($i = 0; $i < 6; ++$i) {
  70.                 for ($j = 0; $j < 6; ++$j) {
  71.                         echo '('.$colors[$solution[$i][$j]].', '.(6 - $cube[$i][$j]).'), ';
  72.                 }
  73.                 echo "<br>\n";
  74.         }
  75.         echo "<br>\n";
  76. }
  77.  
  78. function solve($pos, $solution, $used_towers, $best, $color) {
  79.         global $colors, $cube;
  80.  
  81.         if (36 == $pos) {
  82.                 return array($best, $solution);
  83.         }
  84.  
  85.         $size = $cube[floor($pos / 6)][$pos % 6];
  86.  
  87.         if (test_color($pos, $solution, $used_towers, $color, $size)) {
  88.                 $used_towers[$size] |= $color;
  89.                 $solution[floor($pos / 6)][$pos % 6] = $color;
  90.  
  91.                 if ($pos > $best) {
  92.                         echo '####### '.($pos + 1).' towers placed';
  93.                         print_solution($solution, $colors, $cube);
  94.                         $best = $pos;
  95.                 }
  96.  
  97.                 list($best, $solution) = solve($pos + 1, $solution, $used_towers, $best, $color);
  98.  
  99.                 if (35 <= $best) {
  100.                         return array($best, $solution);
  101.                 }
  102.  
  103.                 $solution[floor($pos / 6)][$pos % 6] = false;
  104.                 $used_towers[$size] ^= $color;
  105.         }
  106.  
  107.         return array($best, $solution);
  108. }
  109.  
  110. function solve_color($color) {
  111.         global $solution, $used_towers, $colors, $cube;
  112.  
  113.         $orig = $solution;
  114.         $failed = array( );
  115.  
  116.         $i = 0;
  117.         $j = -1;
  118.         while ($i <= 5) {
  119.                 // if we already have a piece in this space (via pre-reqs)
  120.                 // just skip it
  121.                 if (in_array($color, $orig[$i])) {
  122.                         $i++;
  123.                         $j = -1;
  124.                         continue;
  125.                 }
  126.  
  127.                 if ( ! isset($failed[$i])) {
  128.                         $failed[$i] = array( );
  129.                 }
  130.  
  131.                 do {
  132.                         $j++;
  133.                         $size = $cube[$i][$j];
  134.                         // we've already tried this one, try the next space
  135.                 } while (in_array($size, $failed[$i]) && (5 > $j));
  136.  
  137.                 // try putting a piece in the ith row in the last + 1 col
  138.                 if (test_color(($i * 6) + $j, $solution, $used_towers, $color, $size) && ! in_array($size, $failed[$i])) {
  139.                         $solution[$i][$j] = $color;
  140.                         $used_towers[$size] |= $color;
  141.                         $i++;
  142.                         $j = -1;
  143.                         continue;
  144.                 }
  145.  
  146.                 // something failed, go back a row and try a new piece
  147.                 if (5 <= $j) {
  148.                         // backtrack until we find a row that didn't already have a piece in it
  149.                         do {
  150.                                 $i--;
  151.                         } while (in_array($color, $orig[$i]));
  152.  
  153.                         // find out what piece was used here
  154.                         foreach ($solution[$i] as $j => $col) {
  155.                                 if ($color == $col) {
  156.                                         $size = $cube[$i][$j];
  157.                                         $failed[$i][] = $size;
  158.                                         $used_towers[$size] ^= $color;
  159.                                 }
  160.                         }
  161.  
  162.                         $solution[$i] = $orig[$i];
  163.                         $j = -1;
  164.                         continue;
  165.                 }
  166.         }
  167.  
  168.         return $solution;
  169. }
  170.  
  171.  
  172. #$color = 4;
  173. #$solution[1][2] = $color;
  174. #$size = $cube[1][2];
  175. #$used_towers[$size] |= $color;
  176. #debug(solve_color($color));
  177.  
  178.  
  179. function filter_color($used, $color) {
  180.         foreach ($used as & $row) {
  181.                 $row = (bool) ($color & $row);
  182.         }
  183.         unset($row);
  184.  
  185.         return array_filter($used);
  186. }
  187.  
  188.  
  189. function solve_color_by_pieces($color, $solution, $used_towers, $continue_colors = false, $top = false) {
  190.         global $colors, $cube;
  191.  
  192.         $orig = $solution;
  193.  
  194.         $sols = array( );
  195.         while (32 >= $color) {
  196.                 $size = 0;
  197.  
  198.                 while (6 >= count(filter_color($used_towers, $color)) && (5 >= $size)) {
  199.                         if ($color & $used_towers[$size]) {
  200.                                 $size++;
  201.                                 continue;
  202.                         }
  203.  
  204.                         // find ALL possible solutions for this size
  205.                         for ($pos = 0; $pos < 36; ++$pos) {
  206.                                 if ($size != $cube[floor($pos / 6)][$pos % 6]) {
  207.                                         continue;
  208.                                 }
  209.  
  210.                                 if ( ! test_color($pos, $solution, $used_towers, $color, $size)) {
  211.                                         continue;
  212.                                 }
  213.  
  214.                                 // add to this solution path and continue...
  215.                                 $solution[floor($pos / 6)][$pos % 6] = $color;
  216.                                 $used_towers[$size] |= $color;
  217.                                 $return = ((6 > count(filter_color($used_towers, $color))) ? solve_color_by_pieces($color, $solution, $used_towers, $continue_colors) : array('SOLUTION' => $solution));
  218.                                 if ($return) {
  219.                                         $sols[] = $return;
  220.                                 }
  221.  
  222.                                 // remove this solution and continue searching this level
  223.                                 $solution[floor($pos / 6)][$pos % 6] = false;
  224.                                 $used_towers[$size] ^= $color;
  225.                         }
  226.  
  227.                         break;
  228.                 }
  229.  
  230.                 if ($top) {
  231. debug($sols);
  232.                         unset($sols);
  233.                         $sols = array( );
  234.                 }
  235.  
  236.                 if ($continue_colors) {
  237.                         $color << 1;
  238.                 }
  239.                 else {
  240.                         break;
  241.                 }
  242.         }
  243.  
  244.         return $sols;
  245. }
  246.  
  247. function find_sols($sols, $color) {
  248.         foreach ($sols as $key => $sol) {
  249.                 if ('SOLUTION' === $key) {
  250.                         foreach ($sol as $i => $row) {
  251.                                 foreach ($row as $j => $piece) {
  252.                                         if ($color == $piece) {
  253.                                                 echo '$solution['.$i.']['.$j.'] = ';
  254.                                         }
  255.                                 }
  256.                         }
  257.                         echo "<br>\n";
  258.                 }
  259.                 elseif (is_array($sol)) {
  260.                         find_sols($sol, $color);
  261.                 }
  262.         }
  263. }
  264.  
  265.  
  266. #$color = 4;
  267. #$solution[1][2] = $color;
  268. #$size = $cube[1][2];
  269. #$used_towers[$size] |= $color;
  270. #debug(solve_color_by_pieces($color, $solution, $used_towers));
  271.  
  272.  
  273. // set up the yellow in the first of the two solutions and solve for orange
  274. #$solution[0][3] = $solution[1][2] = $solution[2][5] = $solution[3][4] = $solution[4][1] = $solution[5][0] = 4; // yellow
  275. #$color = 1;
  276. #$solution[3][2] = $color;
  277. #$size = $cube[3][2];
  278. #$used_towers[$size] |= $color;
  279. #debug(solve_color_by_pieces($color, $solution, $used_towers));
  280.  
  281. /*
  282.  
  283. Yellow and orange = 2 solutions (both same footprint, so multiply final answer by 2)
  284.  
  285. */
  286.  
  287.  
  288. // solve for red
  289. $solution[0][3] = $solution[1][2] = $solution[2][5] = $solution[3][4] = $solution[4][1] = $solution[5][0] = 4; // yellow
  290. $solution[0][1] = $solution[1][4] = $solution[2][3] = $solution[3][2] = $solution[4][0] = $solution[5][5] = 2; // orange
  291. #$color = 1;
  292. #debug($solutions = solve_color_by_pieces($color, $solution, $used_towers, false));
  293. #find_sols($solutions, $color);
  294. /*
  295.  
  296. Red = 8 solutions
  297.  
  298. */
  299.  
  300. // red solutions
  301. #$solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 1; // A - C,F,H
  302. #$solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 1; // B - D,E,G
  303. #$solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 1; // C - A,F,H
  304. #$solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 1; // D - B,E,G
  305. #$solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 1; // E - B,D,G
  306. #$solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 1; // F - A,C,H
  307. #$solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 1; // G - B,D,E
  308. #$solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 1; // H - A,C,F
  309.  
  310. // solve for green
  311. #$color = 8;
  312. #debug($solutions = solve_color_by_pieces($color, $solution, $used_towers, false));
  313. #find_sols($solutions, $color);
  314. /*
  315.  
  316. Green = 3 solutions each red = 24 solutions
  317.         but there are only 8 unique solutions
  318.                 although when matched up with the 8 red solutions they may make different footprints
  319.  
  320. */
  321.  
  322. // green solutions
  323.  
  324. // A
  325. #$solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 8; // H
  326. #$solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 8; // F
  327. #$solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 8; // C
  328.  
  329. // B
  330. #$solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 8; // G
  331. #$solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 8; // D
  332. #$solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 8; // E
  333.  
  334. // C
  335. #$solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 8; // H
  336. #$solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 8; // F
  337. #$solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 8; // A
  338.  
  339. // D
  340. #$solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 8; // G
  341. #$solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 8; // B
  342. #$solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 8; // E
  343.  
  344. // E
  345. #$solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 8; // G
  346. #$solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 8; // D
  347. #$solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 8; // B
  348.  
  349. // F
  350. #$solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 8; // H
  351. #$solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 8; // A
  352. #$solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 8; // C
  353.  
  354. // G
  355. #$solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 8; // D
  356. #$solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 8; // B
  357. #$solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 8; // E
  358.  
  359. // H
  360. #$solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 8; // F
  361. #$solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 8; // A
  362. #$solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 8; // C
  363.  
  364.  
  365. // unique solutions for green
  366. #$solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 8; // A - H,F,C
  367. #$solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 8; // B - G,E,D
  368. #$solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 8; // C - H,F,A
  369. #$solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 8; // D - G,E,B
  370. #$solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 8; // E - G,D,B
  371. #$solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 8; // F - H,C,A
  372. #$solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 8; // G - E,D,B
  373. #$solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 8; // H - F,C,A
  374.  
  375. // upon further inspection, there are duplicates between the red and green (which should be obvious), which means
  376. // that only half of the solutions are truly unique (the other half just being swapped colors, red and green)
  377. // so 12 solutions total, with the unique solutions as follows:
  378. /*
  379.  
  380. AC = AC - CA = CA
  381. AF = AF - FA = FA
  382. AH = AH - HA = HA
  383.  
  384. BD = BD - DB = DB
  385. BE = BE - EB = EB
  386. BG = BG - GB = GB
  387.  
  388. CF = CF - FC = FC
  389. CH = CH - HC = HC
  390.  
  391. DE = DE - ED = ED
  392. DG = DG - GD = GD
  393.  
  394. EG = EG - GE = GE
  395.  
  396. FH = FH - HF = HF
  397.  
  398. */
  399.  
  400.  
  401. // combo solutions
  402. #$solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 1; // A - C,F,H
  403. #$solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 8; // C - H,F,A
  404.  
  405. #$solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 1; // A - C,F,H
  406. #$solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 8; // F - H,C,A
  407.  
  408. #$solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 1; // A - C,F,H
  409. #$solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 8; // H - F,C,A
  410.  
  411. #$solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 1; // B - D,E,G
  412. #$solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 8; // D - G,E,B
  413.  
  414. #$solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 1; // B - D,E,G
  415. #$solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 8; // E - G,D,B
  416.  
  417. #$solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 1; // B - D,E,G
  418. #$solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 8; // G - E,D,B
  419.  
  420. #$solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 1; // C - A,F,H
  421. #$solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 8; // F - H,C,A
  422.  
  423. #$solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 1; // C - A,F,H
  424. #$solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 8; // H - F,C,A
  425.  
  426. #$solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 1; // D - B,E,G
  427. #$solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 8; // E - G,D,B
  428.  
  429. #$solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 1; // D - B,E,G
  430. #$solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 8; // G - E,D,B
  431.  
  432. #$solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 1; // E - B,D,G
  433. #$solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 8; // G - E,D,B
  434.  
  435. #$solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 1; // 3 - A,C,H
  436. #$solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 8; // H - F,C,A
  437.  
  438. // solve for blue
  439. #$color = 16;
  440. #debug($solutions = solve_color_by_pieces($color, $solution, $used_towers, false));
  441. #find_sols($solutions, $color);
  442.  
  443. /*
  444.  
  445. Blue = 2 solutions each for a total of 24 as follows:
  446.  
  447. AC
  448. $solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 16; // H
  449. $solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 16; // F
  450.  
  451. AF
  452. $solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 16; // H
  453. $solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 16; // C
  454.  
  455. AH
  456. $solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 16; // F
  457. $solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 16; // C
  458.  
  459. BD
  460. $solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 16; // G
  461. $solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 16; // E
  462.  
  463. BE
  464. $solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 16; // G
  465. $solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 16; // D
  466.  
  467. BG
  468. $solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 16; // D
  469. $solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 16; // E
  470.  
  471. CF
  472. $solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 16; // H
  473. $solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 16; // A
  474.  
  475. CH
  476. $solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 16; // F
  477. $solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 16; // A
  478.  
  479. DE
  480. $solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 16; // G
  481. $solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 16; // B
  482.  
  483. DG
  484. $solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 16; // B
  485. $solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 16; // E
  486.  
  487. EG
  488. $solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 16; // D
  489. $solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 16; // B
  490.  
  491. FH
  492. $solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 16; // A
  493. $solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 16; // C
  494.  
  495.  
  496. only 8 of the solutions are unique though
  497.  
  498. $solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 16; // A - FH,CH,CF
  499. $solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 16; // B - EG,DG,DE
  500. $solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 16; // C - FH,AH,AF
  501. $solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 16; // D - EG,BG,BE
  502. $solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 16; // E - DG,BG,BD
  503. $solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 16; // F - CH,AH,AC
  504. $solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 16; // G - DE,BE,BD
  505. $solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 16; // H - CF,AF,AC
  506.  
  507.  
  508. and when combined with the other two sets, they combine to create only 8 unique solutions
  509.  
  510. AFH - FAH - HAF
  511. ACH - CAH - HAC
  512. ACF - CAF - FAC
  513.  
  514. BEG - EBG - GBE
  515. BDG - DBG - GBD
  516. BDE - DBE - EBD
  517.  
  518. CFH - FCH - HCF
  519.  
  520. DEG - EDG - GDE
  521.  
  522. */
  523.  
  524. // AFH
  525. #$solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 1; // A - C,F,H
  526. #$solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 8; // F - H,C,A
  527. #$solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 16; // H - CF,AF,AC
  528.  
  529. // ACH
  530. #$solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 1; // A - C,F,H
  531. #$solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 8; // C - H,F,A
  532. #$solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 16; // H - CF,AF,AC
  533.  
  534. // ACF
  535. #$solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 1; // A - C,F,H
  536. #$solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 8; // C - H,F,A
  537. #$solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 16; // F - CH,AH,AC
  538.  
  539. // BEG
  540. #$solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 1; // B - D,E,G
  541. #$solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 8; // E - G,D,B
  542. #$solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 16; // G - DE,BE,BD
  543.  
  544. // BDG
  545. #$solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 1; // B - D,E,G
  546. #$solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 8; // D - G,E,B
  547. #$solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 16; // G - DE,BE,BD
  548.  
  549. // BDE
  550. #$solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 1; // B - D,E,G
  551. #$solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 8; // D - G,E,B
  552. #$solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 16; // E - DG,BG,BD
  553.  
  554. // CFH
  555. #$solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 1; // C - A,F,H
  556. #$solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 8; // F - H,C,A
  557. #$solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 16; // H - CF,AF,AC
  558.  
  559. // DEG
  560. $solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 1; // D - B,E,G
  561. $solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 8; // E - G,D,B
  562. $solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 16; // G - DE,BE,BD
  563.  
  564.  
  565. // solve for purple
  566. $color = 32;
  567. debug($solutions = solve_color_by_pieces($color, $solution, $used_towers, false));
  568. find_sols($solutions, $color);
  569.  
  570. /*
  571.  
  572. purple solutions
  573.  
  574. obviously, there will only be one solution for each set
  575.  
  576. AFH
  577. $solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 32; // C
  578.  
  579. ACH
  580. $solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 32; // F
  581.  
  582. ACF
  583. $solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 32; // H
  584.  
  585. BEG
  586. $solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 32; // D
  587.  
  588. BDG
  589. $solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 32; // E
  590.  
  591. BDE
  592. $solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 32; // G
  593.  
  594. CFH
  595. $solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 32; // A
  596.  
  597. DEG
  598. $solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 32; // B
  599.  
  600.  
  601. and each solution is unique
  602.  
  603. $solution[0][0] = $solution[1][1] = $solution[2][2] = $solution[3][5] = $solution[4][3] = $solution[5][4] = 32; // A - CFH
  604. $solution[0][0] = $solution[1][5] = $solution[2][4] = $solution[3][1] = $solution[4][3] = $solution[5][2] = 32; // B - DEG
  605. $solution[0][2] = $solution[1][0] = $solution[2][4] = $solution[3][3] = $solution[4][5] = $solution[5][1] = 32; // C - AFH
  606. $solution[0][2] = $solution[1][1] = $solution[2][0] = $solution[3][5] = $solution[4][4] = $solution[5][3] = 32; // D - BEG
  607. $solution[0][4] = $solution[1][3] = $solution[2][2] = $solution[3][0] = $solution[4][5] = $solution[5][1] = 32; // E - BDG
  608. $solution[0][4] = $solution[1][5] = $solution[2][0] = $solution[3][1] = $solution[4][2] = $solution[5][3] = 32; // F - ACH
  609. $solution[0][5] = $solution[1][0] = $solution[2][1] = $solution[3][3] = $solution[4][2] = $solution[5][4] = 32; // G - BDE
  610. $solution[0][5] = $solution[1][3] = $solution[2][1] = $solution[3][0] = $solution[4][4] = $solution[5][2] = 32; // H - ACF
  611.  
  612.  
  613. and when taken as a whole, there are only 2 solutions (with all others being color swaps)
  614.  
  615. ACFH - CAFH - FACH - HACF
  616.  
  617. BDEG - DBEG - EBDG - GBDE
  618.  
  619. and when multiplied by the original 2 yellow and orange solutions, that leaves us with 4 unique solutions.
  620.  
  621. QED
  622. */
  623.  
  624.  
  625. /** function call [dump] [debug]
  626.  *              This function is for debugging only
  627.  *              Outputs given var to screen
  628.  *              or, if no var given, outputs stars to note position
  629.  *
  630.  * @param mixed optional var to output
  631.  * @param bool optional bypass debug value and output anyway
  632.  * @action outputs var to screen
  633.  * @return void
  634.  */
  635. function call($var = 'Th&F=xUFucreSp2*ezAhe=ApuPR*$axe', $bypass = false, $show_from = true, $new_window = false, $error = false)
  636. {
  637.         if ((( ! defined('DEBUG') || ! DEBUG) || ! empty($GLOBALS['NODEBUG'])) && ! (bool) $bypass) {
  638.                 return false;
  639.         }
  640.  
  641.         if ('Th&F=xUFucreSp2*ezAhe=ApuPR*$axe' === $var) {
  642.                 $contents = '<span style="font-size:larger;font-weight:bold;color:red;">--==((OO88OO))==--</span>';
  643.         }
  644.         else {
  645.                 // begin output buffering so we can escape any html
  646.                 // and print_r is better at catching recursion than var_export
  647.                 ob_start( );
  648.  
  649.                 if ((is_string($var) && ! preg_match('/^\\s*$/', $var))) { // non-whitespace strings
  650.                         print_r($var);
  651.                 }
  652.                 else {
  653.                         if ( ! function_exists('xdebug_disable')) {
  654.                                 if (is_array($var) || is_object($var)) {
  655.                                         print_r($var);
  656.                                 }
  657.                                 else {
  658.                                         var_dump($var);
  659.                                 }
  660.                         }
  661.                         else {
  662.                                 var_dump($var);
  663.                         }
  664.                 }
  665.  
  666.                 // end output buffering and output the result
  667.                 if ( ! function_exists('xdebug_disable')) {
  668.                         $contents = htmlentities(ob_get_contents( ));
  669.                 }
  670.                 else {
  671.                         $contents = ob_get_contents( );
  672.                 }
  673.  
  674.                 ob_end_clean( );
  675.         }
  676.  
  677.         $j = 0;
  678.         $html = '';
  679.         $debug_funcs = array('dump', 'debug');
  680.         if ((bool) $show_from) {
  681.                 $called_from = debug_backtrace( );
  682.  
  683.                 if (isset($called_from[$j + 1]) && in_array($called_from[$j + 1]['function'], $debug_funcs)) {
  684.                         ++$j;
  685.                 }
  686.  
  687.                 $file0 = substr($called_from[$j]['file'], strlen($_SERVER['DOCUMENT_ROOT']));
  688.                 $line0 = $called_from[$j]['line'];
  689.  
  690.                 $called = '';
  691.                 if (isset($called_from[$j + 1]['file'])) {
  692.                         $file1 = substr($called_from[$j + 1]['file'], strlen($_SERVER['DOCUMENT_ROOT']));
  693.                         $line1 = $called_from[$j + 1]['line'];
  694.                         $called = "{$file1} : {$line1} called ";
  695.                 }
  696.                 elseif (isset($called_from[$j + 1]['class'])) {
  697.                         $called = $called_from[$j + 1]['class'].$called_from[$j + 1]['type'].$called_from[$j + 1]['function'].' called ';
  698.                 }
  699.  
  700.                 $html = "<strong>{$called}{$file0} : {$line0}</strong>\n";
  701.         }
  702.  
  703.         if ( ! $new_window) {
  704.                 $color = '#000';
  705.                 if ($error) {
  706.                         $color = '#F00';
  707.                 }
  708.  
  709.                 echo "\n\n<pre style=\"background:#FFF;color:{$color};font-size:larger;\">{$html}{$contents}\n<hr /></pre>\n\n";
  710.         }
  711.         else { ?>
  712.                 <script language="javascript">
  713.                         myRef = window.open('','debugWindow');
  714.                         myRef.document.write('\n\n<pre style="background:#FFF;color:#000;font-size:larger;">');
  715.                         myRef.document.write('<?php echo str_replace("'", "\'", str_replace("\n", "<br />", "{$html}{$contents}")); ?>');
  716.                         myRef.document.write('\n<hr /></pre>\n\n');
  717.                 </script>
  718.         <?php }
  719. }
  720. 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); }
  721. 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); }
RAW Paste Data
Want to get better at PHP?
Learn to code PHP in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top