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' => 3, 'b' => 2, 'c' => 5, 'd' => 2, 'e' => 4, 'f' => 4, 'g' => 2, 'h' => 3, 'i' => 4, 'j' => 3, ); $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); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 11
Branch analysis from position: 25
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 32
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 32
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 11
Branch analysis from position: 25
Branch analysis from position: 11
filename:       /in/Ub6ao
function name:  (null)
number of ops:  41
compiled vars:  !0 = $clock, !1 = $positions, !2 = $values, !3 = $i, !4 = $chain, !5 = $solution, !6 = $position
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ASSIGN                                                   !0, <array>
   18     1        INIT_FCALL                                               'array_keys'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $8      
          4        ASSIGN                                                   !1, $8
   19     5        INIT_FCALL                                               'array_values'
          6        SEND_VAR                                                 !0
          7        DO_ICALL                                         $10     
          8        ASSIGN                                                   !2, $10
   22     9        ASSIGN                                                   !3, 0
         10      > JMP                                                      ->22
   23    11    >   INIT_FCALL_BY_NAME                                       'test_clock'
         12        SEND_VAR_EX                                              !2
         13        SEND_VAR_EX                                              !3
         14        DO_FCALL                                      0  $13     
         15        ASSIGN                                                   !4, $13
   26    16        COUNT                                            ~15     !4
         17        COUNT                                            ~16     !0
         18        IS_EQUAL                                                 ~15, ~16
         19      > JMPZ                                                     ~17, ->21
   27    20    > > JMP                                                      ->25
   22    21    >   PRE_INC                                                  !3
         22    >   COUNT                                            ~19     !0
         23        IS_SMALLER                                               !3, ~19
         24      > JMPNZ                                                    ~20, ->11
   32    25    >   ASSIGN                                                   !5, <array>
   33    26      > FE_RESET_R                                       $22     !4, ->32
         27    > > FE_FETCH_R                                               $22, !6, ->32
   34    28    >   FETCH_DIM_R                                      ~24     !1, !6
         29        ASSIGN_DIM                                               !5
         30        OP_DATA                                                  ~24
   33    31      > JMP                                                      ->27
         32    >   FE_FREE                                                  $22
   37    33        INIT_FCALL                                               'implode'
         34        SEND_VAR                                                 !5
         35        SEND_VAL                                                 '+%E2%86%92+'
         36        DO_ICALL                                         $25     
         37        CONCAT                                           ~26     'The+solution+is%3A+', $25
         38        CONCAT                                           ~27     ~26, '%0A'
         39        ECHO                                                     ~27
   84    40      > RETURN                                                   1

Function test_clock:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 23
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 38
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
Branch analysis from position: 19
filename:       /in/Ub6ao
function name:  test_clock
number of ops:  45
compiled vars:  !0 = $values, !1 = $i, !2 = $chain, !3 = $position1, !4 = $position2, !5 = $solution
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      <array>
   54     3        FETCH_DIM_R                                      ~6      !0, !1
          4        IS_EQUAL                                                 ~6, 0
          5      > JMPZ                                                     ~7, ->7
   55     6    > > RETURN                                                   !2
   59     7    >   FETCH_DIM_R                                      ~8      !0, !1
          8        ADD                                              ~9      !1, ~8
          9        ASSIGN                                                   !3, ~9
   60    10        FETCH_DIM_R                                      ~11     !0, !1
         11        SUB                                              ~12     !1, ~11
         12        ASSIGN                                                   !4, ~12
   63    13        COUNT                                            ~14     !0
         14        SUB                                              ~15     ~14, 1
         15        IS_SMALLER                                               ~15, !3
         16      > JMPZ                                                     ~16, ->19
   64    17    >   COUNT                                            ~17     !0
         18        ASSIGN_OP                                     2          !3, ~17
   66    19    >   IS_SMALLER                                               !4, 0
         20      > JMPZ                                                     ~19, ->23
   67    21    >   COUNT                                            ~20     !0
         22        ASSIGN_OP                                     1          !4, ~20
   71    23    >   ASSIGN_DIM                                               !0, !1
         24        OP_DATA                                                  0
   72    25        ASSIGN_DIM                                               !2
         26        OP_DATA                                                  !1
   75    27        INIT_FCALL_BY_NAME                                       'test_clock'
         28        SEND_VAR_EX                                              !0
         29        SEND_VAR_EX                                              !3
         30        SEND_VAR_EX                                              !2
         31        DO_FCALL                                      0  $24     
         32        ASSIGN                                                   !5, $24
   78    33        COUNT                                            ~26     !5
         34        COUNT                                            ~27     !0
         35        IS_EQUAL                                                 ~26, ~27
         36      > JMPZ                                                     ~28, ->38
   79    37    > > RETURN                                                   !5
   83    38    >   INIT_FCALL_BY_NAME                                       'test_clock'
         39        SEND_VAR_EX                                              !0
         40        SEND_VAR_EX                                              !4
         41        SEND_VAR_EX                                              !2
         42        DO_FCALL                                      0  $29     
         43      > RETURN                                                   $29
   84    44*     > RETURN                                                   null

End of function test_clock

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
200.91 ms | 1404 KiB | 19 Q