3v4l.org

run code in 300+ PHP versions simultaneously
<?php $solution = new PuzzleSolver($argv[1], $argv[2]); $solution->solvePuzzle(); $solution->saveOutput(); /* * PuzzleSolver class. * * Will draw lines between points defined by '#' character. */ class PuzzleSolver { const searchChar = '#'; const lineChar = '*'; public $puzzle = array(); public $solvedPuzzle = array(); public $output; private $linePoints = array(); /** * Constructs a PuzzleSolver object. * * @param string $input * The input file in .txt format containing the puzzle. * @param string $output optional * The output file to save the solved puzzle. * * @throws ErrorException * If either the input file is not provided or the puzzle is empty. */ function __construct($input, $output = null) { if (!isset($input)) throw new ErrorException("Input puzzle must be defined."); $this->puzzle = file_get_contents($input); $this->puzzle = explode("\n", $this->puzzle); $this->output = $output; if (!$this->puzzle) throw new ErrorException("Puzzle is empty; I need a challenge!"); } /** * Main puzzle solving function, finds each point which will be connected. */ public function solvePuzzle() { foreach ($this->puzzle as $index => $row) { $i = 0; while ($i <= strlen($row)) { $foundChar = strpos($row, $this::searchChar, $i); if ($foundChar === FALSE) { break; } else { array_push($this->linePoints, array('row' => $index, 'col' => $foundChar)); $i = $foundChar + 1; } } } $this->solvedPuzzle = $this->puzzle; $this->connectPoints(); } /** * Takes the line points found by solvePuzzle() and connects them. */ private function connectPoints() { foreach ($this->linePoints as $point) { if (isset($previousPoint)) { if ($previousPoint['row'] != $point['row']) { $this->draw($previousPoint, $point); $pointCorner = ($previousPoint['col'] > $point['col']) ? $previousPoint['col'] + 1 : $previousPoint['col'] - 1; $previousPoint = array('row' => $point['row'], 'col' => $pointCorner); } $this->draw($previousPoint, $point); } $previousPoint = $point; } } /** * Draw a line between two points using the global lineChar variable. * Each point is expected to be an associative array with a row and col key. * * @param Array $start * An associative array containing the start point. * @param Array $end * An associative array containing the end point. */ private function draw(Array $start, Array $end) { if ($start['col'] == $end['col'] && $start['row'] == $end['row']) return; if ($start['row'] == $end['row']) { $this->solvedPuzzle[$start['row']] = $this->line($this->solvedPuzzle[$start['row']], $start['col'], $end['col']); } else { foreach ($this->puzzle as $index => $row) { if ($index > $start['row'] && $index < $end['row']) { $this->solvedPuzzle[$index] = $this->line($row, $start['col']); } } } } /** * Responsible for the character replacement in the line draw. * * @param string $text * The full text of the line being drawn in. * @param integer $start * The character position of the start of the line. * @param integer $end optional * The character position of the end of the line. * * @return string * The resulting string after the line is drawn. */ private function line($text, $start, $end = null) { if (isset($end) && $start > $end) list($start, $end) = array($end, $start); $start = $start + isset($end); $length = (!isset($end)) ? 1 : abs($start - $end); return substr($text, 0, $start) . str_repeat($this::lineChar, $length) . substr($text, $start+$length); } /** * Saves the puzzle results to a file. */ public function saveOutput() { if (!$this->output) { $this->printOutput(); } else { file_put_contents($this->output, implode($this->solvedPuzzle, "\n")); } } /** * Prints the puzzle results to the screen. */ public function printOutput() { print implode($this->solvedPuzzle, "\n"); } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/T8RJv
function name:  (null)
number of ops:  14
compiled vars:  !0 = $solution, !1 = $argv
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   NEW                                              $2      'PuzzleSolver'
          1        CHECK_FUNC_ARG                                           
          2        FETCH_DIM_FUNC_ARG                               $3      !1, 1
          3        SEND_FUNC_ARG                                            $3
          4        CHECK_FUNC_ARG                                           
          5        FETCH_DIM_FUNC_ARG                               $4      !1, 2
          6        SEND_FUNC_ARG                                            $4
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !0, $2
    4     9        INIT_METHOD_CALL                                         !0, 'solvePuzzle'
         10        DO_FCALL                                      0          
    5    11        INIT_METHOD_CALL                                         !0, 'saveOutput'
         12        DO_FCALL                                      0          
  150    13      > RETURN                                                   1

Class PuzzleSolver:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 30
Branch analysis from position: 26
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/T8RJv
function name:  __construct
number of ops:  31
compiled vars:  !0 = $input, !1 = $output
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      null
   35     2        ISSET_ISEMPTY_CV                                 ~2      !0
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->9
   36     5    >   NEW                                              $4      'ErrorException'
          6        SEND_VAL_EX                                              'Input+puzzle+must+be+defined.'
          7        DO_FCALL                                      0          
          8      > THROW                                         0          $4
   38     9    >   INIT_FCALL                                               'file_get_contents'
         10        SEND_VAR                                                 !0
         11        DO_ICALL                                         $7      
         12        ASSIGN_OBJ                                               'puzzle'
         13        OP_DATA                                                  $7
   39    14        INIT_FCALL                                               'explode'
         15        SEND_VAL                                                 '%0A'
         16        FETCH_OBJ_R                                      ~9      'puzzle'
         17        SEND_VAL                                                 ~9
         18        DO_ICALL                                         $10     
         19        ASSIGN_OBJ                                               'puzzle'
         20        OP_DATA                                                  $10
   40    21        ASSIGN_OBJ                                               'output'
         22        OP_DATA                                                  !1
   42    23        FETCH_OBJ_R                                      ~12     'puzzle'
         24        BOOL_NOT                                         ~13     ~12
         25      > JMPZ                                                     ~13, ->30
   43    26    >   NEW                                              $14     'ErrorException'
         27        SEND_VAL_EX                                              'Puzzle+is+empty%3B+I+need+a+challenge%21'
         28        DO_FCALL                                      0          
         29      > THROW                                         0          $14
   44    30    > > RETURN                                                   null

End of function __construct

Function solvepuzzle:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 32
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 32
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 6
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 19
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 6
Branch analysis from position: 31
Branch analysis from position: 6
Branch analysis from position: 32
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 32
filename:       /in/T8RJv
function name:  solvePuzzle
number of ops:  39
compiled vars:  !0 = $row, !1 = $index, !2 = $i, !3 = $foundChar
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   FETCH_OBJ_R                                      ~4      'puzzle'
          1      > FE_RESET_R                                       $5      ~4, ->32
          2    > > FE_FETCH_R                                       ~6      $5, !0, ->32
          3    >   ASSIGN                                                   !1, ~6
   51     4        ASSIGN                                                   !2, 0
   53     5      > JMP                                                      ->28
   54     6    >   INIT_FCALL                                               'strpos'
          7        SEND_VAR                                                 !0
          8        FETCH_THIS                                       ~9      
          9        FETCH_CLASS                                   0  $10     ~9
         10        FETCH_CLASS_CONSTANT                             ~11     $10, 'searchChar'
         11        SEND_VAL                                                 ~11
         12        SEND_VAR                                                 !2
         13        DO_ICALL                                         $12     
         14        ASSIGN                                                   !3, $12
   56    15        TYPE_CHECK                                    4          !3
         16      > JMPZ                                                     ~14, ->19
   57    17    > > JMP                                                      ->31
         18*       JMP                                                      ->28
   59    19    >   INIT_FCALL                                               'array_push'
         20        FETCH_OBJ_W                                      $15     'linePoints'
         21        SEND_REF                                                 $15
         22        INIT_ARRAY                                       ~16     !1, 'row'
         23        ADD_ARRAY_ELEMENT                                ~16     !3, 'col'
         24        SEND_VAL                                                 ~16
         25        DO_ICALL                                                 
   60    26        ADD                                              ~18     !3, 1
         27        ASSIGN                                                   !2, ~18
   53    28    >   STRLEN                                           ~20     !0
         29        IS_SMALLER_OR_EQUAL                                      !2, ~20
         30      > JMPNZ                                                    ~21, ->6
   50    31    > > JMP                                                      ->2
         32    >   FE_FREE                                                  $5
   65    33        FETCH_OBJ_R                                      ~23     'puzzle'
         34        ASSIGN_OBJ                                               'solvedPuzzle'
         35        OP_DATA                                                  ~23
   66    36        INIT_METHOD_CALL                                         'connectPoints'
         37        DO_FCALL                                      0          
   67    38      > RETURN                                                   null

End of function solvepuzzle

Function connectpoints:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 35
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 35
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 33
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 29
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 29
Branch analysis from position: 33
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 35
filename:       /in/T8RJv
function name:  connectPoints
number of ops:  37
compiled vars:  !0 = $point, !1 = $previousPoint, !2 = $pointCorner
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   FETCH_OBJ_R                                      ~3      'linePoints'
          1      > FE_RESET_R                                       $4      ~3, ->35
          2    > > FE_FETCH_R                                               $4, !0, ->35
   74     3    >   ISSET_ISEMPTY_CV                                         !1
          4      > JMPZ                                                     ~5, ->33
   75     5    >   FETCH_DIM_R                                      ~6      !1, 'row'
          6        FETCH_DIM_R                                      ~7      !0, 'row'
          7        IS_NOT_EQUAL                                             ~6, ~7
          8      > JMPZ                                                     ~8, ->29
   76     9    >   INIT_METHOD_CALL                                         'draw'
         10        SEND_VAR_EX                                              !1
         11        SEND_VAR_EX                                              !0
         12        DO_FCALL                                      0          
   77    13        FETCH_DIM_R                                      ~10     !1, 'col'
         14        FETCH_DIM_R                                      ~11     !0, 'col'
         15        IS_SMALLER                                               ~11, ~10
         16      > JMPZ                                                     ~12, ->21
         17    >   FETCH_DIM_R                                      ~13     !1, 'col'
         18        ADD                                              ~14     ~13, 1
         19        QM_ASSIGN                                        ~15     ~14
         20      > JMP                                                      ->24
         21    >   FETCH_DIM_R                                      ~16     !1, 'col'
         22        SUB                                              ~17     ~16, 1
         23        QM_ASSIGN                                        ~15     ~17
         24    >   ASSIGN                                                   !2, ~15
   78    25        FETCH_DIM_R                                      ~19     !0, 'row'
         26        INIT_ARRAY                                       ~20     ~19, 'row'
         27        ADD_ARRAY_ELEMENT                                ~20     !2, 'col'
         28        ASSIGN                                                   !1, ~20
   81    29    >   INIT_METHOD_CALL                                         'draw'
         30        SEND_VAR_EX                                              !1
         31        SEND_VAR_EX                                              !0
         32        DO_FCALL                                      0          
   84    33    >   ASSIGN                                                   !1, !0
   73    34      > JMP                                                      ->2
         35    >   FE_FREE                                                  $4
   86    36      > RETURN                                                   null

End of function connectpoints

Function draw:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 34
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
2 jumps found. (Code = 77) Position 1 = 36, Position 2 = 55
Branch analysis from position: 36
2 jumps found. (Code = 78) Position 1 = 37, Position 2 = 55
Branch analysis from position: 37
2 jumps found. (Code = 46) Position 1 = 41, Position 2 = 44
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 45, Position 2 = 54
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
Branch analysis from position: 54
Branch analysis from position: 44
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 55
Branch analysis from position: 10
filename:       /in/T8RJv
function name:  draw
number of ops:  57
compiled vars:  !0 = $start, !1 = $end, !2 = $row, !3 = $index
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   97     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   98     2        FETCH_DIM_R                                      ~4      !0, 'col'
          3        FETCH_DIM_R                                      ~5      !1, 'col'
          4        IS_EQUAL                                         ~6      ~4, ~5
          5      > JMPZ_EX                                          ~6      ~6, ->10
          6    >   FETCH_DIM_R                                      ~7      !0, 'row'
          7        FETCH_DIM_R                                      ~8      !1, 'row'
          8        IS_EQUAL                                         ~9      ~7, ~8
          9        BOOL                                             ~6      ~9
         10    > > JMPZ                                                     ~6, ->12
   99    11    > > RETURN                                                   null
  101    12    >   FETCH_DIM_R                                      ~10     !0, 'row'
         13        FETCH_DIM_R                                      ~11     !1, 'row'
         14        IS_EQUAL                                                 ~10, ~11
         15      > JMPZ                                                     ~12, ->34
  102    16    >   FETCH_DIM_R                                      ~14     !0, 'row'
         17        INIT_METHOD_CALL                                         'line'
         18        CHECK_FUNC_ARG                                           
         19        FETCH_DIM_R                                      ~17     !0, 'row'
         20        FETCH_OBJ_FUNC_ARG                               $16     'solvedPuzzle'
         21        FETCH_DIM_FUNC_ARG                               $18     $16, ~17
         22        SEND_FUNC_ARG                                            $18
         23        CHECK_FUNC_ARG                                           
         24        FETCH_DIM_FUNC_ARG                               $19     !0, 'col'
         25        SEND_FUNC_ARG                                            $19
         26        CHECK_FUNC_ARG                                           
         27        FETCH_DIM_FUNC_ARG                               $20     !1, 'col'
         28        SEND_FUNC_ARG                                            $20
         29        DO_FCALL                                      0  $21     
         30        FETCH_OBJ_W                                      $13     'solvedPuzzle'
         31        ASSIGN_DIM                                               $13, ~14
         32        OP_DATA                                                  $21
         33      > JMP                                                      ->56
  104    34    >   FETCH_OBJ_R                                      ~22     'puzzle'
         35      > FE_RESET_R                                       $23     ~22, ->55
         36    > > FE_FETCH_R                                       ~24     $23, !2, ->55
         37    >   ASSIGN                                                   !3, ~24
  105    38        FETCH_DIM_R                                      ~26     !0, 'row'
         39        IS_SMALLER                                       ~27     ~26, !3
         40      > JMPZ_EX                                          ~27     ~27, ->44
         41    >   FETCH_DIM_R                                      ~28     !1, 'row'
         42        IS_SMALLER                                       ~29     !3, ~28
         43        BOOL                                             ~27     ~29
         44    > > JMPZ                                                     ~27, ->54
  106    45    >   INIT_METHOD_CALL                                         'line'
         46        SEND_VAR_EX                                              !2
         47        CHECK_FUNC_ARG                                           
         48        FETCH_DIM_FUNC_ARG                               $32     !0, 'col'
         49        SEND_FUNC_ARG                                            $32
         50        DO_FCALL                                      0  $33     
         51        FETCH_OBJ_W                                      $30     'solvedPuzzle'
         52        ASSIGN_DIM                                               $30, !3
         53        OP_DATA                                                  $33
  104    54    > > JMP                                                      ->36
         55    >   FE_FREE                                                  $23
  110    56    > > RETURN                                                   null

End of function draw

Function line:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 23
Branch analysis from position: 21
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: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
Branch analysis from position: 7
filename:       /in/T8RJv
function name:  line
number of ops:  50
compiled vars:  !0 = $text, !1 = $start, !2 = $end, !3 = $length
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  125     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
  126     3        ISSET_ISEMPTY_CV                                 ~4      !2
          4      > JMPZ_EX                                          ~4      ~4, ->7
          5    >   IS_SMALLER                                       ~5      !2, !1
          6        BOOL                                             ~4      ~5
          7    > > JMPZ                                                     ~4, ->15
          8    >   INIT_ARRAY                                       ~6      !2
          9        ADD_ARRAY_ELEMENT                                ~6      !1
         10        FETCH_LIST_R                                     $7      ~6, 0
         11        ASSIGN                                                   !1, $7
         12        FETCH_LIST_R                                     $9      ~6, 1
         13        ASSIGN                                                   !2, $9
         14        FREE                                                     ~6
  127    15    >   ISSET_ISEMPTY_CV                                 ~11     !2
         16        ADD                                              ~12     !1, ~11
         17        ASSIGN                                                   !1, ~12
  128    18        ISSET_ISEMPTY_CV                                 ~14     !2
         19        BOOL_NOT                                         ~15     ~14
         20      > JMPZ                                                     ~15, ->23
         21    >   QM_ASSIGN                                        ~16     1
         22      > JMP                                                      ->28
         23    >   INIT_FCALL                                               'abs'
         24        SUB                                              ~17     !1, !2
         25        SEND_VAL                                                 ~17
         26        DO_ICALL                                         $18     
         27        QM_ASSIGN                                        ~16     $18
         28    >   ASSIGN                                                   !3, ~16
  130    29        INIT_FCALL                                               'substr'
         30        SEND_VAR                                                 !0
         31        SEND_VAL                                                 0
         32        SEND_VAR                                                 !1
         33        DO_ICALL                                         $20     
         34        INIT_FCALL                                               'str_repeat'
         35        FETCH_THIS                                       ~21     
         36        FETCH_CLASS                                   0  $22     ~21
         37        FETCH_CLASS_CONSTANT                             ~23     $22, 'lineChar'
         38        SEND_VAL                                                 ~23
         39        SEND_VAR                                                 !3
         40        DO_ICALL                                         $24     
         41        CONCAT                                           ~25     $20, $24
         42        INIT_FCALL                                               'substr'
         43        SEND_VAR                                                 !0
         44        ADD                                              ~26     !1, !3
         45        SEND_VAL                                                 ~26
         46        DO_ICALL                                         $27     
         47        CONCAT                                           ~28     ~25, $27
         48      > RETURN                                                   ~28
  131    49*     > RETURN                                                   null

End of function line

Function saveoutput:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/T8RJv
function name:  saveOutput
number of ops:  17
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  137     0  E >   FETCH_OBJ_R                                      ~0      'output'
          1        BOOL_NOT                                         ~1      ~0
          2      > JMPZ                                                     ~1, ->6
  138     3    >   INIT_METHOD_CALL                                         'printOutput'
          4        DO_FCALL                                      0          
          5      > JMP                                                      ->16
  140     6    >   INIT_FCALL                                               'file_put_contents'
          7        FETCH_OBJ_R                                      ~3      'output'
          8        SEND_VAL                                                 ~3
          9        INIT_FCALL                                               'implode'
         10        FETCH_OBJ_R                                      ~4      'solvedPuzzle'
         11        SEND_VAL                                                 ~4
         12        SEND_VAL                                                 '%0A'
         13        DO_ICALL                                         $5      
         14        SEND_VAR                                                 $5
         15        DO_ICALL                                                 
  142    16    > > RETURN                                                   null

End of function saveoutput

Function printoutput:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/T8RJv
function name:  printOutput
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  148     0  E >   INIT_FCALL                                               'implode'
          1        FETCH_OBJ_R                                      ~0      'solvedPuzzle'
          2        SEND_VAL                                                 ~0
          3        SEND_VAL                                                 '%0A'
          4        DO_ICALL                                         $1      
          5        ECHO                                                     $1
  149     6      > RETURN                                                   null

End of function printoutput

End of class PuzzleSolver.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164 ms | 1412 KiB | 31 Q