This commit is contained in:
Armin Friedl 2020-11-30 20:24:22 +01:00
parent 8241fde8d1
commit 1eaa6f2c58
4 changed files with 93 additions and 0 deletions

1
day4/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
build/

14
day4/meson.build Normal file
View file

@ -0,0 +1,14 @@
project('aoc2015_day4', 'cpp',
version: '1.0',
default_options: ['cpp_std=c++17',
'warning_level=3',
'b_ndebug=if-release'])
so1_src = ['so1.cpp']
so2_src = ['so2.cpp']
day4_libs = [dependency('nettle', required: true)]
executable('so1', so1_src, dependencies: day4_libs)
executable('so2', so2_src, dependencies: day4_libs)

39
day4/so1.cpp Normal file
View file

@ -0,0 +1,39 @@
#include <iterator>
#include <string>
#include <iostream>
#include <nettle/md5.h>
using namespace std;
int main() {
string input;
cout << "Initializing" << endl << "Input: ";
cin >> input;
md5_ctx ctx;
md5_init(&ctx);
bool found = false;
unsigned long long n = 1;
while (!found) {
string data = input + to_string(n);
size_t len_total = data.length();
md5_update(&ctx, len_total, (uint8_t*) data.c_str());
uint8_t result[MD5_DIGEST_SIZE];
md5_digest(&ctx, MD5_DIGEST_SIZE, result);
bool has_non_zero = false;
if (result[0] || result[1] || (result[2] & 0xf0)) {
has_non_zero = true;
}
if(!has_non_zero) {
found = true;
cout << "Result: " << n << endl;
}
n++;
}
}

39
day4/so2.cpp Normal file
View file

@ -0,0 +1,39 @@
#include <iostream>
#include <iterator>
#include <nettle/md5.h>
#include <string>
using namespace std;
int main() {
string input;
cout << "Initializing" << endl << "Input: ";
cin >> input;
md5_ctx ctx;
md5_init(&ctx);
bool found = false;
unsigned long long n = 1;
while (!found) {
string data = input + to_string(n);
size_t len_total = data.length();
md5_update(&ctx, len_total, (uint8_t *)data.c_str());
uint8_t result[MD5_DIGEST_SIZE];
md5_digest(&ctx, MD5_DIGEST_SIZE, result);
bool has_non_zero = false;
if (result[0] || result[1] || result[2]) {
has_non_zero = true;
}
if (!has_non_zero) {
found = true;
cout << "Result: " << n << endl;
}
n++;
}
}