Skip to content

Instantly share code, notes, and snippets.

@setni
Created May 13, 2017 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save setni/cb2f67f1f62e169e5588accc9084a511 to your computer and use it in GitHub Desktop.
Save setni/cb2f67f1f62e169e5588accc9084a511 to your computer and use it in GitHub Desktop.
ManoMano
<?php
declare(strict_types = 1);
namespace Tests\Other;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class AutoMowerTest extends WebTestCase {
/**
* Using a file or not for the input
*/
const FILE = true;
/**
* in order of clock
*/
const ORIENTATION = [
'N' => ['y' => 1, 'x' => 0],
'E' => ['y' => 0, 'x' => 1],
'S' => ['y' => -1, 'x' => 0],
'W' => ['y' => 0, 'x' => -1]
];
/**
* Operation on the pointer
*/
const MOVE = ['G' => "prev", 'D' => "next", 'A' => "current"];
/**
* Position of the mower
* @var stdClass
*/
private $_position;
private $_grid;
private $_mower = [];
private $_test = [
'{"x":1,"y":3,"o":"N","mower":1}',
'{"x":5,"y":1,"o":"E","mower":2}',
'{"x":0,"y":2,"o":"N","mower":3}'
];
public function testMoving()
{
$raw = $this->_getInput(__DIR__.DIRECTORY_SEPARATOR."mowerInput.txt");
$this->grid = (object) ['x' => $raw[0][0], 'y' => $raw[0][1]];
$or = self::ORIENTATION;
$len = count($raw);
//mower first id
$m = 1;
for ($i=1; $i < $len; $i++) {
$current = $raw[$i];
if($i%2 === 0) {
//reset pointer
reset($or);
// Set the pointer to the current position
while (key($or) !== $this->_position->o) next($or);
$this->_execute($current, $or);
$this->_mower[$m] = $this->_position;
// Here communication of the position: socket_write/echo ...
echo json_encode($this->_position).PHP_EOL;
//test assertion
$this->assertEquals(current($this->_test), json_encode($this->_position));
next($this->_test);
} else {
$this->_position =
(object) [
'x' => $current[0],
'y' => $current[1],
'o' => $current[2],
'mower' => $m++
];
}
}
}
/**
* all position communication
* @return stdClass position
*/
public function __toString()
{
return $this->_mower;
}
/**
* get the input file content
* @param string $filePath the path of the file
* @return array content of the file
*/
private function _getInput(string $filePath = "")
: array
{
if(self::FILE) {
$raw = explode(PHP_EOL,file_get_contents($filePath));
if(empty($raw[count($raw)-1])) array_pop($raw);
} else {
/*$raw = [
* 66,
* 32W,
* DAAGDAAADG,
* ...
* ]
*/
}
return $raw;
}
/**
* Execute the operation list
* @param int $mower the mowerId
* @param string $instruction all instruction
* @param array &$or all orientation
*/
private function _execute(string $instruction, array &$or)
{
$len = strlen($instruction);
for ($i=0; $i < $len; $i++) {
$current = $instruction[$i];
if($current == "A") {
$ope = current($or);
if($this->_borderChecking($ope)) {
$this->_position->x += $ope['x'];
$this->_position->y += $ope['y'];
$this->_position->o = key($or);
}
} else {
$this->_manageOrientation($current, $or);
}
}
//always unset reference
unset($or);
}
/**
* manage the pointer orientation
* @param string $inst [description]
* @param array &$or [description]
*/
private function _manageOrientation(string $inst, array &$or)
{
$key = key($or);
if(!self::MOVE[$inst]($or)){
if($key === 'N') end($or);
else reset($or);
}
}
private function _borderChecking(array $operation)
: bool
{
$x = $this->_position->x + $operation['x'];
$y = $this->_position->y + $operation['y'];
return !(($x > $this->grid->x || $y > $this->grid->y) || ($x < 0 || $y < 0));
}
}
55
12N
GAGAGAGAA
33E
AADAADADDA
00S
AADDAA
SELECT o.customer_id, count(o.customer_id) as nb, c.first_name, c.last_name
FROM orders o
LEFT JOIN customer c ON o.customer_id = c.id
GROUP BY o.customer_id
HAVING count(o.customer_id) > 1;
-- Attention perf
Target Symfony3
-Quel outil permet d'installer des bundles dans symfony ?
Composer
-Est-ce dangereux d'utiliser app_dev en prod ? Si oui pourquoi ?
Oui, les erreurs sont affichées en environnement dev, non en prod. l'user ne doit pas voir les erreurs.
-Comment importer le schéma d'une table SQL ?
Symfony 3 : php app/console doctrine:schema:create / update : php app/console doctrine:schema:update
Utilisé --force pour pousser en base, --dump-sql pour visualiser les MAJ
-Quelles sont les différentes étapes à suivre pour passer un site avec Symfony en production ?
0) ./composer install ET php app/console doctrine:schema:create --force ET config.yml
1) Vider le cache de prod : php app/console cach:clear --env=prod
2) Dans web/app.php : setter le second param de new AppKernel à true
3) Vérifier la configuration du serveur : http://symfony.com/doc/current/setup/web_server_configuration.html
En bonus : passer son code au validateur Sensio pour avoir la médaille d'or :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment