3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo PHP_INT_MIN; echo PHP_INT_MIN * -1; class Cell { var $pos; var $val; public function __construct($pos, $val){ $this->pos = $pos; $this->val = $val; } } class Board { var $cells; public function __construct($raw_array){ $this->cells = array(); $i = 0; foreach($raw_array as $row){ $j = 0; $this->cells[$i] = array(); foreach(str_split($row) as $cell){ $this->cells[$i][$j] = new Cell(new Pos($i, $j), $cell); } } } } class Pos { var $x; var $y; public function __construct($x, $y){ $this->x = $x; $this->y = $y; } public function dist($pos){ return (sqrt(pow($this->x - $pos->x, 2) + pow($this->y - $pos-> y, 2))); } } class Move { var $direction; var $value; var $pos; public static function initializeEmpty(){ return (new Move(array(0, 0), "NONE", -1)); } public function __construct($pos, $direction, $value){ $this->pos = $pos; $this->value = $value; $this->direction = $direction; } public function compare($move){ return $move->value > $this->value ? $move : $this; } public static function maxValDirection($moves, $board, $player, $pos, $opPos){ $best = Move::initializeEmpty(); foreach($moves as $m){ $m->setValue($board, $player, $pos, $opPos); $best = $m->compare($best); } return $best; } public function setValue($board, $player, $pos, $opPos){ if ($board->cells[$this->pos->y][$this->pos->x] != CLEAR) $this->value = -10; else{ if ($this->pos->dist($opPos) == 1){ $this->value = 1; } else{ $this->value = 2; } } } } define("CLEAR", chr(45)); define("P1", chr(114)); define("P2", chr(103)); define("WALL", chr(35)); function nextMove($player, $pos, $opPos, $board){ $moves = array(); array_push($moves, new Move(new Pos($pos->x, $pos->y - 1), "UP", -1)); array_push($moves, new Move(new Pos($pos->x, $pos->y + 1), "DOWN", -1)); array_push($moves, new Move(new Pos($pos->x - 1, $pos->y), "LEFT", -1)); array_push($moves, new Move(new Pos($pos->x + 1, $pos->y), "RIGHT", -1)); return Move::maxValDirection($moves, $board, $player, $pos, $opPos)->direction; } function getPlayerPos($player, $pos){ return ord($player) == ord(P1) ? new Pos($pos[1], $pos[0]) : new Pos($pos[3], $pos[2]); } $fp = fopen("php://stdin", "r"); $player = fgets($fp); //echo $player; $pos = trim(fgets($fp)); $pos = split(' ', $pos); $playerPos = getPlayerPos($player, $pos); $opPos = getPlayerPos(ord($player) == ord(P1) ? P2 : P1, $pos); $board = array(); for ($i=0; $i<15; $i++) { fscanf($fp, "%s", $board[$i]); } print_r($board); $board = new Board($board); print_r($board); $move = nextMove($player,$playerPos,$opPos, $board); echo $move; ?>

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.40.0120.00618.72
8.3.30.0110.00418.69
8.3.20.0060.00320.22
8.3.10.0080.00023.66
8.3.00.0040.00419.38
8.2.170.0040.01122.96
8.2.160.0140.00320.39
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0000.00820.47
8.2.120.0040.00426.35
8.2.110.0070.00320.36
8.2.100.0070.00417.72
8.2.90.0050.00319.17
8.2.80.0000.00817.97
8.2.70.0060.00317.63
8.2.60.0040.00418.04
8.2.50.0040.00418.07
8.2.40.0030.00619.34
8.2.30.0030.00620.70
8.2.20.0000.00917.79
8.2.10.0070.00018.14
8.2.00.0050.00217.95
8.1.270.0100.01023.91
8.1.260.0040.00428.09
8.1.250.0080.00028.09
8.1.240.0070.00723.90
8.1.230.0090.00322.07
8.1.220.0000.00917.74
8.1.210.0090.00018.77
8.1.200.0000.01017.35
8.1.190.0000.01117.35
8.1.180.0040.00418.10
8.1.170.0060.00318.61
8.1.160.0000.00722.02
8.1.150.0030.00518.86
8.1.140.0050.00319.51
8.1.130.0000.00717.65
8.1.120.0000.00717.56
8.1.110.0040.00417.47
8.1.100.0060.00317.50
8.1.90.0000.00817.40
8.1.80.0030.00617.48
8.1.70.0080.00017.38
8.1.60.0030.00517.60
8.1.50.0080.00017.67
8.1.40.0000.00817.58
8.1.30.0060.00317.77
8.1.20.0000.00817.70
8.1.10.0040.00417.63
8.1.00.0080.00017.62
8.0.300.0070.00018.77
8.0.290.0030.00516.88
8.0.280.0020.00518.59
8.0.270.0000.00717.39
8.0.260.0000.00717.38
8.0.250.0000.00716.97
8.0.240.0000.00716.97
8.0.230.0040.00417.03
8.0.220.0000.00816.91
8.0.210.0030.00517.01
8.0.200.0060.00017.14
8.0.190.0000.00917.07
8.0.180.0040.00417.06
8.0.170.0080.00017.10
8.0.160.0040.00417.00
8.0.150.0030.00516.99
8.0.140.0040.00417.01
8.0.130.0000.00613.54
8.0.120.0080.00017.02
8.0.110.0050.00316.98
8.0.100.0040.00417.04
8.0.90.0000.00717.07
8.0.80.0040.01117.05
8.0.70.0000.00917.03
8.0.60.0050.00316.98
8.0.50.0030.00516.90
8.0.30.0110.01217.11
8.0.20.0030.01617.40
8.0.10.0050.00317.02
8.0.00.0100.00916.91
7.4.330.0000.00516.71
7.4.320.0040.00416.61
7.4.300.0060.00016.57
7.4.290.0040.00216.59
7.4.280.0000.00916.58
7.4.270.0030.00316.57
7.4.260.0080.00016.65
7.4.250.0040.00416.44
7.4.240.0020.00616.64
7.4.230.0070.00016.66
7.4.220.0070.01116.69
7.4.210.0110.00716.54
7.4.200.0000.00716.75
7.4.160.0080.00816.59
7.4.150.0030.01617.40
7.4.140.0140.00617.86
7.4.130.0100.01116.63
7.4.120.0160.00216.67
7.4.110.0110.00716.57
7.4.100.0070.01816.67
7.4.90.0070.01016.63
7.4.80.0130.00719.39
7.4.70.0040.01416.58
7.4.60.0060.01016.77
7.4.50.0060.01016.46
7.4.40.0060.01816.54
7.4.30.0060.01216.36
7.4.00.0130.00015.07
7.3.330.0030.00313.48
7.3.320.0030.00313.42
7.3.310.0030.00316.54
7.3.300.0070.00016.30
7.3.290.0070.01016.40
7.3.280.0060.01116.45
7.3.270.0120.00617.40
7.3.260.0150.00716.34
7.3.250.0090.01116.44
7.3.240.0100.00716.37
7.3.230.0100.00716.53
7.3.210.0180.00816.42
7.3.200.0100.00716.34
7.3.190.0040.01216.70
7.3.180.0160.00616.47
7.3.170.0110.01116.38
7.3.160.0090.00616.47
7.2.330.0070.01016.63
7.2.320.0120.00616.52
7.2.310.0030.01316.43
7.2.300.0000.01816.44
7.2.290.0040.01616.75
7.2.60.0000.01416.77
7.2.00.0090.00019.49
7.1.200.0030.00715.69
7.1.100.0080.00317.98
7.1.70.0140.00017.10
7.1.60.0070.01819.36
7.1.50.0110.01116.79
7.1.00.0030.07722.44
7.0.200.0000.00916.83
7.0.100.0300.08020.08
7.0.90.0130.06019.91
7.0.80.0030.08719.99
7.0.70.0330.06319.95
7.0.60.0300.07319.91
7.0.50.0270.06720.35
7.0.40.0130.07320.03
7.0.30.0100.07720.10
7.0.20.0200.04720.07
7.0.10.0100.04320.05
7.0.00.0030.07720.07
5.6.280.0030.05721.13
5.6.250.0070.06020.63
5.6.240.0230.06720.64
5.6.230.0070.08020.64
5.6.220.0070.07720.60
5.6.210.0070.08320.57
5.6.200.0070.06721.04
5.6.190.0100.03721.09
5.6.180.0100.07020.95
5.6.170.0030.08721.03
5.6.160.0170.06721.05
5.6.150.0130.08021.11
5.6.140.0100.04020.96
5.6.130.0070.08020.94
5.6.120.0100.08021.08
5.6.110.0070.05021.01
5.6.100.0070.09321.02
5.6.90.0100.08321.09
5.6.80.0200.06720.48
5.6.70.0100.06320.35
5.6.60.0070.07020.48
5.6.50.0130.05720.45
5.6.40.0230.06020.45
5.6.30.0070.07020.43
5.6.20.0070.07320.41
5.6.10.0000.09720.47
5.6.00.0100.08320.46
5.5.380.0100.06320.39
5.5.370.0100.05720.43
5.5.360.0070.08020.36
5.5.350.0070.08020.43
5.5.340.0030.05320.83
5.5.330.0130.05320.74
5.5.320.0030.07720.84
5.5.310.0030.08320.64
5.5.300.0100.06320.91
5.5.290.0130.07020.79
5.5.280.0070.09020.81
5.5.270.0270.05720.82
5.5.260.0170.06720.90
5.5.250.0170.06320.55
5.5.240.0130.04320.27
5.5.230.0070.07020.29
5.5.220.0070.08320.12
5.5.210.0000.09320.18
5.5.200.0100.07720.16
5.5.190.0100.04720.25
5.5.180.0070.08020.00
5.5.160.0100.04720.28
5.5.150.0100.08020.19
5.5.140.0070.08020.15
5.5.130.0070.06720.21
5.5.120.0130.07320.12
5.5.110.0130.05020.11
5.5.100.0170.06320.09
5.5.90.0100.07720.05
5.5.80.0070.05320.08
5.5.70.0000.04720.04
5.5.60.0100.06720.14
5.5.50.0070.05320.17
5.5.40.0070.03719.93
5.5.30.0000.06720.16
5.5.20.0170.07020.00
5.5.10.0000.08719.98
5.5.00.0130.07320.04

preferences:
40.64 ms | 400 KiB | 5 Q