diff options
| -rw-r--r-- | 2015/src/day04.cpp | 52 | ||||
| -rw-r--r-- | makefile | 5 |
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 | |||
| 13 | using namespace std; | ||
| 14 | |||
| 15 | void | ||
| 16 | brute_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 | |||
| 33 | void | ||
| 34 | part1(string_view puzzle) | ||
| 35 | { | ||
| 36 | brute_force(puzzle, 5); | ||
| 37 | } | ||
| 38 | |||
| 39 | void | ||
| 40 | part2(string_view puzzle) | ||
| 41 | { | ||
| 42 | brute_force(puzzle, 6); | ||
| 43 | } | ||
| 44 | |||
| 45 | int | ||
| 46 | main() | ||
| 47 | { | ||
| 48 | string puzzle = "yzbqklnj"; | ||
| 49 | |||
| 50 | part1(puzzle); | ||
| 51 | part2(puzzle); | ||
| 52 | } | ||
| @@ -12,6 +12,9 @@ all: $(patsubst 2015/src/%.cpp,2015/bin/%,$(wildcard 2015/src/*.cpp)) \ | |||
| 12 | 2015/bin/%: 2015/src/%.cpp | 2015/bin | 12 | 2015/bin/%: 2015/src/%.cpp | 2015/bin |
| 13 | c++ $(CPPFLAGS) $^ -o $@ | 13 | c++ $(CPPFLAGS) $^ -o $@ |
| 14 | 14 | ||
| 15 | 2015/bin/day04: 2015/src/day04.cpp | 2015/bin | ||
| 16 | c++ $(CPPFLAGS) $^ -lmd -o $@ | ||
| 17 | |||
| 15 | # 2020 | 18 | # 2020 |
| 16 | 2020/bin/%: 2020/src/%.cpp | 2020/bin | 19 | 2020/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 | ||
| 30 | clean: | 33 | clean: |
| 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 |
