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"); } }

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.60.0120.00317.13
8.3.50.0110.00718.03
8.3.40.0000.01518.96
8.3.30.0100.01019.31
8.3.20.0000.00820.39
8.3.10.0000.00923.48
8.3.00.0040.00717.63
8.2.180.0090.00618.67
8.2.170.0110.00422.96
8.2.160.0000.01420.65
8.2.150.0040.00424.18
8.2.140.0000.00724.66
8.2.130.0030.01026.16
8.2.120.0040.00422.20
8.2.110.0060.00322.20
8.2.100.0090.00318.03
8.2.90.0000.00819.52
8.2.80.0050.00318.05
8.2.70.0030.00618.00
8.2.60.0060.00317.93
8.2.50.0040.00418.07
8.2.40.0040.00418.16
8.2.30.0000.00718.12
8.2.20.0050.00317.89
8.2.10.0040.00418.27
8.2.00.0110.00017.89
8.1.280.0140.00325.92
8.1.270.0030.00623.97
8.1.260.0000.00826.35
8.1.250.0040.00428.09
8.1.240.0000.00923.79
8.1.230.0000.01119.22
8.1.220.0000.00817.74
8.1.210.0080.00018.77
8.1.200.0030.00617.60
8.1.190.0060.00317.50
8.1.180.0030.00719.14
8.1.170.0040.00418.77
8.1.160.0040.00422.10
8.1.150.0080.00018.64
8.1.140.0040.00417.46
8.1.130.0040.00417.94
8.1.120.0000.00717.52
8.1.110.0000.00817.57
8.1.100.0040.00417.50
8.1.90.0040.00417.36
8.1.80.0040.00417.44
8.1.70.0040.00417.41
8.1.60.0000.00817.71
8.1.50.0040.00417.57
8.1.40.0000.00817.64
8.1.30.0060.00317.59
8.1.20.0040.00417.56
8.1.10.0000.00817.51
8.1.00.0080.00017.63
8.0.300.0000.00820.47
8.0.290.0030.00517.29
8.0.280.0050.00218.53
8.0.270.0030.00317.34
8.0.260.0000.00817.26
8.0.250.0050.00317.02
8.0.240.0000.00716.98
8.0.230.0030.00517.01
8.0.220.0000.00717.07
8.0.210.0000.00717.05
8.0.200.0030.00316.98
8.0.190.0000.00817.16
8.0.180.0120.00017.06
8.0.170.0080.00016.99
8.0.160.0050.00317.12
8.0.150.0000.00716.87
8.0.140.0040.00417.00
8.0.130.0030.00313.45
8.0.120.0000.00816.90
8.0.110.0030.00517.05
8.0.100.0040.00417.09
8.0.90.0090.00017.07
8.0.80.0120.00416.99
8.0.70.0080.00017.05
8.0.60.0050.00216.87
8.0.50.0040.00417.07
8.0.30.0120.00717.20
8.0.20.0100.01117.40
8.0.10.0000.00716.99
8.0.00.0090.00917.00
7.4.330.0000.00515.15
7.4.320.0000.00616.54
7.4.300.0030.00316.52
7.4.290.0000.00816.59
7.4.280.0030.00316.37
7.4.270.0000.00816.43
7.4.260.0000.00716.52
7.4.250.0050.00316.56
7.4.240.0030.00516.61
7.4.230.0030.00316.63
7.4.220.0130.01016.72
7.4.210.0100.00516.58
7.4.200.0030.00316.51
7.4.190.0000.00716.66
7.4.160.0070.01016.59
7.4.150.0060.01317.40
7.4.140.0130.00517.86
7.4.130.0100.00916.55
7.4.120.0150.00216.63
7.4.110.0060.01216.56
7.4.100.0080.01116.48
7.4.90.0090.00916.56
7.4.80.0130.00519.39
7.4.70.0060.01116.67
7.4.60.0140.00316.64
7.4.50.0000.00916.38
7.4.40.0030.01422.77
7.4.30.0070.01016.77
7.4.00.0070.01114.89
7.3.330.0000.00513.52
7.3.320.0030.00313.58
7.3.310.0000.00816.38
7.3.300.0030.00316.55
7.3.290.0110.00816.47
7.3.280.0100.00616.50
7.3.270.0090.00917.40
7.3.260.0140.00316.42
7.3.250.0100.01116.51
7.3.240.0140.00716.61
7.3.230.0090.00916.43
7.3.210.0090.00916.43
7.3.200.0070.01319.39
7.3.190.0070.01116.74
7.3.180.0060.01116.48
7.3.170.0060.01016.50
7.3.160.0040.01216.52
7.3.120.0070.01014.96
7.2.330.0080.01216.86
7.2.320.0140.00916.81
7.2.310.0100.01016.52
7.2.300.0060.01016.62
7.2.290.0090.00916.58
7.2.60.0090.00616.82
7.2.50.0000.01516.96
7.2.00.0070.01019.03
7.1.200.0040.01115.88
7.1.70.0080.00017.04
7.1.60.0130.01019.46
7.1.50.0090.01316.99
7.1.00.0070.07022.34
7.0.200.0040.00416.64
7.0.140.0000.07722.09
7.0.60.0000.04720.09
7.0.50.0130.05317.95
7.0.40.0070.04020.36
7.0.30.0170.04320.27
7.0.20.0270.05720.12
7.0.10.0230.04020.30
7.0.00.0100.07320.27
5.6.280.0000.07720.95
5.6.210.0030.04020.65
5.6.200.0070.08318.14
5.6.190.0030.08720.60
5.6.180.3970.04020.53
5.6.170.0270.05020.55
5.6.160.0030.08720.54
5.6.150.0070.06718.28
5.6.140.0130.06018.18
5.6.130.0170.07318.17
5.6.120.0070.06321.05
5.6.110.0070.08321.04
5.6.100.0100.06321.13
5.6.90.0030.04321.02
5.6.80.0100.08020.53
5.6.70.0000.08320.39
5.5.350.0130.07020.43
5.5.340.0000.04318.01
5.5.330.0000.07320.29
5.5.320.0430.07020.41
5.5.310.0370.07020.34
5.5.300.0000.05018.04
5.5.290.0000.08717.99
5.5.280.0130.05020.76
5.5.270.0100.07720.89
5.5.260.0070.07721.00
5.5.250.0200.07020.71
5.5.240.0100.07020.16
5.4.450.0530.07019.64
5.4.440.0230.08719.16
5.4.430.0230.07719.40
5.4.420.0330.03319.52
5.4.410.0130.04019.07
5.4.400.0200.07319.22
5.4.390.3900.03019.20
5.4.380.0330.03718.97
5.4.370.0300.03718.84
5.4.360.0200.05319.13
5.4.350.0170.03719.14
5.4.340.0170.03719.09
5.4.320.0230.04319.13
5.4.310.0330.03719.09
5.4.300.0270.03719.08
5.4.290.0090.04812.51
5.4.280.0120.04112.40
5.4.270.0100.04712.40
5.4.260.0090.05512.40
5.4.250.0060.05812.41
5.4.240.0080.05812.41
5.4.230.0090.05712.40
5.4.220.0170.09712.40
5.4.210.0250.09412.40
5.4.200.0140.04412.40
5.4.190.0100.04212.39
5.4.180.0100.03912.39
5.4.170.0090.03612.40
5.4.160.0090.04012.40
5.4.150.0130.04512.39
5.4.140.0140.07112.09
5.4.130.0120.04612.07
5.4.120.0090.03912.03
5.4.110.0070.04612.02
5.4.100.0080.03812.03
5.4.90.0040.04112.02
5.4.80.0110.04512.02
5.4.70.0100.04412.02
5.4.60.0040.03812.02
5.4.50.0030.03912.02
5.4.40.0100.03612.01
5.4.30.0060.03812.01
5.4.20.0100.04212.01
5.4.10.0060.04112.01
5.4.00.0060.04211.50
5.3.290.3970.03315.82
5.3.280.0160.04612.72
5.3.270.0100.05412.74
5.3.260.0070.04512.74
5.3.250.0090.05112.73
5.3.240.0160.08612.74
5.3.230.0090.06612.73
5.3.220.0180.06612.70
5.3.210.0120.05912.70
5.3.200.0110.05112.70
5.3.190.0120.08912.70
5.3.180.0150.05212.70
5.3.170.0050.06112.69
5.3.160.0090.05612.70
5.3.150.0140.05412.70
5.3.140.0090.05812.68
5.3.130.0110.05112.68
5.3.120.0110.05212.68
5.3.110.0100.05912.68
5.3.100.0180.05512.16
5.3.90.0100.04712.15
5.3.80.0130.04712.13
5.3.70.0120.04412.14
5.3.60.0130.04012.12
5.3.50.0080.05212.07
5.3.40.0110.05512.07
5.3.30.0090.03512.03
5.3.20.0100.03511.82
5.3.10.0100.03811.77
5.3.00.0120.03611.76
5.2.170.0030.0379.24
5.2.160.0090.0329.23
5.2.150.0150.0379.24
5.2.140.0080.0459.23
5.2.130.0090.0349.20
5.2.120.0110.0359.20
5.2.110.0120.0289.20
5.2.100.0050.0329.19
5.2.90.0070.0349.20
5.2.80.0050.0459.20
5.2.70.0080.0439.20
5.2.60.0090.0419.14
5.2.50.0060.0369.11
5.2.40.0080.0289.09
5.2.30.0100.0299.07
5.2.20.0060.0309.05
5.2.10.0050.0308.96
5.2.00.0050.0318.83
5.1.60.0070.0238.10
5.1.50.0030.0298.10
5.1.40.0060.0258.08
5.1.30.0040.0298.44
5.1.20.0060.0288.45
5.1.10.0060.0278.18
5.1.00.0100.0268.18
5.0.50.0030.0256.66
5.0.40.0070.0176.52
5.0.30.0080.0296.33
5.0.20.0080.0236.30
5.0.10.0060.0296.28
5.0.00.0080.0386.27
4.4.90.0050.0224.78
4.4.80.0080.0184.75
4.4.70.0060.0194.75
4.4.60.0060.0174.75
4.4.50.0030.0154.77
4.4.40.0060.0384.71
4.4.30.0050.0154.76
4.4.20.0030.0184.84
4.4.10.0020.0194.85
4.4.00.0030.0294.76
4.3.110.0060.0204.67
4.3.100.0030.0234.67
4.3.90.0050.0154.63
4.3.80.0030.0274.59
4.3.70.0050.0244.63
4.3.60.0090.0194.63
4.3.50.0060.0214.62
4.3.40.0040.0344.54
4.3.30.0040.0223.31
4.3.20.0050.0213.29
4.3.10.0070.0343.24
4.3.00.0100.02713.91

preferences:
47.32 ms | 400 KiB | 5 Q