3v4l.org

run code in 300+ PHP versions simultaneously
<?php class SudokuSolver { protected $sudokuColumns; public function __construct ($sudoku) { $this->sudokuColumns = $sudoku; } protected static function getNumbers () { return array_flip(str_split("123456789")); } // could be static as well public function printOut ($sudoku) { foreach ($sudoku as $column) { foreach ($column as $cell) { echo "|"; if (null !== $cell) { echo $cell; } else { echo " "; } } echo "|\n"; } } public function solve() { if (!$this->checkConsistenz()) { throw new Exception("inconsistent data!"); } } protected function checkConsistenz () { $columns = $this->sudokuColumns; foreach ($columns as $columnIndex => $column) { if (!$this->checkLine($columnIndex,$column)) { return false; } } // check rows -> own method $rows = array(); foreach ($columns as $columnIndex => $column) { foreach ($column as $rowIndex => $cell) { $rows[$rowIndex][$columnIndex] = $cell; } //$this->printOut($rows); } foreach ($rows as $rowIndex => $row) { if (!$this->checkLine($rowIndex,$row)) { return false; } } // check field -> own method $fields = array(); foreach ($columns as $columnIndex => $column) { foreach ($column as $rowIndex => $cell) { //echo "(3 * ($columnIndex / 3)) + ($rowIndex / 3) = ". (3 * ($columnIndex / 3)) ." + ". ($rowIndex / 3) ."\n"; $fields[(3 * intval($columnIndex / 3)) + intval($rowIndex / 3)][] = $cell; } } print_r($fields); foreach ($rows as $rowIndex => $row) { if (!$this->checkLine($rowIndex,$row)) { return false; } } return true; } protected function checkLine ($rowIndex, $row) { $numbers = self::getNumbers(); echo "checking $rowIndex..\n"; foreach ($row as $column) { if (null === $column) { continue; } //echo "$column , $numbers\n" . var_export(strpos($numbers,$column),true); if (!array_key_exists($column,$numbers)) { return false; } unset($numbers[$column]); } return true; } } $sudoku = array(); $sudoku[] = array(null,3,null, null,null,null, null,null,null); $sudoku[] = array(3,null,null, 1,9,5, null,null,null); $sudoku[] = array(null,null,8, null,null,null, null,6,null); $sudoku[] = array(8,null,null, null,6,null, null,null,null); $sudoku[] = array(4,null,null, 8,null,null, null,null,1); $sudoku[] = array(null,null,null ,null,2,null, null,null,null); $sudoku[] = array(null,6,null, null,null,null, 2,8,null); $sudoku[] = array(null,null,null, 4,1,9, null,null,5); $sudoku[] = array(null,null,null, null,null,null, null,7,null); $sudokuSolver = new SudokuSolver($sudoku); //$sudokuSolver->printOut($sudoku); $sudokuSolver->solve($sudoku);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/10P4h
function name:  (null)
number of ops:  27
compiled vars:  !0 = $sudoku, !1 = $sudokuSolver
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  101     0  E >   ASSIGN                                                   !0, <array>
  102     1        ASSIGN_DIM                                               !0
          2        OP_DATA                                                  <array>
  103     3        ASSIGN_DIM                                               !0
          4        OP_DATA                                                  <array>
  104     5        ASSIGN_DIM                                               !0
          6        OP_DATA                                                  <array>
  105     7        ASSIGN_DIM                                               !0
          8        OP_DATA                                                  <array>
  106     9        ASSIGN_DIM                                               !0
         10        OP_DATA                                                  <array>
  107    11        ASSIGN_DIM                                               !0
         12        OP_DATA                                                  <array>
  108    13        ASSIGN_DIM                                               !0
         14        OP_DATA                                                  <array>
  109    15        ASSIGN_DIM                                               !0
         16        OP_DATA                                                  <array>
  110    17        ASSIGN_DIM                                               !0
         18        OP_DATA                                                  <array>
  112    19        NEW                                              $12     'SudokuSolver'
         20        SEND_VAR_EX                                              !0
         21        DO_FCALL                                      0          
         22        ASSIGN                                                   !1, $12
  114    23        INIT_METHOD_CALL                                         !1, 'solve'
         24        SEND_VAR_EX                                              !0
         25        DO_FCALL                                      0          
         26      > RETURN                                                   1

Class SudokuSolver:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/10P4h
function name:  __construct
number of ops:  4
compiled vars:  !0 = $sudoku
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
    9     1        ASSIGN_OBJ                                               'sudokuColumns'
          2        OP_DATA                                                  !0
   10     3      > RETURN                                                   null

End of function __construct

Function getnumbers:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/10P4h
function name:  getNumbers
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   INIT_FCALL                                               'array_flip'
          1        INIT_FCALL                                               'str_split'
          2        SEND_VAL                                                 '123456789'
          3        DO_ICALL                                         $0      
          4        SEND_VAR                                                 $0
          5        DO_ICALL                                         $1      
          6      > RETURN                                                   $1
   15     7*     > RETURN                                                   null

End of function getnumbers

Function printout:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 15
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 15
Branch analysis from position: 3
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 12
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/10P4h
function name:  printOut
number of ops:  17
compiled vars:  !0 = $sudoku, !1 = $column, !2 = $cell
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
   20     1      > FE_RESET_R                                       $3      !0, ->15
          2    > > FE_FETCH_R                                               $3, !1, ->15
   21     3    > > FE_RESET_R                                       $4      !1, ->12
          4    > > FE_FETCH_R                                               $4, !2, ->12
   22     5    >   ECHO                                                     '%7C'
   23     6        TYPE_CHECK                                  1020          !2
          7      > JMPZ                                                     ~5, ->10
   24     8    >   ECHO                                                     !2
          9      > JMP                                                      ->11
   26    10    >   ECHO                                                     '+'
   21    11    > > JMP                                                      ->4
         12    >   FE_FREE                                                  $4
   29    13        ECHO                                                     '%7C%0A'
   20    14      > JMP                                                      ->2
         15    >   FE_FREE                                                  $3
   31    16      > RETURN                                                   null

End of function printout

Function solve:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/10P4h
function name:  solve
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   INIT_METHOD_CALL                                         'checkConsistenz'
          1        DO_FCALL                                      0  $0      
          2        BOOL_NOT                                         ~1      $0
          3      > JMPZ                                                     ~1, ->8
   36     4    >   NEW                                              $2      'Exception'
          5        SEND_VAL_EX                                              'inconsistent+data%21'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $2
   38     8    > > RETURN                                                   null

End of function solve

Function checkconsistenz:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 14
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 14
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 14
2 jumps found. (Code = 77) Position 1 = 17, Position 2 = 28
Branch analysis from position: 17
2 jumps found. (Code = 78) Position 1 = 18, Position 2 = 28
Branch analysis from position: 18
2 jumps found. (Code = 77) Position 1 = 20, Position 2 = 26
Branch analysis from position: 20
2 jumps found. (Code = 78) Position 1 = 21, Position 2 = 26
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
Branch analysis from position: 26
Branch analysis from position: 28
2 jumps found. (Code = 77) Position 1 = 30, Position 2 = 41
Branch analysis from position: 30
2 jumps found. (Code = 78) Position 1 = 31, Position 2 = 41
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 40
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
Branch analysis from position: 41
2 jumps found. (Code = 77) Position 1 = 44, Position 2 = 61
Branch analysis from position: 44
2 jumps found. (Code = 78) Position 1 = 45, Position 2 = 61
Branch analysis from position: 45
2 jumps found. (Code = 77) Position 1 = 47, Position 2 = 59
Branch analysis from position: 47
2 jumps found. (Code = 78) Position 1 = 48, Position 2 = 59
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 59
Branch analysis from position: 61
2 jumps found. (Code = 77) Position 1 = 66, Position 2 = 77
Branch analysis from position: 66
2 jumps found. (Code = 78) Position 1 = 67, Position 2 = 77
Branch analysis from position: 67
2 jumps found. (Code = 43) Position 1 = 74, Position 2 = 76
Branch analysis from position: 74
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 76
1 jumps found. (Code = 42) Position 1 = 66
Branch analysis from position: 66
Branch analysis from position: 77
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 77
Branch analysis from position: 61
Branch analysis from position: 41
Branch analysis from position: 28
Branch analysis from position: 14
filename:       /in/10P4h
function name:  checkConsistenz
number of ops:  80
compiled vars:  !0 = $columns, !1 = $column, !2 = $columnIndex, !3 = $rows, !4 = $cell, !5 = $rowIndex, !6 = $row, !7 = $fields
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   FETCH_OBJ_R                                      ~8      'sudokuColumns'
          1        ASSIGN                                                   !0, ~8
   43     2      > FE_RESET_R                                       $10     !0, ->14
          3    > > FE_FETCH_R                                       ~11     $10, !1, ->14
          4    >   ASSIGN                                                   !2, ~11
   44     5        INIT_METHOD_CALL                                         'checkLine'
          6        SEND_VAR_EX                                              !2
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0  $13     
          9        BOOL_NOT                                         ~14     $13
         10      > JMPZ                                                     ~14, ->13
   45    11    >   FE_FREE                                                  $10
         12      > RETURN                                                   <false>
   43    13    > > JMP                                                      ->3
         14    >   FE_FREE                                                  $10
   50    15        ASSIGN                                                   !3, <array>
   51    16      > FE_RESET_R                                       $16     !0, ->28
         17    > > FE_FETCH_R                                       ~17     $16, !1, ->28
         18    >   ASSIGN                                                   !2, ~17
   52    19      > FE_RESET_R                                       $19     !1, ->26
         20    > > FE_FETCH_R                                       ~20     $19, !4, ->26
         21    >   ASSIGN                                                   !5, ~20
   53    22        FETCH_DIM_W                                      $22     !3, !5
         23        ASSIGN_DIM                                               $22, !2
         24        OP_DATA                                                  !4
   52    25      > JMP                                                      ->20
         26    >   FE_FREE                                                  $19
   51    27      > JMP                                                      ->17
         28    >   FE_FREE                                                  $16
   57    29      > FE_RESET_R                                       $24     !3, ->41
         30    > > FE_FETCH_R                                       ~25     $24, !6, ->41
         31    >   ASSIGN                                                   !5, ~25
   58    32        INIT_METHOD_CALL                                         'checkLine'
         33        SEND_VAR_EX                                              !5
         34        SEND_VAR_EX                                              !6
         35        DO_FCALL                                      0  $27     
         36        BOOL_NOT                                         ~28     $27
         37      > JMPZ                                                     ~28, ->40
   59    38    >   FE_FREE                                                  $24
         39      > RETURN                                                   <false>
   57    40    > > JMP                                                      ->30
         41    >   FE_FREE                                                  $24
   64    42        ASSIGN                                                   !7, <array>
   65    43      > FE_RESET_R                                       $30     !0, ->61
         44    > > FE_FETCH_R                                       ~31     $30, !1, ->61
         45    >   ASSIGN                                                   !2, ~31
   66    46      > FE_RESET_R                                       $33     !1, ->59
         47    > > FE_FETCH_R                                       ~34     $33, !4, ->59
         48    >   ASSIGN                                                   !5, ~34
   68    49        DIV                                              ~36     !2, 3
         50        CAST                                          4  ~37     ~36
         51        MUL                                              ~38     ~37, 3
         52        DIV                                              ~39     !5, 3
         53        CAST                                          4  ~40     ~39
         54        ADD                                              ~41     ~38, ~40
         55        FETCH_DIM_W                                      $42     !7, ~41
         56        ASSIGN_DIM                                               $42
         57        OP_DATA                                                  !4
   66    58      > JMP                                                      ->47
         59    >   FE_FREE                                                  $33
   65    60      > JMP                                                      ->44
         61    >   FE_FREE                                                  $30
   71    62        INIT_FCALL                                               'print_r'
         63        SEND_VAR                                                 !7
         64        DO_ICALL                                                 
   72    65      > FE_RESET_R                                       $45     !3, ->77
         66    > > FE_FETCH_R                                       ~46     $45, !6, ->77
         67    >   ASSIGN                                                   !5, ~46
   73    68        INIT_METHOD_CALL                                         'checkLine'
         69        SEND_VAR_EX                                              !5
         70        SEND_VAR_EX                                              !6
         71        DO_FCALL                                      0  $48     
         72        BOOL_NOT                                         ~49     $48
         73      > JMPZ                                                     ~49, ->76
   74    74    >   FE_FREE                                                  $45
         75      > RETURN                                                   <false>
   72    76    > > JMP                                                      ->66
         77    >   FE_FREE                                                  $45
   78    78      > RETURN                                                   <true>
   79    79*     > RETURN                                                   null

End of function checkconsistenz

Function checkline:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 21
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 21
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 14
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename:       /in/10P4h
function name:  checkLine
number of ops:  24
compiled vars:  !0 = $rowIndex, !1 = $row, !2 = $numbers, !3 = $column
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   81     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   83     2        INIT_STATIC_METHOD_CALL                                  'getNumbers'
          3        DO_FCALL                                      0  $4      
          4        ASSIGN                                                   !2, $4
   84     5        ROPE_INIT                                     3  ~7      'checking+'
          6        ROPE_ADD                                      1  ~7      ~7, !0
          7        ROPE_END                                      2  ~6      ~7, '..%0A'
          8        ECHO                                                     ~6
   85     9      > FE_RESET_R                                       $9      !1, ->21
         10    > > FE_FETCH_R                                               $9, !3, ->21
   86    11    >   TYPE_CHECK                                    2          !3
         12      > JMPZ                                                     ~10, ->14
   87    13    > > JMP                                                      ->10
   90    14    >   ARRAY_KEY_EXISTS                                 ~11     !3, !2
         15        BOOL_NOT                                         ~12     ~11
         16      > JMPZ                                                     ~12, ->19
   92    17    >   FE_FREE                                                  $9
         18      > RETURN                                                   <false>
   94    19    >   UNSET_DIM                                                !2, !3
   85    20      > JMP                                                      ->10
         21    >   FE_FREE                                                  $9
   96    22      > RETURN                                                   <true>
   97    23*     > RETURN                                                   null

End of function checkline

End of class SudokuSolver.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.18 ms | 1408 KiB | 19 Q