-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem59.cpp
80 lines (66 loc) · 1.56 KB
/
problem59.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
string alphabet = "bcdefghijklmnopqrstuvwxyza";
bool isValidLetter(char letter)
{
bool const isValid = (letter >= 'a' && letter <='z' ||
letter >= 'A' && letter <= 'Z' ||
letter >= '0' && letter <= '9' ||
letter == ' ' || letter == ',' ||
letter == '.' || letter == ';' ||
letter == ')' || letter == '(' ||
letter == '!' || letter == '?' ||
letter == '\'');
return isValid;
}
int decode(string const& message, string const& key)
{
long sum(0);
auto keyLength = key.length();
for (int i = 0; i<message.length(); i++){
char decodedLetter = char(message[i] ^ key[i % keyLength]);
if (isValidLetter(decodedLetter)){
sum += int(decodedLetter);
} else {
return -1;
}
}
return sum;
}
int main(){
auto message = string();
ifstream s;
s.open("/local/projects/projectEuler/p059_cipher.txt" , ios_base::in);
auto num = string();
while (!s.eof()){
char x;
s>>x;
if (x!=','){
num.push_back(x);
}
if (x==',' ) {
message.push_back(char(std::stoi(num)));
num.clear();
}
}
num.pop_back();
message.push_back(char(std::stoi(num)));
s.close();
auto key = string("xxx");
for (int i = 0; i < alphabet.length(); i++){
for (int j = 0; j < alphabet.length(); j++){
for (int k = 0; k < alphabet.length(); k++){
key[0] = alphabet[i];
key[1] = alphabet[j];
key[2] = alphabet[k];
int sum = decode(message, key);
if (sum!= -1){
cout<<key<<":"<<sum<<endl;
}
}
}
}
return 0;
}