#! /usr/bin/perl -w

use strict;

my $buff;
my $delka = 0;
my $text = '';
my %cetnosti;
open FILE,'aktualne.txt';

while (read FILE,$buff,1) {
  $text .= $buff;
  next if ($buff =~ /\n|\d|\s/);
  $delka ++;
}

close FILE;

my $klic;
my %sekvence;

foreach (0..(length($text) - 10)) {
  $klic = substr $text,$_,3;
  $sekvence{$klic} ++;
  $klic = substr $text,$_,4;
  $sekvence{$klic} ++;
  $klic = substr $text,$_,5;
  $sekvence{$klic} ++;
  $klic = substr $text,$_,6;
  $sekvence{$klic} ++;
  $klic = substr $text,$_,7;
  $sekvence{$klic} ++;
  $klic = substr $text,$_,8;
  $sekvence{$klic} ++;
  $klic = substr $text,$_,9;
  $sekvence{$klic} ++;
  $klic = substr $text,$_,10;
  $sekvence{$klic} ++;
}

#foreach (sort {$sekvence{$a} <=> $sekvence{$b}} keys %sekvence) {
#  next if ($sekvence{$_} < 10);
#  print "$_: $sekvence{$_}\n";
#}

my %skupiny;
foreach (0..(length($text) - 1)) {
  my $pismeno = substr($text,$_,1);
  push @{$skupiny{$_ % 3}},$pismeno;
  $cetnosti{$_ % 3}->{$pismeno} ++;
}

#foreach (sort keys %skupiny) {
#  my $skup = $_;
#  print "----- $skup -----\n";
#  foreach (sort {$cetnosti{$skup}->{$a} <=> $cetnosti{$skup}->{$b}} keys %{$cetnosti{$skup}}) {
#    print "\t$_: $cetnosti{$skup}->{$_}\n";
#  }
#}

$klic = 'KPIBO';
foreach (0..(length($text) - 1)) {
  my $posun = ord(substr($klic,$_ % length $klic,1)) - 64;
  my $pismeno = substr $text,$_,1;
#  print ord($pismeno);
  substr $text,$_,1,chr((ord($pismeno) - $posun - 64) % 26 + 65);
}

print "$text\n"
