diff --git a/day4/.gitignore b/day4/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/day4/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file diff --git a/day4/meson.build b/day4/meson.build new file mode 100644 index 0000000..02f48e1 --- /dev/null +++ b/day4/meson.build @@ -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) diff --git a/day4/so1.cpp b/day4/so1.cpp new file mode 100644 index 0000000..36e529b --- /dev/null +++ b/day4/so1.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +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++; + } +} diff --git a/day4/so2.cpp b/day4/so2.cpp new file mode 100644 index 0000000..9c80988 --- /dev/null +++ b/day4/so2.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +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++; + } +}