main.cpp (1201B)
1 #include <iostream> 2 #include <filesystem> 3 #include <fstream> 4 #include <sstream> 5 #include <vector> 6 #include <cstring> 7 #include <cstdio> 8 #include <tuple> 9 #include <algorithm> 10 #include <openssl/md5.h> 11 #include <openssl/evp.h> 12 13 std::string md5(const std::string &str){ 14 unsigned char hash[MD5_DIGEST_LENGTH]; 15 EVP_Q_digest(NULL, "MD5", NULL, str.c_str(), str.size(), hash, NULL); 16 std::stringstream ss; 17 for(int i = 0; i < MD5_DIGEST_LENGTH; i++){ 18 ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]); 19 } 20 return ss.str(); 21 } 22 23 int solve(const std::string input, const std::string prefix) { 24 for (int i=0; i<INT_MAX; ++i) { 25 if (md5(input+std::to_string(i)).compare(0, prefix.size(), prefix)==0) { 26 return i; 27 } 28 } 29 return 0; 30 } 31 32 int main(int argc, char **argv) { 33 if (argc<2) { 34 std::cerr << "No argument string provided" << std::endl; 35 return -1; 36 } 37 const std::string input = argv[1]; 38 std::cout << "AoC 2015 day 04 " << input << std::endl; 39 std::cout << "part1: " << solve(input, "00000") << std::endl; 40 std::cout << "part2: " << solve(input, "000000") << std::endl; 41 42 return 0; 43 }