Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. 2
  2. 2 2
  3. 3 1
  4. 1
  5.  
  6. A | B | C | D | E | F
  7. --------------------------
  8. A x | x | o | x | o | x
  9. B o | x | o | x | x | x
  10. C x | o | x | o | x | x
  11. D x | o | x | x | x | o
  12. E x | o | x | x | x | x
  13. F x | o | x | o | x | x
  14.  
  15. <?php
  16.  
  17. // Supply clock values clockwise.
  18. // Keys can be anything you want to use to remember the positions.
  19. $clock = array(
  20. 'a' => 2,
  21. 'b' => 1,
  22. 'c' => 1,
  23. 'd' => 2,
  24. 'e' => 3,
  25. 'f' => 2,
  26. );
  27.  
  28. $positions = array_keys($clock);
  29. $values = array_values($clock);
  30.  
  31. // Test all possible starting positions.
  32. for ($i = 0; $i < count($clock); ++$i) {
  33. $chain = test_clock($values, $i);
  34.  
  35. // When the solution has all values, it's the right one.
  36. if (count($chain) == count($clock)) {
  37. break;
  38. }
  39. }
  40.  
  41. // Use the user-supplied keys.
  42. $solution = array();
  43. foreach ($chain as $position) {
  44. $solution[] = $positions[$position];
  45. }
  46.  
  47. print 'The solution is: ' . implode($solution, ' → ') . PHP_EOL;
  48.  
  49. /**
  50. * Recursively test the clock based on a supplied position.
  51. *
  52. * @param array $values
  53. * The current values of the clock.
  54. * @param integer $i
  55. * The current position of the clock.
  56. * @param array $chain
  57. * The current possible solution.
  58. *
  59. * @return
  60. * An array of positions that represents a possible solution.
  61. */
  62. function test_clock(array $values, $i, array $chain = array()) {
  63. // If the value of the position we're in is 0, we've already tested it.
  64. if ($values[$i] == 0) {
  65. return $chain;
  66. }
  67.  
  68. // Find the next two positions.
  69. $position1 = $i + $values[$i];
  70. $position2 = $i - $values[$i];
  71.  
  72. // Account for wraparound in the array.
  73. if ($position1 > count($values) - 1) {
  74. $position1 -= count($values);
  75. }
  76. if ($position2 < 0) {
  77. $position2 += count($values);
  78. }
  79.  
  80. // Mark this position as tested.
  81. $values[$i] = 0;
  82. $chain[] = $i;
  83.  
  84. // Test the first position.
  85. $solution = test_clock($values, $position1, $chain);
  86.  
  87. // Don't bother checking the second position if the first is correct.
  88. if (count($solution) == count($values)) {
  89. return $solution;
  90. }
  91.  
  92. // Test the second position.
  93. return test_clock($values, $position2, $chain);
  94. }
  95.  
  96. $ php hands-of-time.php
  97. The solution is: a → e → b → c → d → f
  98.  
  99. $clock = array(
  100. 'a' => 2,
  101. 'b' => 2,
  102. 'c' => 2,
  103. 'd' => 4,
  104. 'e' => 2,
  105. 'f' => 2,
  106. 'g' => 3,
  107. 'h' => 3,
  108. 'i' => 6,
  109. 'j' => 4,
  110. 'k' => 5,
  111. 'l' => 3,
  112. );
  113.  
  114. The solution is: a → k → f → h → e → g → j → b → d → l → i → c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement