3v4l.org

run code in 300+ PHP versions simultaneously
<?php function matrix_brute_solve(array $matrix) { matrix_make_square($matrix); foreach (matrix_generate_vectors($matrix) as $vector) { $attempt = matrix_rotate($matrix, $vector); if (matrix_is_solved($attempt)) return matrix_display($attempt); } echo 'No solution'; } function matrix_make_square(array &$matrix) { $pad = count(array_keys($matrix)); foreach ($matrix as &$row) $row = array_pad($row, $pad, ''); } function matrix_generate_vectors(array $matrix) { $vectors = []; $columns = count(current($matrix)); $gen = function ($depth=0, $combo='') use (&$gen, &$vectors, $columns) { if ($depth < $columns) for ($i = 0; $i < $columns; $i++) $gen($depth + 1, $i . $combo); else $vectors[] = array_map('intval', str_split($combo)); }; $gen(); return $vectors; } function matrix_rotate(array $matrix, array $vector) { foreach ($matrix as $row => &$values) { array_rotate($values, $vector[$row]); } return $matrix; } function matrix_is_solved(array $matrix) { foreach (array_keys(current($matrix)) as $offset) { $column = array_filter($raw = array_column($matrix, $offset)); if (count($column) != count(array_unique($column))) return false; } return true; } function array_rotate(array &$array, $offset) { foreach (array_slice($array, 0, $offset) as $key => $val) { unset($array[$key]); $array[$key] = $val; } $array = array_values($array); } function matrix_display(array $matrix = null) { echo "[\n"; foreach ($matrix as $row => $inner) { echo " $row => ['" . implode("', '", $inner) . "']\n"; } echo "]\n"; } $tests = [ [ ['X'], ['X'] ], [ ['X'], ['X'], ['X'] ], [ [ 'X', '' ], [ '', 'X' ] ], [ ['X', 'Y', 'Z'], ['X', 'Y'], ['X']], [ ['X', 'Y'], ['X', 'Y'], ['X', 'Y'] ], [ ['X', 'Y', 'Z'], ['X', 'Y', 'Z'], ['X', 'Y', 'Z'] ], [ ['X', 'Y', 'Z', 'I', 'J'], ['X', 'Y', 'Z', 'I'], ['X', 'Y', 'Z', 'I'], ['X', 'Y', 'Z', 'I'], ['X', 'Y', 'Z'], ['X', 'Y', 'Z'] ], ]; array_map(function ($matrix) { matrix_display($matrix); echo "solved by:" . PHP_EOL; matrix_brute_solve($matrix); echo PHP_EOL; }, $tests);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mLIlQ
function name:  (null)
number of ops:  7
compiled vars:  !0 = $tests
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   ASSIGN                                                   !0, <array>
   73     1        INIT_FCALL                                               'array_map'
          2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FmLIlQ%3A73%241'
   78     3        SEND_VAL                                                 ~2
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                                 
          6      > RETURN                                                   1

Function matrix_brute_solve:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 24
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 24
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 23
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/mLIlQ
function name:  matrix_brute_solve
number of ops:  27
compiled vars:  !0 = $matrix, !1 = $vector, !2 = $attempt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    4     1        INIT_FCALL_BY_NAME                                       'matrix_make_square'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
    5     4        INIT_FCALL_BY_NAME                                       'matrix_generate_vectors'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $4      
          7      > FE_RESET_R                                       $5      $4, ->24
          8    > > FE_FETCH_R                                               $5, !1, ->24
    6     9    >   INIT_FCALL_BY_NAME                                       'matrix_rotate'
         10        SEND_VAR_EX                                              !0
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0  $6      
         13        ASSIGN                                                   !2, $6
    7    14        INIT_FCALL_BY_NAME                                       'matrix_is_solved'
         15        SEND_VAR_EX                                              !2
         16        DO_FCALL                                      0  $8      
         17      > JMPZ                                                     $8, ->23
    8    18    >   INIT_FCALL_BY_NAME                                       'matrix_display'
         19        SEND_VAR_EX                                              !2
         20        DO_FCALL                                      0  $9      
         21        FE_FREE                                                  $5
         22      > RETURN                                                   $9
    5    23    > > JMP                                                      ->8
         24    >   FE_FREE                                                  $5
   10    25        ECHO                                                     'No+solution'
   11    26      > RETURN                                                   null

End of function matrix_brute_solve

Function matrix_make_square:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
2 jumps found. (Code = 126) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/mLIlQ
function name:  matrix_make_square
number of ops:  17
compiled vars:  !0 = $matrix, !1 = $pad, !2 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        INIT_FCALL                                               'array_keys'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $3      
          4        COUNT                                            ~4      $3
          5        ASSIGN                                                   !1, ~4
   15     6      > FE_RESET_RW                                      $6      !0, ->15
          7    > > FE_FETCH_RW                                              $6, !2, ->15
   16     8    >   INIT_FCALL                                               'array_pad'
          9        SEND_VAR                                                 !2
         10        SEND_VAR                                                 !1
         11        SEND_VAL                                                 ''
         12        DO_ICALL                                         $7      
         13        ASSIGN                                                   !2, $7
   15    14      > JMP                                                      ->7
         15    >   FE_FREE                                                  $6
   17    16      > RETURN                                                   null

End of function matrix_make_square

Function matrix_generate_vectors:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mLIlQ
function name:  matrix_generate_vectors
number of ops:  16
compiled vars:  !0 = $matrix, !1 = $vectors, !2 = $columns, !3 = $gen
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   20     1        ASSIGN                                                   !1, <array>
   21     2        INIT_FCALL                                               'current'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $5      
          5        COUNT                                            ~6      $5
          6        ASSIGN                                                   !2, ~6
   22     7        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FmLIlQ%3A22%240'
          8        BIND_LEXICAL                                             ~8, !3
          9        BIND_LEXICAL                                             ~8, !1
         10        BIND_LEXICAL                                             ~8, !2
         11        ASSIGN                                                   !3, ~8
   29    12        INIT_DYNAMIC_CALL                                        !3
         13        DO_FCALL                                      0          
   30    14      > RETURN                                                   !1
   31    15*     > RETURN                                                   null

End of function matrix_generate_vectors

Function %00%7Bclosure%7D%2Fin%2FmLIlQ%3A22%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 19
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 9
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 9
Branch analysis from position: 18
Branch analysis from position: 9
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mLIlQ
function name:  {closure}
number of ops:  29
compiled vars:  !0 = $depth, !1 = $combo, !2 = $gen, !3 = $vectors, !4 = $columns, !5 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV_INIT                                        !0      0
          1        RECV_INIT                                        !1      ''
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
          4        BIND_STATIC                                              !4
   23     5        IS_SMALLER                                               !0, !4
          6      > JMPZ                                                     ~6, ->19
   24     7    >   ASSIGN                                                   !5, 0
          8      > JMP                                                      ->16
   25     9    >   INIT_DYNAMIC_CALL                                        !2
         10        ADD                                              ~8      !0, 1
         11        SEND_VAL_EX                                              ~8
         12        CONCAT                                           ~9      !5, !1
         13        SEND_VAL_EX                                              ~9
         14        DO_FCALL                                      0          
   24    15        PRE_INC                                                  !5
         16    >   IS_SMALLER                                               !5, !4
         17      > JMPNZ                                                    ~12, ->9
         18    > > JMP                                                      ->28
   27    19    >   INIT_FCALL                                               'array_map'
         20        SEND_VAL                                                 'intval'
         21        INIT_FCALL                                               'str_split'
         22        SEND_VAR                                                 !1
         23        DO_ICALL                                         $14     
         24        SEND_VAR                                                 $14
         25        DO_ICALL                                         $15     
         26        ASSIGN_DIM                                               !3
         27        OP_DATA                                                  $15
   28    28    > > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FmLIlQ%3A22%240

Function matrix_rotate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
2 jumps found. (Code = 126) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/mLIlQ
function name:  matrix_rotate
number of ops:  15
compiled vars:  !0 = $matrix, !1 = $vector, !2 = $values, !3 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   34     2      > FE_RESET_RW                                      $4      !0, ->12
          3    > > FE_FETCH_RW                                      ~5      $4, !2, ->12
          4    >   ASSIGN                                                   !3, ~5
   35     5        INIT_FCALL_BY_NAME                                       'array_rotate'
          6        SEND_VAR_EX                                              !2
          7        CHECK_FUNC_ARG                                           
          8        FETCH_DIM_FUNC_ARG                               $7      !1, !3
          9        SEND_FUNC_ARG                                            $7
         10        DO_FCALL                                      0          
   34    11      > JMP                                                      ->3
         12    >   FE_FREE                                                  $4
   37    13      > RETURN                                                   !0
   38    14*     > RETURN                                                   null

