3v4l.org

run code in 300+ PHP versions simultaneously
<?php $weight = 5678; $trucks = [1200, 600, 300]; $result = optimize($weight, $trucks); var_dump($result); $weight = 900; $trucks = [600, 350, 100]; $result = optimize($weight, $trucks); var_dump($result); $weight = 1300; $trucks = [1200, 600, 300]; $result = optimize($weight, $trucks); var_dump($result); function optimize(int $weight = 0, array $trucks = [], $sensitivity = 10): array { $weightStore = $weight; // cache weight // sort truck-type array (highest to lowest weight capacity). rsort($trucks); /* initialize truckCount at 0 for all types */ foreach ($trucks as $type) { $truckCount[$type] = 0; } foreach ($trucks as $type) { $capacity = $type; // truck capacity $exact = ($weight / $type); $round = (int) (floor($exact)); // whole trucks if ($exact >= 1) { $truckCount[$type] = $round; // whole trucks // adjust weight $weight = $weight - ($round * $type); } } // do we still have remaining weight if ($weight > 0) { rsort($trucks); foreach ($trucks as $type) { $ratio[$type] = $weight / $type; $max = max($ratio); } $type = array_search($max, $ratio); $truckCount[$type]++; $weight -= $type; // final weight adjustment } /* * use the ratio of truck capacities to identify * edge cases in results array: * e.g.: algorithm selected two trucks of 300 cap * where 1 of 600 cap would be more efficient. */ $ratioCap = []; foreach ($trucks as $key => $type) { if (isset($trucks[$key + 1])) { $ratioCap[$trucks[$key + 1]] = ($trucks[$key] / $trucks[$key + 1]); } } /* edge cases - force fewer trucks */ $sensitivity = ($sensitivity <= 0) ? 10 : $sensitivity; // set default if neg. or 0 foreach ($trucks as $cycle) { foreach ($truckCount as $type => $number) { foreach ($ratioCap as $key => $value) { if ($type == $key && $number >= (floor($value) / $sensitivity) && $number != 1) { $truckCount[$type] = 0; // group of smaller type trucks = 0 $truckCount[$type * $value] += 1; // the group of smaller trucks is moved into one larger truck } } } } /* truck capacity vs. weight transported */ $capacityUse = 0; foreach($truckCount as $type => $number) { $capacityUse += $type * $number; } $weight = $weightStore - $capacityUse; return ['trucks' => $truckCount, 'remaining weight' => $weight]; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/JFc8X
function name:  (null)
number of ops:  31
compiled vars:  !0 = $weight, !1 = $trucks, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, 5678
    3     1        ASSIGN                                                   !1, <array>
    4     2        INIT_FCALL_BY_NAME                                       'optimize'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $5      
          6        ASSIGN                                                   !2, $5
    5     7        INIT_FCALL                                               'var_dump'
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                                 
    6    10        ASSIGN                                                   !0, 900
    7    11        ASSIGN                                                   !1, <array>
    8    12        INIT_FCALL_BY_NAME                                       'optimize'
         13        SEND_VAR_EX                                              !0
         14        SEND_VAR_EX                                              !1
         15        DO_FCALL                                      0  $10     
         16        ASSIGN                                                   !2, $10
    9    17        INIT_FCALL                                               'var_dump'
         18        SEND_VAR                                                 !2
         19        DO_ICALL                                                 
   10    20        ASSIGN                                                   !0, 1300
   11    21        ASSIGN                                                   !1, <array>
   12    22        INIT_FCALL_BY_NAME                                       'optimize'
         23        SEND_VAR_EX                                              !0
         24        SEND_VAR_EX                                              !1
         25        DO_FCALL                                      0  $15     
         26        ASSIGN                                                   !2, $15
   13    27        INIT_FCALL                                               'var_dump'
         28        SEND_VAR                                                 !2
         29        DO_ICALL                                                 
   86    30      > RETURN                                                   1

Function optimize:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 12
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 31
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 31
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 30
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 30
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 56
Branch analysis from position: 34
2 jumps found. (Code = 77) Position 1 = 38, Position 2 = 47
Branch analysis from position: 38
2 jumps found. (Code = 78) Position 1 = 39, Position 2 = 47
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
Branch analysis from position: 47
2 jumps found. (Code = 77) Position 1 = 58, Position 2 = 72
Branch analysis from position: 58
2 jumps found. (Code = 78) Position 1 = 59, Position 2 = 72
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 63, Position 2 = 71
Branch analysis from position: 63
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
Branch analysis from position: 71
Branch analysis from position: 72
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 77
Branch analysis from position: 75
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
2 jumps found. (Code = 77) Position 1 = 80, Position 2 = 109
Branch analysis from position: 80
2 jumps found. (Code = 78) Position 1 = 81, Position 2 = 109
Branch analysis from position: 81
2 jumps found. (Code = 77) Position 1 = 82, Position 2 = 107
Branch analysis from position: 82
2 jumps found. (Code = 78) Position 1 = 83, Position 2 = 107
Branch analysis from position: 83
2 jumps found. (Code = 77) Position 1 = 85, Position 2 = 105
Branch analysis from position: 85
2 jumps found. (Code = 78) Position 1 = 86, Position 2 = 105
Branch analysis from position: 86
2 jumps found. (Code = 46) Position 1 = 89, Position 2 = 95
Branch analysis from position: 89
2 jumps found. (Code = 46) Position 1 = 96, Position 2 = 98
Branch analysis from position: 96
2 jumps found. (Code = 43) Position 1 = 99, Position 2 = 104
Branch analysis from position: 99
1 jumps found. (Code = 42) Position 1 = 85
Branch analysis from position: 85
Branch analysis from position: 104
Branch analysis from position: 98
Branch analysis from position: 95
Branch analysis from position: 105
1 jumps found. (Code = 42) Position 1 = 82
Branch analysis from position: 82
Branch analysis from position: 105
Branch analysis from position: 107
1 jumps found. (Code = 42) Position 1 = 80
Branch analysis from position: 80
Branch analysis from position: 107
Branch analysis from position: 109
2 jumps found. (Code = 77) Position 1 = 112, Position 2 = 117
Branch analysis from position: 112
2 jumps found. (Code = 78) Position 1 = 113, Position 2 = 117
Branch analysis from position: 113
1 jumps found. (Code = 42) Position 1 = 112
Branch analysis from position: 112
Branch analysis from position: 117
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 117
Branch analysis from position: 109
Branch analysis from position: 77
2 jumps found. (Code = 77) Position 1 = 80, Position 2 = 109
Branch analysis from position: 80
Branch analysis from position: 109
Branch analysis from position: 72
Branch analysis from position: 47
Branch analysis from position: 56
Branch analysis from position: 31
Branch analysis from position: 12
filename:       /in/JFc8X
function name:  optimize
number of ops:  126
compiled vars:  !0 = $weight, !1 = $trucks, !2 = $sensitivity, !3 = $weightStore, !4 = $type, !5 = $truckCount, !6 = $capacity, !7 = $exact, !8 = $round, !9 = $ratio, !10 = $max, !11 = $ratioCap, !12 = $key, !13 = $cycle, !14 = $number, !15 = $value, !16 = $capacityUse
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV_INIT                                        !0      0
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      10
   18     3        ASSIGN                                                   !3, !0
   21     4        INIT_FCALL                                               'rsort'
          5        SEND_REF                                                 !1
          6        DO_ICALL                                                 
   24     7      > FE_RESET_R                                       $19     !1, ->12
          8    > > FE_FETCH_R                                               $19, !4, ->12
   25     9    >   ASSIGN_DIM                                               !5, !4
         10        OP_DATA                                                  0
   24    11      > JMP                                                      ->8
         12    >   FE_FREE                                                  $19
   28    13      > FE_RESET_R                                       $21     !1, ->31
         14    > > FE_FETCH_R                                               $21, !4, ->31
   29    15    >   ASSIGN                                                   !6, !4
   30    16        DIV                                              ~23     !0, !4
         17        ASSIGN                                                   !7, ~23
   31    18        INIT_FCALL                                               'floor'
         19        SEND_VAR                                                 !7
         20        DO_ICALL                                         $25     
         21        CAST                                          4  ~26     $25
         22        ASSIGN                                                   !8, ~26
   33    23        IS_SMALLER_OR_EQUAL                                      1, !7
         24      > JMPZ                                                     ~28, ->30
   34    25    >   ASSIGN_DIM                                               !5, !4
         26        OP_DATA                                                  !8
   37    27        MUL                                              ~30     !8, !4
         28        SUB                                              ~31     !0, ~30
         29        ASSIGN                                                   !0, ~31
   28    30    > > JMP                                                      ->14
         31    >   FE_FREE                                                  $21
   42    32        IS_SMALLER                                               0, !0
         33      > JMPZ                                                     ~33, ->56
   43    34    >   INIT_FCALL                                               'rsort'
         35        SEND_REF                                                 !1
         36        DO_ICALL                                                 
   44    37      > FE_RESET_R                                       $35     !1, ->47
         38    > > FE_FETCH_R                                               $35, !4, ->47
   45    39    >   DIV                                              ~37     !0, !4
         40        ASSIGN_DIM                                               !9, !4
         41        OP_DATA                                                  ~37
   46    42        INIT_FCALL                                               'max'
         43        SEND_VAR                                                 !9
         44        DO_ICALL                                         $38     
         45        ASSIGN                                                   !10, $38
   44    46      > JMP                                                      ->38
         47    >   FE_FREE                                                  $35
   48    48        INIT_FCALL                                               'array_search'
         49        SEND_VAR                                                 !10
         50        SEND_VAR                                                 !9
         51        DO_ICALL                                         $40     
         52        ASSIGN                                                   !4, $40
   49    53        FETCH_DIM_RW                                     $42     !5, !4
         54        PRE_INC                                                  $42
   50    55        ASSIGN_OP                                     2          !0, !4
   59    56    >   ASSIGN                                                   !11, <array>
   60    57      > FE_RESET_R                                       $46     !1, ->72
         58    > > FE_FETCH_R                                       ~47     $46, !4, ->72
         59    >   ASSIGN                                                   !12, ~47
   61    60        ADD                                              ~49     !12, 1
         61        ISSET_ISEMPTY_DIM_OBJ                         0          !1, ~49
         62      > JMPZ                                                     ~50, ->71
   62    63    >   ADD                                              ~51     !12, 1
         64        FETCH_DIM_R                                      ~52     !1, ~51
         65        FETCH_DIM_R                                      ~54     !1, !12
         66        ADD                                              ~55     !12, 1
         67        FETCH_DIM_R                                      ~56     !1, ~55
         68        DIV                                              ~57     ~54, ~56
         69        ASSIGN_DIM                                               !11, ~52
         70        OP_DATA                                                  ~57
   60    71    > > JMP                                                      ->58
         72    >   FE_FREE                                                  $46
   67    73        IS_SMALLER_OR_EQUAL                                      !2, 0
         74      > JMPZ                                                     ~58, ->77
         75    >   QM_ASSIGN                                        ~59     10
         76      > JMP                                                      ->78
         77    >   QM_ASSIGN                                        ~59     !2
         78    >   ASSIGN                                                   !2, ~59
   68    79      > FE_RESET_R                                       $61     !1, ->109
         80    > > FE_FETCH_R                                               $61, !13, ->109
   69    81    > > FE_RESET_R                                       $62     !5, ->107
         82    > > FE_FETCH_R                                       ~63     $62, !14, ->107
         83    >   ASSIGN                                                   !4, ~63
   70    84      > FE_RESET_R                                       $65     !11, ->105
         85    > > FE_FETCH_R                                       ~66     $65, !15, ->105
         86    >   ASSIGN                                                   !12, ~66
   71    87        IS_EQUAL                                         ~68     !4, !12
         88      > JMPZ_EX                                          ~68     ~68, ->95
         89    >   INIT_FCALL                                               'floor'
         90        SEND_VAR                                                 !15
         91        DO_ICALL                                         $69     
         92        DIV                                              ~70     $69, !2
         93        IS_SMALLER_OR_EQUAL                              ~71     ~70, !14
         94        BOOL                                             ~68     ~71
         95    > > JMPZ_EX                                          ~68     ~68, ->98
         96    >   IS_NOT_EQUAL                                     ~72     !14, 1
         97        BOOL                                             ~68     ~72
         98    > > JMPZ                                                     ~68, ->104
   72    99    >   ASSIGN_DIM                                               !5, !4
        100        OP_DATA                                                  0
   73   101        MUL                                              ~74     !4, !15
        102        ASSIGN_DIM_OP                +=               1          !5, ~74
        103        OP_DATA                                                  1
   70   104    > > JMP                                                      ->85
        105    >   FE_FREE                                                  $65
   69   106      > JMP                                                      ->82
        107    >   FE_FREE                                                  $62
   68   108      > JMP                                                      ->80
        109    >   FE_FREE                                                  $61
   80   110        ASSIGN                                                   !16, 0
   81   111      > FE_RESET_R                                       $77     !5, ->117
        112    > > FE_FETCH_R                                       ~78     $77, !14, ->117
        113    >   ASSIGN                                                   !4, ~78
   82   114        MUL                                              ~80     !4, !14
        115        ASSIGN_OP                                     1          !16, ~80
   81   116      > JMP                                                      ->112
        117    >   FE_FREE                                                  $77
   83   118        SUB                                              ~82     !3, !16
        119        ASSIGN                                                   !0, ~82
   85   120        INIT_ARRAY                                       ~84     !5, 'trucks'
        121        ADD_ARRAY_ELEMENT                                ~84     !0, 'remaining+weight'
        122        VERIFY_RETURN_TYPE                                       ~84
        123      > RETURN                                                   ~84
   86   124*       VERIFY_RETURN_TYPE                                       
        125*     > RETURN                                                   null

End of function optimize

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.56 ms | 1021 KiB | 18 Q