Data Update

This commit is contained in:
Ingy döt Net 2023-07-09 17:42:30 -04:00
parent 015c2add84
commit e50b5c3114
206 changed files with 6337 additions and 523 deletions

View file

@ -1,6 +1,7 @@
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
@ -8,7 +9,7 @@ typedef std::pair<int32_t, int32_t> point;
class Bifid {
public:
Bifid(const int32_t& n, const std::string& text) {
Bifid(const int32_t n, std::string_view text) {
if ( text.length() != n * n ) {
throw new std::invalid_argument("Incorrect length of text");
}
@ -20,7 +21,7 @@ public:
int32_t row = 0;
int32_t col = 0;
for ( char ch : text ) {
for ( const char& ch : text ) {
grid[row][col] = ch;
coordinates[ch] = point(row, col);
col += 1;
@ -35,9 +36,9 @@ public:
}
}
std::string encrypt(const std::string& text) {
std::string encrypt(std::string_view text) {
std::vector<int32_t> row_one, row_two;
for ( char ch : text ) {
for ( const char& ch : text ) {
point coordinate = coordinates[ch];
row_one.push_back(coordinate.first);
row_two.push_back(coordinate.second);
@ -51,9 +52,9 @@ public:
return result;
}
std::string decrypt(const std::string& text) {
std::string decrypt(std::string_view text) {
std::vector<int32_t> row;
for ( char ch : text ) {
for ( const char& ch : text ) {
point coordinate = coordinates[ch];
row.push_back(coordinate.first);
row.push_back(coordinate.second);
@ -70,8 +71,8 @@ public:
}
void display() const {
for ( std::vector<char> row : grid ) {
for ( char ch : row ) {
for ( const std::vector<char>& row : grid ) {
for ( const char& ch : row ) {
std::cout << ch << " ";
}
std::cout << std::endl;
@ -82,7 +83,7 @@ private:
std::unordered_map<char, point> coordinates;
};
void runTest(Bifid bifid, std::string message) {
void runTest(Bifid& bifid, std::string_view message) {
std::cout << "Using Polybius square:" << std::endl;
bifid.display();
std::cout << "Message: " << message << std::endl;
@ -94,9 +95,9 @@ void runTest(Bifid bifid, std::string message) {
}
int main() {
const std::string message1 = "ATTACKATDAWN";
const std::string message2 = "FLEEATONCE";
const std::string message3 = "THEINVASIONWILLSTARTONTHEFIRSTOFJANUARY";
const std::string_view message1 = "ATTACKATDAWN";
const std::string_view message2 = "FLEEATONCE";
const std::string_view message3 = "THEINVASIONWILLSTARTONTHEFIRSTOFJANUARY";
Bifid bifid1(5, "ABCDEFGHIKLMNOPQRSTUVWXYZ");
Bifid bifid2(5, "BGWKZQPNDSIOAXEFCLUMTHYVR");

View file

@ -0,0 +1,8 @@
# _nwise is for gojq
def _nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
# Input: a string
# Output: a stream of strings
def chars: explode[] | [.] | implode;

View file

@ -0,0 +1,54 @@
# Input: the message to be encrypted
def encrypt($polybius):
(ascii_upcase | gsub("J"; "I") ) as $m
| {rows: [], cols : [] }
| reduce ($m|chars) as $c (.;
($polybius|index($c)) as $ix
| if $ix
then .rows += [($ix/5)|floor + 1]
| .cols += [($ix%5) + 1]
else .
end )
| reduce (.rows + .cols | _nwise(2)) as $pair ("";
(($pair[0] - 1) * 5 + $pair[1] - 1) as $ix
| . + $polybius[$ix:$ix+1] ) ;
# Input: the message to be decrypted
def decrypt($polybius):
reduce chars as $c ( {rows: [], cols : [] };
($polybius|index($c)) as $ix
| .rows += [($ix/5)|floor + 1]
| .cols += [($ix%5) + 1] )
| ([.rows, .cols] | transpose | flatten) as $lines
| ($lines|length/2) as $count
| $lines[:$count] as $rows
| $lines[$count:] as $cols
| [$rows, $cols] as $d
| reduce range(0; $count) as $i ("";
(($rows[$i] - 1) * 5 + $cols[$i] - 1) as $ix
| . + $polybius[$ix:$ix+1] ) ;
def polys:
def p1: "ABCDEFGHIKLMNOPQRSTUVWXYZ";
def p2: "BGWKZQPNDSIOAXEFCLUMTHYVR";
def p3: "PLAYFIREXMBCDGHKNOQSTUVWZ";
[p1, p2, p2, p3];
def messages: [
"ATTACKATDAWN",
"FLEEATONCE",
"ATTACKATDAWN",
"The invasion will start on the first of January"
];
def task:
range(0; messages|length) as $i
| messages[$i]
| encrypt(polys[$i]) as $encrypted
| ($encrypted | decrypt(polys[$i] )) as $decrypted
| "Message : \(.)",
"Encrypted : \($encrypted)",
"Decrypted : \($decrypted)"
"" ;
task