bf12e141a6
- Add Md5 algorithm to library
62 lines
1.2 KiB
C++
62 lines
1.2 KiB
C++
#include "common/aoc.h"
|
|
|
|
namespace y2016::day05
|
|
{
|
|
REGISTER_DAY(2016, Day05, std::string, std::string);
|
|
|
|
REGISTER_TEST(2016, Day05, Input, 1, "f97c354d");
|
|
REGISTER_TEST(2016, Day05, Input, 2, "863dde27");
|
|
|
|
READ_INPUT(input)
|
|
{
|
|
std::string str;
|
|
getline(input, str);
|
|
return str;
|
|
}
|
|
|
|
OUTPUT1(input)
|
|
{
|
|
int i = 0;
|
|
std::string result;
|
|
while (true)
|
|
{
|
|
std::string str = input + std::to_string(i);
|
|
std::string s = Md5::Hash(str);
|
|
if (Helper::StartsWith(s, "00000"))
|
|
{
|
|
result.push_back(s[5]);
|
|
if (result.size() == 8)
|
|
return result;
|
|
}
|
|
i++;
|
|
}
|
|
return "";
|
|
}
|
|
|
|
OUTPUT2(input)
|
|
{
|
|
int i = 0;
|
|
std::string result{"XXXXXXXX"};
|
|
int replacements = 0;
|
|
for (int i = 0;; i++)
|
|
{
|
|
std::string str = input + std::to_string(i);
|
|
std::string s = Md5::Hash(str);
|
|
if (Helper::StartsWith(s, "00000"))
|
|
{
|
|
if (s[5] < '0' || s[5] > '7')
|
|
continue;
|
|
|
|
if (result[s[5] - '0'] == 'X')
|
|
{
|
|
result[s[5] - '0'] = s[6];
|
|
replacements++;
|
|
}
|
|
if (replacements == 8)
|
|
return result;
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
}
|