Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.53 KB | None | 0 0
  1. <?php
  2.  
  3. class Decode
  4. {
  5.     private static $_dict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  6.     private static $_decodedString;
  7.    
  8.     public function __construct($str)
  9.     {
  10.         $str = strtoupper($str);
  11.         $length = strlen($str);
  12.         $fib = self::FibonacciN($length);
  13.         $s = '';
  14.  
  15.         for ($i = 0; $i < $length; ++$i)
  16.         {
  17.             $s .= self::GetCharacter($str[$i], -$fib[$i]);
  18.         }
  19.        
  20.         self::$_decodedString = $s;
  21.     }
  22.    
  23.     public static function GetConfig()
  24.     {
  25.         $result = array();
  26.        
  27.         preg_match('/K(\d)+/', self::$_decodedString, $matches);
  28.         preg_match('/(\d)+/', $matches[0], $matches);
  29.         $result['columns'] = $matches[0];
  30.        
  31.         preg_match('/W(\d)+/', self::$_decodedString, $matches);
  32.         preg_match('/(\d)+/', $matches[0], $matches);
  33.         $result['rows'] = $matches[0];
  34.        
  35.         preg_match('/A(\d)+/', self::$_decodedString, $matches);
  36.         preg_match('/(\d)+/', $matches[0], $matches);
  37.         $result['min'] = $matches[0];
  38.        
  39.         preg_match('/Z(\d)+/', self::$_decodedString, $matches);
  40.         preg_match('/(\d)+/', $matches[0], $matches);
  41.         $result['max'] = $matches[0];
  42.  
  43.         return $result;
  44.     }
  45.  
  46.     public static function GetCharacter($char, $rank)
  47.     {
  48.         $pos = stripos(self::$_dict, $char) + $rank;
  49.        
  50.         if ($pos > 0)
  51.         {
  52.             while ($pos > 36)
  53.             {
  54.                 $pos -= 36;
  55.             }
  56.         }
  57.         else
  58.         {
  59.             while ($pos < 0)
  60.             {
  61.                 $pos = 36 - ($pos * -1);
  62.             }
  63.         }
  64.        
  65.         return self::$_dict[$pos];
  66.     }
  67.    
  68.     public static function FibonacciN($n)
  69.     {
  70.         $a = 0;
  71.         $b = $n;
  72.         $sum = 0;
  73.         $result = array();
  74.        
  75.         for ($i = 0; $i < $n; $i++)
  76.         {
  77.             $sum = $a + $b;
  78.             $a = $b;
  79.             $b = $sum;
  80.             $result[] = $sum;
  81.         }
  82.  
  83.         return $result;
  84.     }
  85. }
  86.  
  87. class Competition
  88. {
  89.     private static $_sequence;
  90.    
  91.     public static function SetConfiguration($configuration)
  92.     {
  93.         if (self::$_sequence == null)
  94.         {
  95.             self::$_sequence = self::SequenceBody(
  96.                 $configuration['min'],
  97.                 $configuration['max'],
  98.                 $configuration['columns'],
  99.                 $configuration['rows']);
  100.         }
  101.     }
  102.    
  103.     private static function Sequence($min, $max)
  104.     {
  105.         return range($min, $max);
  106.     }
  107.    
  108.     private static function SequenceBody($min, $max, $cal, $row)
  109.     {
  110.         $body = array();
  111.         $sequence = self::Sequence($min, $max);
  112.         $length = $row * $cal;
  113.         $totalLength = ($length / count($sequence)) + 1;
  114.        
  115.         for ($i = 0; $i < $totalLength; ++$i)
  116.         {
  117.             $body = array_merge($body, $sequence);
  118.         }
  119.        
  120.         return array_slice($body, 0, $length);
  121.     }
  122.    
  123.     public static function Draw($rectangle, $rows, $cols)
  124.     {
  125.         $s = '';
  126.        
  127.         for($i = 0; $i < $rows; ++$i)
  128.         {
  129.             for($j = 0; $j < $cols; ++$j)
  130.             {
  131.                 $s .= self::$_sequence[$rectangle[$j][$i]];
  132.             }
  133.            
  134.             $s .= "\n";
  135.         }
  136.        
  137.         echo $s;
  138.     }
  139. }
  140.  
  141. class Point
  142. {
  143.     public $x;
  144.     public $y;
  145.    
  146.     public function __construct($x, $y)
  147.     {
  148.         $this->x = $x;
  149.         $this->y = $y;
  150.     }
  151.    
  152.     public function Add(Point $point)
  153.     {
  154.         $this->x += $point->x;
  155.         $this->y += $point->y;
  156.     }
  157.    
  158.     public function Remove(Point $point)
  159.     {
  160.         $this->x -= $point->x;
  161.         $this->y -= $point->y;
  162.     }
  163. }
  164.  
  165. abstract class RectangleBase implements  IRectangle
  166. {
  167.     protected static $_vectorLeft;
  168.     protected static $_vectorRight;
  169.     protected static $_vectorUp;
  170.     protected static $_vectorDown;
  171.     protected $_stepStartPoint;
  172.     protected $_vectorWeight;
  173.     protected $_courses;
  174.     protected $_length;
  175.     protected $_rows;
  176.     protected $_columns;
  177.     protected $_courseIdx;
  178.    
  179.     public function __construct($weight, $configuration)
  180.     {
  181.         if (self::$_vectorLeft == null)
  182.         {
  183.             self::InitVectors();
  184.         }
  185.        
  186.         $this->DetermineCourse();
  187.         $this->_vectorWeight = $weight;
  188.         $this->_rows = $configuration['rows'];
  189.         $this->_columns = $configuration['columns'];
  190.         $this->_length = $configuration['rows'] * $configuration['columns'];
  191.     }
  192.    
  193.     public function Run()
  194.     {
  195.         $result = array();
  196.         $idx = 0;
  197.         $this->_courseIdx = 0;
  198.         $count = count($this->_courses);
  199.        
  200.         while($idx < $this->_length)
  201.         {
  202.             if ($count == $this->_courseIdx)
  203.             {
  204.                 $this->_courseIdx = 0;
  205.             }
  206.            
  207.             $course = $this->_courses[$this->_courseIdx];
  208.             $steps = $this->DetermineStep();
  209.            
  210.             for($i = 0; $i < $steps; ++$i)
  211.             {
  212.                 $result[$this->_stepStartPoint->x][$this->_stepStartPoint->y] = ++$idx - 1;    
  213.                 $this->_stepStartPoint->Add($course);
  214.             }
  215.  
  216.             $this->_stepStartPoint->Remove($course);
  217.             $this->UpdateStepStartPoint();
  218.             $this->_stepStartPoint->Add($this->_vectorWeight);
  219.             ++$this->_courseIdx;
  220.         }      
  221.        
  222.         return $result;
  223.     }
  224.    
  225.     private static function InitVectors()
  226.     {
  227.         self::$_vectorLeft = new Point(-1, 0);
  228.         self::$_vectorRight = new Point(1, 0);
  229.         self::$_vectorUp = new Point(0, -1);
  230.         self::$_vectorDown = new Point(0, 1);
  231.     }
  232. }
  233.  
  234. interface IRectangle
  235. {
  236.     public function DetermineCourse();
  237.     public function DetermineStep();
  238.     public function UpdateStepStartPoint();
  239. }
  240.  
  241. class Snake extends RectangleBase
  242. {
  243.     public function __construct($configuration)
  244.     {
  245.         parent::__construct(new Point(1, 0), $configuration);
  246.         $this->_stepStartPoint = new Point(0, $configuration['rows'] - 1);
  247.     }
  248.    
  249.     public function DetermineCourse()
  250.     {
  251.         $this->_courses[] = self::$_vectorUp;
  252.         $this->_courses[] = self::$_vectorDown;
  253.     }
  254.    
  255.     public function DetermineStep()
  256.     {
  257.         return $this->_rows;
  258.     }
  259.    
  260.     public function UpdateStepStartPoint() { }
  261. }
  262.  
  263. class Rows extends RectangleBase
  264. {
  265.     public function __construct($configuration)
  266.     {
  267.         parent::__construct(new Point($configuration['columns'] - 1, -1), $configuration);
  268.         $this->_stepStartPoint = new Point($configuration['columns'] - 1, $configuration['rows'] - 1);
  269.     }
  270.    
  271.     public function DetermineCourse()
  272.     {
  273.         $this->_courses[] = self::$_vectorLeft;
  274.     }
  275.    
  276.     public function DetermineStep()
  277.     {
  278.         return $this->_columns;
  279.     }
  280.    
  281.     public function UpdateStepStartPoint() { }
  282. }
  283.  
  284. class Spiral extends RectangleBase
  285. {
  286.     private $_spiralRows;
  287.     private $_spiralColumns;
  288.    
  289.     public function __construct($configuration)
  290.     {
  291.         parent::__construct(new Point(0, 0), $configuration);
  292.         $this->_stepStartPoint = new Point(0, 0);
  293.         $this->_spiralRows = $configuration['rows'];
  294.         $this->_spiralColumns = $configuration['columns'];
  295.     }
  296.    
  297.     public function DetermineCourse()
  298.     {
  299.         $this->_courses[] = self::$_vectorRight;
  300.         $this->_courses[] = self::$_vectorDown;
  301.         $this->_courses[] = self::$_vectorLeft;
  302.         $this->_courses[] = self::$_vectorUp;
  303.     }
  304.    
  305.     public function DetermineStep()
  306.     {
  307.         switch($this->_courseIdx)
  308.         {
  309.             case 0:
  310.                 $this->_spiralRows -= 1;
  311.                 return $this->_spiralColumns;
  312.             case 1:
  313.                 $this->_spiralColumns -= 1;
  314.                 return $this->_spiralRows;
  315.             case 2:
  316.                 $this->_spiralRows -= 1;
  317.                 return $this->_spiralColumns;
  318.             case 3:
  319.                 $this->_spiralColumns -= 1;
  320.                 return $this->_spiralRows;
  321.         }
  322.     }
  323.    
  324.     public function UpdateStepStartPoint()
  325.     {
  326.         switch($this->_courseIdx)
  327.         {
  328.             case 0:
  329.                 $this->_stepStartPoint->Add(new Point(0, 1));
  330.                 break;
  331.             case 1:
  332.                 $this->_stepStartPoint->Remove(new Point(1, 0));
  333.                 break;
  334.             case 2:
  335.                 $this->_stepStartPoint->Remove(new Point(0, 1));
  336.                 break;
  337.             case 3:
  338.                 $this->_stepStartPoint->Add(new Point(1, 0));
  339.                 break;
  340.         }
  341.     }
  342. }
  343.  
  344. $konkurs = new Decode('UMUAAM4G9Z'); //new Decode($argv[1]);
  345. $configuration = Decode::GetConfig();
  346.  
  347. Competition::SetConfiguration($configuration);
  348. $rectangle = new Spiral($configuration);
  349. Competition::Draw($rectangle->Run(), $configuration['rows'], $configuration['columns']);
  350. echo "\n";
  351. $rectangle = new Snake($configuration);
  352. Competition::Draw($rectangle->Run(), $configuration['rows'], $configuration['columns']);
  353. echo "\n";
  354. $rectangle = new Rows($configuration);
  355. Competition::Draw($rectangle->Run(), $configuration['rows'], $configuration['columns']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement