aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-10 14:56:42 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-10 14:56:42 +0800
commitfd91857910e637f6d813a19b745df7808b9b25af (patch)
tree435ce0ffd5deac7cdebb758610f833740af3297c /src
parentd4d515b2159dd50b76da060bfa11337bca04feed (diff)
downloadadvent-of-code-fd91857910e637f6d813a19b745df7808b9b25af.tar.gz
advent-of-code-fd91857910e637f6d813a19b745df7808b9b25af.zip
2016 day5
Diffstat (limited to 'src')
-rw-r--r--src/2016/day5/README.md14
-rw-r--r--src/2016/day5/aoc.cpp27
-rw-r--r--src/2016/day5/aoc.h1
3 files changed, 42 insertions, 0 deletions
diff --git a/src/2016/day5/README.md b/src/2016/day5/README.md
index b255f52..7c38150 100644
--- a/src/2016/day5/README.md
+++ b/src/2016/day5/README.md
@@ -14,4 +14,18 @@ In this example, after continuing this search a total of eight times, the passwo
Given the actual Door ID, what is the password?
+--- Part Two ---
+As the door slides open, you are presented with a second door that uses a slightly more inspired security mechanism. Clearly unimpressed by the last version (in what movie is the password decrypted in order?!), the Easter Bunny engineers have worked out a better solution.
+Instead of simply filling in the password from left to right, the hash now also indicates the position within the password to fill. You still look for hashes that begin with five zeroes; however, now, the sixth character represents the position (0-7), and the seventh character is the character to put in that position.
+
+A hash result of 000001f means that f is the second character in the password. Use only the first result for each position, and ignore invalid positions.
+
+For example, if the Door ID is abc:
+
+The first interesting hash is from abc3231929, which produces 0000015...; so, 5 goes in position 1: _5______.
+In the previous method, 5017308 produced an interesting hash; however, it is ignored, because it specifies an invalid position (8).
+The second interesting hash is at index 5357525, which produces 000004e...; so, e goes in position 4: _5__e___.
+You almost choke on your popcorn as the final character falls into place, producing the password 05ace8e3.
+
+Given the actual Door ID and this new method, what is the password? Be extra proud of your solution if it uses a cinematic "decrypting" animation.
diff --git a/src/2016/day5/aoc.cpp b/src/2016/day5/aoc.cpp
index 55f4247..16c5f70 100644
--- a/src/2016/day5/aoc.cpp
+++ b/src/2016/day5/aoc.cpp
@@ -1,5 +1,32 @@
#include "aoc.h"
+#include "md5.h"
+#include <string.h>
namespace aoc2016 {
+void day5(const char* secret, int len, char pass1[], char pass2[]) {
+ char buf[128] = {0};
+ int l = strlen(secret);
+ memcpy(buf, secret, len);
+ int64_t i = 0;
+ int x1 = len;
+ int x2 = 0;
+ while (i < INT64_MAX && x2 < 8) {
+ sprintf(buf + l, "%zu", i);
+ char* hash = md5sum(buf);
+ if (lead_zeros(hash) >= 5) {
+ printf("%zu %s\n", i, hash);
+ if (x1 > 0) {
+ pass1[len - x1] = hash[5];
+ x1--;
+ }
+ if (hash[5] >= '0' && hash[5] <= '7' && pass2[hash[5] - '0'] == '\0') {
+ pass2[hash[5] - '0'] = hash[6];
+ x2 += 1;
+ }
+ }
+ i++;
+ }
}
+
+} // namespace aoc2016
diff --git a/src/2016/day5/aoc.h b/src/2016/day5/aoc.h
index 4239dbc..1d264c8 100644
--- a/src/2016/day5/aoc.h
+++ b/src/2016/day5/aoc.h
@@ -4,4 +4,5 @@
namespace aoc2016 {
+void day5(const char*, int, char[], char[]);
}