3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Supply clock values clockwise. // Keys can be anything you want to use to remember the positions. $clock = array( 'a' => 2, 'b' => 1, 'c' => 1, 'd' => 2, 'e' => 3, 'f' => 2, ); $positions = array_keys($clock); $values = array_values($clock); // Test all possible starting positions. for ($i = 0; $i < count($clock); ++$i) { $chain = test_clock($values, $i); // When the solution has all values, it's the right one. if (count($chain) == count($clock)) { break; } } // Use the user-supplied keys. $solution = array(); foreach ($chain as $position) { $solution[] = $positions[$position]; } print 'The solution is: ' . implode($solution, ' ? ') . PHP_EOL; /** * Recursively test the clock based on a supplied position. * * @param array $values * The current values of the clock. * @param integer $i * The current position of the clock. * @param array $chain * The current possible solution. * * @return * An array of positions that represents a possible solution. */ function test_clock(array $values, $i, array $chain = array()) { // If the value of the position we're in is 0, we've already tested it. if ($values[$i] == 0) { return $chain; } // Find the next two positions. $position1 = $i + $values[$i]; $position2 = $i - $values[$i]; // Account for wraparound in the array. if ($position1 > count($values) - 1) { $position1 -= count($values); } if ($position2 < 0) { $position2 += count($values); } // Mark this position as tested. $values[$i] = 0; $chain[] = $i; // Test the first position. $solution = test_clock($values, $position1, $chain); // Don't bother checking the second position if the first is correct. if (count($solution) == count($values)) { return $solution; } // Test the second position. return test_clock($values, $position2, $chain); }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Uncaught TypeError: implode(): Argument #2 ($array) must be of type ?array, string given in /in/YUp2q:33 Stack trace: #0 /in/YUp2q(33): implode(Array, ' ? ') #1 {main} thrown in /in/YUp2q on line 33
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Deprecated: implode(): Passing glue string after array is deprecated. Swap the parameters in /in/YUp2q on line 33 The solution is: a ? e ? b ? c ? d ? f
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33
The solution is: a ? e ? b ? c ? d ? f
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/YUp2q on line 48
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_ARRAY, expecting ')' in /in/YUp2q on line 48
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_ARRAY, expecting ')' in /in/YUp2q on line 48
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/YUp2q on line 48
Process exited with code 255.

preferences:
207.02 ms | 401 KiB | 350 Q