/** * Returns the total brightness of all lights in a {@link LightGrid} * @param LightGrid $grid * @return int total brightness */ public static function totalBrightness(LightGrid $grid) { $sum = 0; foreach ($grid->getGrid() as $row) { $sum += array_sum($row); } return $sum; }
/** * Runs a single {@link LightInstruction} on a {@link LightGrid} applying the * {@link LightInstructionActionBehavior} rules on each light. * * @param LightGrid $lightGrid * @param LightInstruction $instruction * @param LightInstructionActionBehavior $actionBehavior */ public static function run(LightGrid $lightGrid, LightInstruction $instruction, LightInstructionActionBehavior $actionBehavior) { $grid = $lightGrid->getGrid(); $action = $instruction->getAction(); $coordinates = $instruction->getCoordinatePair(); $start = $coordinates[0]; $end = $coordinates[1]; $minY = min($start->getY(), $end->getY()); $yRange = abs($end->getY() - $start->getY()) + 1; for ($x = min($start->getX(), $end->getX()); $x <= max($start->getX(), $end->getX()); $x++) { $lights = array_slice($grid[$x], $minY, $yRange); $lights = array_map($actionBehavior->behaviorForAction($action), $lights); array_splice($grid[$x], $minY, $yRange, $lights); } $lightGrid->setGrid($grid); }