Skip to content

Instantly share code, notes, and snippets.

@mariusadam
Created November 1, 2016 20:39
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 mariusadam/2928329447900fef9d6b76e3875d2e7d to your computer and use it in GitHub Desktop.
Save mariusadam/2928329447900fef9d6b76e3875d2e7d to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
class IOHandler
{
/**
* @var resource
*/
private $inputDescriptor;
/**
* @var resource
*/
private $outputDescriptor;
/**
* IOHandler constructor.
*
* @param string $in
* @param string $out
*/
public function __construct(string $in = 'php://stdin', string $out = 'php://stdout')
{
$this->inputDescriptor = fopen($in, 'r');
$this->outputDescriptor = fopen($out, 'w');
}
/**
* @param string $prompt
*
* @return int
*/
public function readInt(string $prompt = '') : int
{
$this->write($prompt);
fscanf($this->inputDescriptor, "%d", $ret);
return $ret;
}
/**
* @param $message
*
* @return IOHandler
*/
public function write($message) : IOHandler
{
fwrite($this->outputDescriptor, (string) $message);
return $this;
}
/**
* @param $message
*
* @return IOHandler
*/
public function writeln($message) : IOHandler
{
$this->write($message);
$this->write(PHP_EOL);
return $this;
}
}
class ElementGetter
{
/**
* @var int
*/
private $n;
/**
* ElementGetter constructor.
*
* @param int $n
*/
public function __construct(int $n)
{
$this->n = $n;
}
/**
* @param int $line
* @param int $column
* @return int
*/
public function getElementAt(int $line, int $column) : int
{
if ($this->isAboveAndSecondaryDiagonal($line, $column)) {
return $this->getAboveAndSecondaryDiagonal($line, $column);
} elseif ($this->isBelowSecondaryDiagonal($line, $column)) {
return $this->getBelowSecondaryDiagonal($line, $column);
} else {
return $this->getBelowSecondaryDiagonalWithOneIndex($line, $column);
}
}
/**
* @param int $line
* @param int $column
* @return bool
*/
private function isAboveAndSecondaryDiagonal(int $line, int $column) : bool
{
return $line + $column <= $this->n + 1;
}
/**
* @param int $line
* @param int $column
* @return bool
*/
private function isBelowSecondaryDiagonal(int $line, int $column) : bool
{
return $line + $column > $this->n + 2;
}
/**
* @param int $line
* @param int $column
* @return int
*/
private function getAboveAndSecondaryDiagonal(int $line, int $column) : int
{
return (($line + $column - 2) * ($line + $column - 1)) / 2 + $column;
}
/**
* @param int $line
* @param int $column
* @return int
*/
private function getBelowSecondaryDiagonalWithOneIndex(int$line, int $column) : int{
return $this->getAboveAndSecondaryDiagonal($line-1, $column) + ($this->n - 1);
}
/**
* @param int $line
* @param int $column
* @return int
*/
private function getBelowSecondaryDiagonal(int $line, int $column) : int
{
return $this->getBelowSecondaryDiagonalWithOneIndex( $this->n - $column + 2, $column) +
((($this->n - 2) * ($this->n - 1)) - ((2 * $this->n - $line - $column)* (2 * $this->n - $line - $column + 1))) / 2;
}
}
$handler = new IOHandler();
$n = $handler->readInt("n = ");
$line = $handler->readInt("line = ");
$column = $handler->readInt("column = ");
$getter = new ElementGetter($n);
$handler
->write("The element at line ")
->write($line)
->write(" and column ")
->write($column)
->write(" is: ")
->writeln($getter->getElementAt($line, $column))
;
@mihaiToader
Copy link

Very nice, you did i good job. This is fucking great man, how not to love it? GG man GG

@mariusadam
Copy link
Author

Mihai, your sarcasm always amazes me :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment