public function testExecuteInstructions() { $plateau = new Plateau(5, 5); $rover = new Rover($plateau, 1, 2, Plateau::NORTH, ['L', 'M', 'L', 'M', 'L', 'M', 'L', 'M', 'M']); $rover->executeInstructions(); $this->assertEquals('1 3 N', $rover->getInfo()); $rover = new Rover($plateau, 3, 3, Plateau::EAST, ['M', 'M', 'R', 'M', 'M', 'R', 'M', 'R', 'R', 'M']); $rover->executeInstructions(); $this->assertEquals('5 1 E', $rover->getInfo()); $rover = new Rover($plateau, 5, 5, Plateau::SOUTH, ['M', 'M', 'M', 'M', 'M', 'R', 'M', 'M', 'M', 'M', 'M']); $rover->executeInstructions(); $this->assertEquals('0 0 W', $rover->getInfo()); $rover = new Rover($plateau, 0, 0, Plateau::EAST, ['M', 'M', 'M', 'M', 'M', 'L', 'M', 'M', 'M', 'M', 'M']); $rover->executeInstructions(); $this->assertEquals('5 5 N', $rover->getInfo()); }
<?php include_once __DIR__ . "/Plateau.php"; include_once __DIR__ . "/Rover.php"; $plateauSize = []; $roversInputs = []; $handle = fopen("input.txt", "r"); while (($line = fgets($handle)) !== false) { if (!$plateauSize) { list($plateauSize['x'], $plateauSize['y']) = explode(' ', trim($line)); } else { $roverInput = []; list($roverInput['x'], $roverInput['y'], $roverInput['orientation']) = explode(' ', trim($line)); $line = fgets($handle); $roverInput['instructions'] = str_split(trim($line)); $roversInputs[] = $roverInput; } } foreach ($roversInputs as $roverInput) { $rover = new Rover(new Plateau($plateauSize['x'], $plateauSize['y']), $roverInput['x'], $roverInput['y'], $roverInput['orientation'], $roverInput['instructions']); try { $rover->executeInstructions(); print $rover->getInfo() . "\n"; } catch (Exception $e) { print "Error occurred: " . $e->getMessage(); } }