SHARE
TWEET

colorsheet

a guest Jan 22nd, 2020 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Matrix
  4. {
  5.     private const COLORS = [
  6.         '41',
  7.         '42',
  8.         '43',
  9.         '44',
  10.         '46',
  11.     ];
  12.  
  13.     private $n;
  14.     private $m;
  15.     private $items;
  16.     private $processedMap;
  17.     private $blocks;
  18.  
  19.     public function __construct(int $n, int $m)
  20.     {
  21.         $this->n = $n;
  22.         $this->m = $m;
  23.  
  24.         $this->reset();
  25.     }
  26.  
  27.     public function reset(): void
  28.     {
  29.         $this->items = [];
  30.         $this->processedMap = [];
  31.         $this->blocks = [];
  32.  
  33.         for ($i = 0; $i < $this->n; $i++) {
  34.             $this->items[$i] = [];
  35.             $this->processedMap[$i] = [];
  36.  
  37.             for ($j = 0; $j < $this->m; $j++) {
  38.                 $this->items[$i][$j] = rand(0, count(self::COLORS) - 1);
  39.                 $this->processedMap[$i][$j] = 0;
  40.             }
  41.         }
  42.  
  43.         foreach (self::COLORS as $color => $_) {
  44.             $this->blocks[$color] = 0;
  45.         }
  46.     }
  47.  
  48.     public function print(bool $printHeader = false): void
  49.     {
  50.         if ($printHeader) {
  51.             print 'X';
  52.             for ($i = 0; $i < count($this->items[0]); $i++) {
  53.                 print ' | ' . $i;
  54.             }
  55.             print PHP_EOL;
  56.         }
  57.  
  58.         foreach ($this->items as $j => $row) {
  59.             if ($printHeader) {
  60.                 print $j;
  61.             }
  62.  
  63.             $colorizedRow = array_map([$this, 'getColorizedOutput'], $row);
  64.             print implode('', $colorizedRow) . PHP_EOL;
  65.         }
  66.     }
  67.  
  68.     public function compute(): void
  69.     {
  70.         foreach ($this->items as $i => $row) {
  71.             foreach ($row as $j => $color) {
  72.                 if ($this->processedMap[$i][$j]) {
  73.                     continue;
  74.                 }
  75.  
  76.                 $blockSize = $this->passBlock($i, $j);
  77.                 $this->blocks[$color] = max($this->blocks[$color], $blockSize);
  78.             }
  79.         }
  80.  
  81.         foreach ($this->blocks as $color => $size) {
  82.             print $this->getColorizedOutput($color) . ': ' . $size . PHP_EOL;
  83.         }
  84.     }
  85.  
  86.     private function passBlock(int $i, int $j): int
  87.     {
  88.         if (!isset($this->items[$i][$j]) || $this->processedMap[$i][$j]) {
  89.             return 0;
  90.         }
  91.  
  92.         $this->visit($i, $j);
  93.  
  94.         $possibleNeighbors = [
  95.             [$i + 1, $j],
  96.             [$i - 1, $j],
  97.             [$i, $j + 1],
  98.             [$i, $j - 1],
  99.         ];
  100.  
  101.         $currentStepResult = 1;
  102.         foreach ($possibleNeighbors as $index) {
  103.             list($nearI, $nearJ) = $index;
  104.             if ($this->items[$i][$j] === $this->items[$nearI][$nearJ]) {
  105.                 $currentStepResult += $this->passBlock($nearI, $nearJ);
  106.             }
  107.         }
  108.  
  109.         return $currentStepResult;
  110.     }
  111.  
  112.     private function visit(int $i, int $j): void
  113.     {
  114.         $this->processedMap[$i][$j] = 1;
  115.     }
  116.  
  117.     private function getColorizedOutput(int $color): string
  118.     {
  119.         return "\e[1;37;" . self::COLORS[$color] . "m " . $color . " \e[0m";
  120.     }
  121. }
  122.  
  123. $matrix = new Matrix(40, 50);
  124. $matrix->print();
  125. $matrix->compute();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top