project-euler

https://projecteuler.net/
Log | Files | Refs | README

Euler_59.cpp (917B)


      1 #include <fstream>
      2 #include <numeric>
      3 
      4 #include "Euler.h"
      5 
      6 int Euler::xorDecryption()
      7 {
      8     const int file_size = 1201;
      9 
     10     std::ifstream file;
     11     std::vector<int> cypher;
     12     std::string character;
     13     std::string word = " the ";
     14 
     15     file.open("files/p059_cipher.txt");
     16 
     17     while(getline(file, character, ','))
     18         cypher.push_back(atoi(character.c_str()));
     19 
     20     char message[file_size];
     21     int pwd[3];
     22 
     23     for (pwd[0] = 'a'; pwd[0] < 'z' + 1; ++pwd[0])
     24     {
     25         for (pwd[1] = 'a'; pwd[1] < 'z' + 1; ++pwd[1])
     26         {
     27             for (pwd[2] = 'a'; pwd[2] < 'z' + 1; ++pwd[2])
     28             {
     29                 for (int i = 0; i < file_size; ++i)
     30                     message[i] = cypher[i] ^ pwd[i % 3];
     31 
     32                 if (std::string(message).find(word) != std::string::npos)
     33                     return std::accumulate(message, message + file_size, 0);
     34             }
     35         }
     36     }
     37 
     38     return 0;
     39 }