Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function inicjujSekwencje($minimum, $maximum) {
- global $sekwencja;
- global $dlugosc;
- global $sekwIdx;
- $sekwIdx = 0;
- $sekwencja = range($minimum, $maximum);
- $dlugosc = $maximum - $minimum + 1;
- }
- function nastepna() {
- global $dlugosc;
- global $sekwencja;
- global $sekwIdx;
- if ($sekwIdx === $dlugosc) $sekwIdx = 0;
- return $sekwencja[$sekwIdx++];
- }
- function stworzMatryce($kolumny, $wiersze, $wartosc = null) {
- return array_fill(0, $wiersze, array_fill(0, $kolumny, $wartosc));
- }
- function prostokat_1($kolumny, $wiersze, $minimum, $maximum, $matryca) {
- $x = 0;
- $y = 0;
- $pole = $kolumny * $wiersze;
- $matryca[$y][$x] = nastepna();
- $pole--;
- while ( $pole ) {
- while ( $pole ) {
- $matryca[$y][++$x] = nastepna();
- $pole--;
- if ( $x == $kolumny - 1 || $matryca[$y][$x + 1] !== null ) {
- break;
- }
- }
- while ( $pole ) {
- $matryca[++$y][$x] = nastepna();
- $pole--;
- if ( $y == $wiersze - 1 || $matryca[$y + 1][$x] !== null ) {
- break;
- }
- }
- while ( $pole ) {
- $matryca[$y][--$x] = nastepna();
- $pole--;
- if ( $x == 0 || $matryca[$y][$x - 1] !== null ) {
- break;
- }
- }
- while ( $pole ) {
- $matryca[--$y][$x] = nastepna();
- $pole--;
- if ( $y == 0 || $matryca[$y - 1][$x] !== null ) {
- break;
- }
- }
- }
- return $matryca;
- }
- function prostokat_2($kolumny, $wiersze, $minimum, $maximum) {
- $matryca = array();
- $y = $wiersze - 1;
- $pole = $kolumny * $wiersze;
- while ( $pole ) {
- while ( $pole ) {
- $matryca[$y--][] = nastepna();
- $pole--;
- if ( $y == 0 ) {
- $matryca[$y][] = nastepna();
- $pole--;
- break;
- }
- }
- while ( $pole ) {
- $matryca[$y++][] = nastepna();
- $pole--;
- if ( $y == $wiersze - 1 ) {
- $matryca[$y][] = nastepna();
- $pole--;
- break;
- }
- }
- }
- return $matryca;
- }
- function prostokat_3($kolumny, $wiersze, $minimum, $maximum, $matryca) {
- $y = $wiersze;
- while ( $y-- ) {
- for ($x = 0; $x < $kolumny; $x++) {
- $matryca[$y][$x] = nastepna();
- }
- $matryca[$y] = array_reverse($matryca[$y]);
- }
- return $matryca;
- }
- function wykonaj($kolumny, $wiersze, $minimum, $maximum) {
- $calosc = array();
- if ( $minimum === $maximum ) {
- $tmp = stworzMatryce($kolumny, $wiersze, $minimum);
- $calosc = array($tmp, $tmp, $tmp);
- } else {
- inicjujSekwencje($minimum, $maximum);
- global $sekwIdx;
- $matryca = stworzMatryce($kolumny, $wiersze);
- $calosc[] = prostokat_1($kolumny, $wiersze, $minimum, $maximum, $matryca);
- $sekwIdx = 0;
- $calosc[] = prostokat_2($kolumny, $wiersze, $minimum, $maximum);
- $sekwIdx = 0;
- $calosc[] = prostokat_3($kolumny, $wiersze, $minimum, $maximum, $matryca);
- }
- $wynik = '';
- foreach ($calosc as $value) {
- $temp = '';
- for ($i = 0; $i < $wiersze; $i++) {
- $temp .= implode($value[$i]) . "\n";
- }
- $wynik .= $temp . "\n";
- }
- echo $wynik;
- }
- function modFib($n) {
- $tmp = array();
- for ($i = 0, $a = $n, $b = $n; $i < $n; $i++) {
- list($a, $b) = array($b, $a + $b);
- $tmp[] = $a;
- }
- return $tmp;
- }
- function modRot($znak, $liczbaCofniec) {
- $alfabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
- $calkowitaLiczbaCofniec = stripos($alfabet, $znak) - $liczbaCofniec;
- $reszta = 0;
- if ( $calkowitaLiczbaCofniec < 0 ) {
- $reszta = ceil(abs($calkowitaLiczbaCofniec) / 36);
- }
- return $alfabet{$calkowitaLiczbaCofniec + (36 * $reszta)};
- }
- function odszyfruj($szyfr) {
- $odszyfrowane = implode(array_map('modRot', str_split($szyfr), modFib(strlen($szyfr))));
- preg_match('/K(\d\d?)/', $odszyfrowane, $kolumny);
- preg_match('/W(\d\d?)/', $odszyfrowane, $wiersze);
- preg_match('/A(\d)/', $odszyfrowane, $minimum);
- preg_match('/Z(\d)/', $odszyfrowane, $maximum);
- wykonaj((int)$kolumny[1], (int)$wiersze[1], (int)$minimum[1], (int)$maximum[1]);
- }
- odszyfruj($argv[1]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement