3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Finds pixels with the lowest geometrical distance from given point (x, y) that match the criteria. */ function findGeometricallyClosestPixels(array $grid, int $x, int $y, string $criteria): array { // if grid is empty or we're out of bounds if (empty($grid) || !isset($grid[$y]) || !isset($grid[0][$x])) { return []; } if ($grid[$y][$x] === $criteria) { return [[$x, $y]]; } $result = []; for ($offset = 1; $offset < calculateMaximumOffset($grid, $x, $y); $offset++) { $result = array_merge($result, getClosestPixels($grid, $x, $y, $offset, $criteria)); if (!empty($result)) { return $result; } for ($d = 1; $d <= $offset; $d++) { $result = array_merge($result, getPixelsForNegativeYOffsets($grid, $x, $y, $offset, $d, $criteria)); $result = array_merge($result, getPixelsForPositiveYOffsets($grid, $x, $y, $offset, $d, $criteria)); if (!empty($result)) { return $result; } } } return $result; } function getClosestPixels(array $grid, int $x, int $y, int $offset, string $criteria): array { $result = []; if (isset($grid[$y][$x - $offset]) && $grid[$y][$x - $offset] === $criteria) { $result[] = [$x - $offset, $y]; } if (isset($grid[$y][$x + $offset]) && $grid[$y][$x + $offset] === $criteria) { $result[] = [$x + $offset, $y]; } if (isset($grid[$y - $offset][$x]) && $grid[$y - $offset][$x] === $criteria) { $result[] = [$x, $y - $offset]; } if (isset($grid[$y + $offset][$x]) && $grid[$y + $offset][$x] === $criteria) { $result[] = [$x, $y + $offset]; } return $result; } function getPixelsForNegativeYOffsets(array $grid, int $x, int $y, int $offset, int $d, string $criteria): array { if (!isset($grid[$y - $d])) { return []; } $result = []; if (isset($grid[$y - $d][$x - $offset]) && $grid[$y - $d][$x - $offset] === $criteria) { $result[] = [$x - $offset, $y - $d]; } if (isset($grid[$y - $d][$x + $offset]) && $grid[$y - $d][$x + $offset] === $criteria) { $result[] = [$x + $offset, $y - $d]; } if (!isset($grid[$y - $offset])) { return $result; } if (isset($grid[$y - $offset][$x - $d]) && $grid[$y - $offset][$x - $d] === $criteria) { $result[] = [$x - $d, $y - $offset]; } if (isset($grid[$y - $offset][$x + $d]) && $grid[$y - $offset][$x + $d] === $criteria) { $result[] = [$x + $d, $y - $offset]; } return $result; } function getPixelsForPositiveYOffsets(array $grid, int $x, int $y, int $offset, int $d, string $criteria): array { if (!isset($grid[$y + $d])) { return []; } $result = []; if (isset($grid[$y + $d][$x + $offset]) && $grid[$y + $d][$x + $offset] === $criteria) { $result[] = [$x + $offset, $y + $d]; } if (isset($grid[$y + $d][$x - $offset]) && $grid[$y + $d][$x - $offset] === $criteria) { $result[] = [$x - $offset, $y + $d]; } if (!isset($grid[$y + $offset])) { return $result; } if (isset($grid[$y + $offset][$x - $d]) && $grid[$y + $offset][$x - $d] === $criteria) { $result[] = [$x - $d, $y + $offset]; } if (isset($grid[$y + $offset][$x + $d]) && $grid[$y + $offset][$x + $d] === $criteria) { $result[] = [$x + $d, $y + $offset]; } return $result; } /** * Calculates maximum offset needed to reach the edge of given grid for given starting point. * * Lowest possible offset is right in the middle of the greater grid dimension (width or height). Since grid starts * at zero, if the point is positioned past the middle, then offset will equal its position. Otherwise, it will equal * greater dimension minus its position increased by one (to compensate for the fact that dimensions are measured * starting at 1, but positions start at 0). */ function calculateMaximumOffset(array $grid, int $x, int $y): int { $gridHeight = count($grid); $gridWidth = count($grid[0]); if ($gridHeight >= $gridWidth) { $relevantDimensionValue = $gridHeight; $relevantCoordinateValue = $y; } else { $relevantDimensionValue = $gridWidth; $relevantCoordinateValue = $x; } $half = floor($relevantDimensionValue / 2); if ($relevantCoordinateValue >= $half) { return $relevantCoordinateValue; } return $relevantDimensionValue - ($relevantCoordinateValue + 1); } $heart = [ ['w', 'w', 'w', 'r', 'r', 'w', 'w', 'w', 'w', 'w', 'r', 'r', 'w', 'w', 'w', 'w'], ['w', 'w', 'r', 'r', 'r', 'r', 'w', 'w', 'w', 'r', 'r', 'r', 'r', 'w', 'w', 'w'], ['w', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w'], ['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w'], ['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w'], ['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w'], ['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w'], ['r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w'], ['w', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w'], ['w', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w'], ['w', 'w', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w', 'w'], ['w', 'w', 'w', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w', 'w', 'w'], ['w', 'w', 'w', 'w', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'w', 'w', 'w', 'w', 'w'], ['w', 'w', 'w', 'w', 'w', 'r', 'r', 'r', 'r', 'r', 'w', 'w', 'w', 'w', 'w', 'w'], ['w', 'w', 'w', 'w', 'w', 'w', 'r', 'r', 'r', 'w', 'w', 'w', 'w', 'w', 'w', 'w'], ['w', 'w', 'w', 'w', 'w', 'w', 'w', 'r', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w'], ]; var_dump(findGeometricallyClosestPixels($heart, 3, 0, 'w'));

Abusive script

This script was stopped while abusing our resources

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.110.0030.00620.94
8.3.100.0030.00619.86
8.3.90.0120.00326.77
8.3.80.0060.00319.36
8.3.70.0130.00316.58
8.3.60.0130.00916.75
8.3.50.0050.01324.49
8.3.40.0120.00918.76
8.3.30.0070.01618.84
8.3.20.0040.00424.18
8.3.10.0080.00024.66
8.3.00.0090.00026.16
8.2.230.0050.00522.58
8.2.220.0030.00624.06
8.2.210.0040.00426.77
8.2.200.0050.00518.88
8.2.190.0040.01218.21
8.2.180.0110.00725.92
8.2.170.0100.00618.96
8.2.160.0120.00622.96
8.2.150.0070.00025.66
8.2.140.0060.00324.66
8.2.130.0000.00826.16
8.2.120.0000.00826.35
8.2.110.0040.00720.57
8.2.100.0110.00017.91
8.2.90.0000.00817.75
8.2.80.0000.00918.82
8.2.70.0040.00417.75
8.2.60.0080.00018.05
8.2.50.0040.00418.05
8.2.40.0040.00418.34
8.2.30.0040.00418.18
8.2.20.0040.00418.11
8.2.10.0040.00419.41
8.2.00.0000.00819.38
8.1.290.0060.00330.84
8.1.280.0070.01125.92
8.1.270.0060.00323.99
8.1.260.0040.00428.09
8.1.250.0040.00428.09
8.1.240.0070.00323.82
8.1.230.0080.00420.98
8.1.220.0000.00918.64
8.1.210.0080.00018.77
8.1.200.0050.00517.35
8.1.190.0090.00017.22
8.1.180.0030.00518.10
8.1.170.0030.00520.54
8.1.160.0060.00318.96
8.1.150.0000.00918.88
8.1.140.0000.00819.02
8.1.130.0050.00320.11
8.1.120.0060.00317.57
8.1.110.0070.00517.38
8.1.100.0080.00417.44
8.1.90.0060.00817.46
8.1.80.0060.00717.43
8.1.70.0060.00617.45
8.1.60.0110.00217.58
8.1.50.0090.00517.46
8.1.40.0110.00317.49
8.1.30.0080.00517.66
8.1.20.0080.00617.64
8.1.10.0100.00417.56
8.1.00.0080.00817.43
8.0.300.0090.00318.77
8.0.290.0040.00416.88
8.0.280.0080.00018.52
8.0.270.0050.00318.09
8.0.260.0000.00618.52
8.0.250.0000.01117.02
8.0.240.0060.00616.89
8.0.230.0120.00016.93
8.0.220.0080.00316.89
8.0.210.0040.00716.94
8.0.200.0060.00516.96
8.0.190.0090.00316.92
8.0.180.0100.00116.81
8.0.170.0100.00216.83
8.0.160.0090.00316.88
8.0.150.0100.00216.80
8.0.140.0090.00316.80
8.0.130.0060.00415.08
8.0.120.0080.00416.90
8.0.110.0070.00416.89
8.0.100.0040.00816.89
8.0.90.0060.00716.93
8.0.80.0070.01016.94
8.0.70.0080.00416.82
8.0.60.0070.00516.77
8.0.50.0080.00416.86
8.0.30.0140.00417.02
8.0.20.0100.00717.04
8.0.10.0110.00717.02
8.0.00.0050.00817.02
7.4.330.0030.00315.55
7.4.320.0070.00516.59
7.4.300.0100.00316.54
7.4.290.0090.00316.57
7.4.280.0070.00616.55
7.4.270.0100.00216.63
7.4.260.0060.00515.00
7.4.250.0060.00616.54
7.4.240.0080.00516.43
7.4.230.0080.00416.63
7.4.220.0060.00316.65
7.4.210.0060.01016.60
7.4.200.0070.00516.61
7.4.190.0110.00416.54
7.4.180.0120.00416.57
7.4.160.0100.00516.45
7.4.150.0000.01516.35
7.4.140.0050.01116.38
7.4.130.0100.00516.50
7.4.120.0030.01216.32
7.4.110.0130.00216.31
7.4.100.0080.00616.30
7.4.90.0090.00516.21
7.4.80.0090.00516.24
7.4.70.0110.00516.29
7.4.60.0080.00616.30
7.4.50.0080.01016.19
7.4.40.0080.00916.39
7.4.30.0090.00516.36
7.4.20.0070.00716.24
7.4.10.0070.00716.25
7.4.00.0070.00716.47
7.3.330.0060.00616.33
7.3.320.0050.00614.79
7.3.310.0050.00516.31
7.3.300.0070.00316.34
7.3.290.0090.00616.36
7.3.280.0150.00016.36
7.3.270.0140.00016.39
7.3.260.0100.00616.42
7.3.250.0120.00416.51
7.3.240.0080.00916.50
7.3.230.0100.00716.54
7.3.220.0110.00516.35
7.3.210.0100.00716.44
7.3.200.0100.00616.38
7.3.190.0130.00316.36
7.3.180.0150.00516.54
7.3.170.0140.00516.27
7.3.160.0070.00816.45
7.3.150.0100.00616.39
7.3.140.0080.00816.29
7.3.130.0130.00316.31
7.3.120.0090.00716.40
7.3.110.0090.00716.32
7.3.100.0060.01016.26
7.3.90.0110.00516.42
7.3.80.0120.00416.25
7.3.70.0100.00716.51
7.3.60.0100.00616.44
7.3.50.0100.00616.42
7.3.40.0100.00616.44
7.3.30.0100.00516.45
7.3.20.0120.00416.55
7.3.10.0100.00716.37
7.3.00.0090.00816.49

preferences:
28.18 ms | 403 KiB | 5 Q