aboutsummaryrefslogtreecommitdiff
path: root/2015/src/day04.cpp
blob: bc2417783b2aec2c6e9adce01f00c9f0301ff7a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Standard C++
#include <array>
#include <fstream>
#include <iostream>
#include <string>

// Standard C
#include <cstring>

// System
#include <md5.h>

using namespace std;

void
brute_force(string_view puzzle, size_t length)
{
	array<char, MD5_DIGEST_STRING_LENGTH> digest{};

	string input;
	for ( unsigned long counter = 0;; ++counter ) {
		input = puzzle;
		input += to_string(counter);
		MD5Data(input.data(), input.size(), digest.data());

		if ( memcmp(digest.data(), "00000000000", length) == 0 ) {
			cout << digest.data() << ": " << counter << endl;
			return;
		}
	}
}

void
part1(string_view puzzle)
{
	brute_force(puzzle, 5);
}

void
part2(string_view puzzle)
{
	brute_force(puzzle, 6);
}

int
main()
{
	string puzzle = "yzbqklnj";

	part1(puzzle);
	part2(puzzle);
}