End of function matrix_rotate

Function matrix_is_solved:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 28
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 28
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 27
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/mLIlQ
function name:  matrix_is_solved
number of ops:  31
compiled vars:  !0 = $matrix, !1 = $offset, !2 = $column, !3 = $raw
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   41     1        INIT_FCALL                                               'array_keys'
          2        INIT_FCALL                                               'current'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $4      
          5        SEND_VAR                                                 $4
          6        DO_ICALL                                         $5      
          7      > FE_RESET_R                                       $6      $5, ->28
          8    > > FE_FETCH_R                                               $6, !1, ->28
   42     9    >   INIT_FCALL                                               'array_filter'
         10        INIT_FCALL                                               'array_column'
         11        SEND_VAR                                                 !0
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $7      
         14        ASSIGN                                           ~8      !3, $7
         15        SEND_VAL                                                 ~8
         16        DO_ICALL                                         $9      
         17        ASSIGN                                                   !2, $9
   43    18        COUNT                                            ~11     !2
         19        INIT_FCALL                                               'array_unique'
         20        SEND_VAR                                                 !2
         21        DO_ICALL                                         $12     
         22        COUNT                                            ~13     $12
         23        IS_NOT_EQUAL                                             ~11, ~13
         24      > JMPZ                                                     ~14, ->27
         25    >   FE_FREE                                                  $6
         26      > RETURN                                                   <false>
   41    27    > > JMP                                                      ->8
         28    >   FE_FREE                                                  $6
   45    29      > RETURN                                                   <true>
   46    30*     > RETURN                                                   null

End of function matrix_is_solved

Function array_rotate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 14
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 14
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/mLIlQ
function name:  array_rotate
number of ops:  20
compiled vars:  !0 = $array, !1 = $offset, !2 = $val, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   49     2        INIT_FCALL                                               'array_slice'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 0
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $4      
          7      > FE_RESET_R                                       $5      $4, ->14
          8    > > FE_FETCH_R                                       ~6      $5, !2, ->14
          9    >   ASSIGN                                                   !3, ~6
   50    10        UNSET_DIM                                                !0, !3
   51    11        ASSIGN_DIM                                               !0, !3
         12        OP_DATA                                                  !2
   49    13      > JMP                                                      ->8
         14    >   FE_FREE                                                  $5
   53    15        INIT_FCALL                                               'array_values'
         16        SEND_VAR                                                 !0
         17        DO_ICALL                                         $9      
         18        ASSIGN                                                   !0, $9
   54    19      > RETURN                                                   null

End of function array_rotate

Function matrix_display:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 16
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 16
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/mLIlQ
function name:  matrix_display
number of ops:  19
compiled vars:  !0 = $matrix, !1 = $inner, !2 = $row
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV_INIT                                        !0      null
   57     1        ECHO                                                     '%5B%0A'
   58     2      > FE_RESET_R                                       $3      !0, ->16
          3    > > FE_FETCH_R                                       ~4      $3, !1, ->16
          4    >   ASSIGN                                                   !2, ~4
   59     5        ROPE_INIT                                     3  ~7      '++'
          6        ROPE_ADD                                      1  ~7      ~7, !2
          7        ROPE_END                                      2  ~6      ~7, '+%3D%3E+%5B%27'
          8        INIT_FCALL                                               'implode'
          9        SEND_VAL                                                 '%27%2C+%27'
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                         $9      
         12        CONCAT                                           ~10     ~6, $9
         13        CONCAT                                           ~11     ~10, '%27%5D%0A'
         14        ECHO                                                     ~11
   58    15      > JMP                                                      ->3
         16    >   FE_FREE                                                  $3
   61    17        ECHO                                                     '%5D%0A'
   62    18      > RETURN                                                   null

End of function matrix_display

Function %00%7Bclosure%7D%2Fin%2FmLIlQ%3A73%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mLIlQ
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $matrix
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   RECV                                             !0      
   74     1        INIT_FCALL                                               'matrix_display'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0          
   75     4        ECHO                                                     'solved+by%3A%0A'
   76     5        INIT_FCALL                                               'matrix_brute_solve'
          6        SEND_VAR                                                 !0
          7        DO_FCALL                                      0          
   77     8        ECHO                                                     '%0A'
   78     9      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FmLIlQ%3A73%241

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
181.44 ms | 1419 KiB | 37 Q