aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2015/src/day04.cpp52
-rw-r--r--makefile5
2 files changed, 56 insertions, 1 deletions
diff --git a/2015/src/day04.cpp b/2015/src/day04.cpp
new file mode 100644
index 0000000..bc24177
--- /dev/null
+++ b/2015/src/day04.cpp
@@ -0,0 +1,52 @@
1// Standard C++
2#include <array>
3#include <fstream>
4#include <iostream>
5#include <string>
6
7// Standard C
8#include <cstring>
9
10// System
11#include <md5.h>
12
13using namespace std;
14
15void
16brute_force(string_view puzzle, size_t length)
17{
18 array<char, MD5_DIGEST_STRING_LENGTH> digest{};
19
20 string input;
21 for ( unsigned long counter = 0;; ++counter ) {
22 input = puzzle;
23 input += to_string(counter);
24 MD5Data(input.data(), input.size(), digest.data());
25
26 if ( memcmp(digest.data(), "00000000000", length) == 0 ) {
27 cout << digest.data() << ": " << counter << endl;
28 return;
29 }
30 }
31}
32
33void
34part1(string_view puzzle)
35{
36 brute_force(puzzle, 5);
37}
38
39void
40part2(string_view puzzle)
41{
42 brute_force(puzzle, 6);
43}
44
45int
46main()
47{
48 string puzzle = "yzbqklnj";
49
50 part1(puzzle);
51 part2(puzzle);
52}
diff --git a/makefile b/makefile
index d88478f..0c313e7 100644
--- a/makefile
+++ b/makefile
@@ -12,6 +12,9 @@ all: $(patsubst 2015/src/%.cpp,2015/bin/%,$(wildcard 2015/src/*.cpp)) \
122015/bin/%: 2015/src/%.cpp | 2015/bin 122015/bin/%: 2015/src/%.cpp | 2015/bin
13 c++ $(CPPFLAGS) $^ -o $@ 13 c++ $(CPPFLAGS) $^ -o $@
14 14
152015/bin/day04: 2015/src/day04.cpp | 2015/bin
16 c++ $(CPPFLAGS) $^ -lmd -o $@
17
15# 2020 18# 2020
162020/bin/%: 2020/src/%.cpp | 2020/bin 192020/bin/%: 2020/src/%.cpp | 2020/bin
17 c++ $(CPPFLAGS) $^ -o $@ 20 c++ $(CPPFLAGS) $^ -o $@
@@ -28,6 +31,6 @@ all: $(patsubst 2015/src/%.cpp,2015/bin/%,$(wildcard 2015/src/*.cpp)) \
28 c++ $(CPPFLAGS) -Wno-sign-conversion -I/usr/local/include $^ -L/usr/local/lib -lz3 -o $@ 31 c++ $(CPPFLAGS) -Wno-sign-conversion -I/usr/local/include $^ -L/usr/local/lib -lz3 -o $@
29 32
30clean: 33clean:
31 rm -rf 2020/bin 2022/bin 2023/bin 34 rm -rf 2015/bin 2020/bin 2022/bin 2023/bin
32 35
33.PHONY: all clean 36.PHONY: all clean