2016 / Day 14
One-Time Pad
Read the original puzzle on adventofcode.com ↗
The solution
solution.c
1#include "aoc.h"
2
3/* Salt <= 32 lowercase alphanumeric bytes, 64th key index <= 65535.
4 * Keep only triplet/quintuple metadata. The 2016 stretch rounds have
5 * fixed 32-byte messages; pack hex pairs into words and unroll 8 rounds.
6 * This avoids byte-conversion loops and stays within the Wasm work cap. */
7enum { INDEXMAX = 65535, LOOKAHEAD = 1000 };
8
9static uint16_t hexpairs[256];
10static int8_t triples[INDEXMAX + LOOKAHEAD + 1];
11static uint16_t quints[INDEXMAX + LOOKAHEAD + 1];
12#define MD5ROL(x, s) (((x) << (s)) | ((x) >> (32 - (s))))
13#define MD5F(x, y, z) (((x) & (y)) | (~(x) & (z)))
14#define MD5G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
15#define MD5H(x, y, z) ((x) ^ (y) ^ (z))
16#define MD5I(x, y, z) ((y) ^ ((x) | ~(z)))
17#define MD5STEP(fn, a, b, c, d, x, s, k) \
18 a = b + MD5ROL(a + fn(b, c, d) + (x) + (uint32_t)(k), (s))
19
20/* One-block MD5 over a pre-padded 64-byte block. The digest comes
21 * back as four little-endian words: bytes 0..3 are st[0].
22 */
23static void md5_64(const uint8_t *blk, uint32_t st[4]) {
24 uint32_t w0, w1, w2, w3, w4, w5, w6, w7;
25 uint32_t w8, w9, w10, w11, w12, w13, w14, w15;
26 uint32_t a, b, c, d;
27
28#define WLOAD(i) \
29 ((uint32_t)blk[4 * (i)] | ((uint32_t)blk[4 * (i) + 1] << 8) | \
30 ((uint32_t)blk[4 * (i) + 2] << 16) | ((uint32_t)blk[4 * (i) + 3] << 24))
31 w0 = WLOAD(0);
32 w1 = WLOAD(1);
33 w2 = WLOAD(2);
34 w3 = WLOAD(3);
35 w4 = WLOAD(4);
36 w5 = WLOAD(5);
37 w6 = WLOAD(6);
38 w7 = WLOAD(7);
39 w8 = WLOAD(8);
40 w9 = WLOAD(9);
41 w10 = WLOAD(10);
42 w11 = WLOAD(11);
43 w12 = WLOAD(12);
44 w13 = WLOAD(13);
45 w14 = WLOAD(14);
46 w15 = WLOAD(15);
47#undef WLOAD
48
49 a = 0x67452301u;
50 b = 0xefcdab89u;
51 c = 0x98badcfeu;
52 d = 0x10325476u;
53
54 MD5STEP(MD5F, a, b, c, d, w0, 7, 0xd76aa478u);
55 MD5STEP(MD5F, d, a, b, c, w1, 12, 0xe8c7b756u);
56 MD5STEP(MD5F, c, d, a, b, w2, 17, 0x242070dbu);
57 MD5STEP(MD5F, b, c, d, a, w3, 22, 0xc1bdceeeu);
58 MD5STEP(MD5F, a, b, c, d, w4, 7, 0xf57c0fafu);
59 MD5STEP(MD5F, d, a, b, c, w5, 12, 0x4787c62au);
60 MD5STEP(MD5F, c, d, a, b, w6, 17, 0xa8304613u);
61 MD5STEP(MD5F, b, c, d, a, w7, 22, 0xfd469501u);
62 MD5STEP(MD5F, a, b, c, d, w8, 7, 0x698098d8u);
63 MD5STEP(MD5F, d, a, b, c, w9, 12, 0x8b44f7afu);
64 MD5STEP(MD5F, c, d, a, b, w10, 17, 0xffff5bb1u);
65 MD5STEP(MD5F, b, c, d, a, w11, 22, 0x895cd7beu);
66 MD5STEP(MD5F, a, b, c, d, w12, 7, 0x6b901122u);
67 MD5STEP(MD5F, d, a, b, c, w13, 12, 0xfd987193u);
68 MD5STEP(MD5F, c, d, a, b, w14, 17, 0xa679438eu);
69 MD5STEP(MD5F, b, c, d, a, w15, 22, 0x49b40821u);
70 MD5STEP(MD5G, a, b, c, d, w1, 5, 0xf61e2562u);
71 MD5STEP(MD5G, d, a, b, c, w6, 9, 0xc040b340u);
72 MD5STEP(MD5G, c, d, a, b, w11, 14, 0x265e5a51u);
73 MD5STEP(MD5G, b, c, d, a, w0, 20, 0xe9b6c7aau);
74 MD5STEP(MD5G, a, b, c, d, w5, 5, 0xd62f105du);
75 MD5STEP(MD5G, d, a, b, c, w10, 9, 0x02441453u);
76 MD5STEP(MD5G, c, d, a, b, w15, 14, 0xd8a1e681u);
77 MD5STEP(MD5G, b, c, d, a, w4, 20, 0xe7d3fbc8u);
78 MD5STEP(MD5G, a, b, c, d, w9, 5, 0x21e1cde6u);
79 MD5STEP(MD5G, d, a, b, c, w14, 9, 0xc33707d6u);
80 MD5STEP(MD5G, c, d, a, b, w3, 14, 0xf4d50d87u);
81 MD5STEP(MD5G, b, c, d, a, w8, 20, 0x455a14edu);
82 MD5STEP(MD5G, a, b, c, d, w13, 5, 0xa9e3e905u);
83 MD5STEP(MD5G, d, a, b, c, w2, 9, 0xfcefa3f8u);
84 MD5STEP(MD5G, c, d, a, b, w7, 14, 0x676f02d9u);
85 MD5STEP(MD5G, b, c, d, a, w12, 20, 0x8d2a4c8au);
86 MD5STEP(MD5H, a, b, c, d, w5, 4, 0xfffa3942u);
87 MD5STEP(MD5H, d, a, b, c, w8, 11, 0x8771f681u);
88 MD5STEP(MD5H, c, d, a, b, w11, 16, 0x6d9d6122u);
89 MD5STEP(MD5H, b, c, d, a, w14, 23, 0xfde5380cu);
90 MD5STEP(MD5H, a, b, c, d, w1, 4, 0xa4beea44u);
91 MD5STEP(MD5H, d, a, b, c, w4, 11, 0x4bdecfa9u);
92 MD5STEP(MD5H, c, d, a, b, w7, 16, 0xf6bb4b60u);
93 MD5STEP(MD5H, b, c, d, a, w10, 23, 0xbebfbc70u);
94 MD5STEP(MD5H, a, b, c, d, w13, 4, 0x289b7ec6u);
95 MD5STEP(MD5H, d, a, b, c, w0, 11, 0xeaa127fau);
96 MD5STEP(MD5H, c, d, a, b, w3, 16, 0xd4ef3085u);
97 MD5STEP(MD5H, b, c, d, a, w6, 23, 0x04881d05u);
98 MD5STEP(MD5H, a, b, c, d, w9, 4, 0xd9d4d039u);
99 MD5STEP(MD5H, d, a, b, c, w12, 11, 0xe6db99e5u);
100 MD5STEP(MD5H, c, d, a, b, w15, 16, 0x1fa27cf8u);
101 MD5STEP(MD5H, b, c, d, a, w2, 23, 0xc4ac5665u);
102 MD5STEP(MD5I, a, b, c, d, w0, 6, 0xf4292244u);
103 MD5STEP(MD5I, d, a, b, c, w7, 10, 0x432aff97u);
104 MD5STEP(MD5I, c, d, a, b, w14, 15, 0xab9423a7u);
105 MD5STEP(MD5I, b, c, d, a, w5, 21, 0xfc93a039u);
106 MD5STEP(MD5I, a, b, c, d, w12, 6, 0x655b59c3u);
107 MD5STEP(MD5I, d, a, b, c, w3, 10, 0x8f0ccc92u);
108 MD5STEP(MD5I, c, d, a, b, w10, 15, 0xffeff47du);
109 MD5STEP(MD5I, b, c, d, a, w1, 21, 0x85845dd1u);
110 MD5STEP(MD5I, a, b, c, d, w8, 6, 0x6fa87e4fu);
111 MD5STEP(MD5I, d, a, b, c, w15, 10, 0xfe2ce6e0u);
112 MD5STEP(MD5I, c, d, a, b, w6, 15, 0xa3014314u);
113 MD5STEP(MD5I, b, c, d, a, w13, 21, 0x4e0811a1u);
114 MD5STEP(MD5I, a, b, c, d, w4, 6, 0xf7537e82u);
115 MD5STEP(MD5I, d, a, b, c, w11, 10, 0xbd3af235u);
116 MD5STEP(MD5I, c, d, a, b, w2, 15, 0x2ad7d2bbu);
117 MD5STEP(MD5I, b, c, d, a, w9, 21, 0xeb86d391u);
118
119 st[0] = a + 0x67452301u;
120 st[1] = b + 0xefcdab89u;
121 st[2] = c + 0x98badcfeu;
122 st[3] = d + 0x10325476u;
123}
124
125static inline __attribute__((always_inline)) void stretch(uint32_t st[4]) {
126 uint32_t w0, w1, w2, w3, w4, w5, w6, w7;
127 uint32_t w8, w9, w10, w11, w12, w13, w14, w15;
128 uint32_t a, b, c, d;
129
130 w0 = (uint32_t)hexpairs[(st[0] >> 0) & 255] |
131 ((uint32_t)hexpairs[(st[0] >> 8) & 255] << 16);
132 w1 = (uint32_t)hexpairs[(st[0] >> 16) & 255] |
133 ((uint32_t)hexpairs[(st[0] >> 24) & 255] << 16);
134 w2 = (uint32_t)hexpairs[(st[1] >> 0) & 255] |
135 ((uint32_t)hexpairs[(st[1] >> 8) & 255] << 16);
136 w3 = (uint32_t)hexpairs[(st[1] >> 16) & 255] |
137 ((uint32_t)hexpairs[(st[1] >> 24) & 255] << 16);
138 w4 = (uint32_t)hexpairs[(st[2] >> 0) & 255] |
139 ((uint32_t)hexpairs[(st[2] >> 8) & 255] << 16);
140 w5 = (uint32_t)hexpairs[(st[2] >> 16) & 255] |
141 ((uint32_t)hexpairs[(st[2] >> 24) & 255] << 16);
142 w6 = (uint32_t)hexpairs[(st[3] >> 0) & 255] |
143 ((uint32_t)hexpairs[(st[3] >> 8) & 255] << 16);
144 w7 = (uint32_t)hexpairs[(st[3] >> 16) & 255] |
145 ((uint32_t)hexpairs[(st[3] >> 24) & 255] << 16);
146 w8 = 128;
147 w9 = 0;
148 w10 = 0;
149 w11 = 0;
150 w12 = 0;
151 w13 = 0;
152 w14 = 256;
153 w15 = 0;
154
155 a = 0x67452301u;
156 b = 0xefcdab89u;
157 c = 0x98badcfeu;
158 d = 0x10325476u;
159
160 MD5STEP(MD5F, a, b, c, d, w0, 7, 0xd76aa478u);
161 MD5STEP(MD5F, d, a, b, c, w1, 12, 0xe8c7b756u);
162 MD5STEP(MD5F, c, d, a, b, w2, 17, 0x242070dbu);
163 MD5STEP(MD5F, b, c, d, a, w3, 22, 0xc1bdceeeu);
164 MD5STEP(MD5F, a, b, c, d, w4, 7, 0xf57c0fafu);
165 MD5STEP(MD5F, d, a, b, c, w5, 12, 0x4787c62au);
166 MD5STEP(MD5F, c, d, a, b, w6, 17, 0xa8304613u);
167 MD5STEP(MD5F, b, c, d, a, w7, 22, 0xfd469501u);
168 MD5STEP(MD5F, a, b, c, d, w8, 7, 0x698098d8u);
169 MD5STEP(MD5F, d, a, b, c, w9, 12, 0x8b44f7afu);
170 MD5STEP(MD5F, c, d, a, b, w10, 17, 0xffff5bb1u);
171 MD5STEP(MD5F, b, c, d, a, w11, 22, 0x895cd7beu);
172 MD5STEP(MD5F, a, b, c, d, w12, 7, 0x6b901122u);
173 MD5STEP(MD5F, d, a, b, c, w13, 12, 0xfd987193u);
174 MD5STEP(MD5F, c, d, a, b, w14, 17, 0xa679438eu);
175 MD5STEP(MD5F, b, c, d, a, w15, 22, 0x49b40821u);
176 MD5STEP(MD5G, a, b, c, d, w1, 5, 0xf61e2562u);
177 MD5STEP(MD5G, d, a, b, c, w6, 9, 0xc040b340u);
178 MD5STEP(MD5G, c, d, a, b, w11, 14, 0x265e5a51u);
179 MD5STEP(MD5G, b, c, d, a, w0, 20, 0xe9b6c7aau);
180 MD5STEP(MD5G, a, b, c, d, w5, 5, 0xd62f105du);
181 MD5STEP(MD5G, d, a, b, c, w10, 9, 0x02441453u);
182 MD5STEP(MD5G, c, d, a, b, w15, 14, 0xd8a1e681u);
183 MD5STEP(MD5G, b, c, d, a, w4, 20, 0xe7d3fbc8u);
184 MD5STEP(MD5G, a, b, c, d, w9, 5, 0x21e1cde6u);
185 MD5STEP(MD5G, d, a, b, c, w14, 9, 0xc33707d6u);
186 MD5STEP(MD5G, c, d, a, b, w3, 14, 0xf4d50d87u);
187 MD5STEP(MD5G, b, c, d, a, w8, 20, 0x455a14edu);
188 MD5STEP(MD5G, a, b, c, d, w13, 5, 0xa9e3e905u);
189 MD5STEP(MD5G, d, a, b, c, w2, 9, 0xfcefa3f8u);
190 MD5STEP(MD5G, c, d, a, b, w7, 14, 0x676f02d9u);
191 MD5STEP(MD5G, b, c, d, a, w12, 20, 0x8d2a4c8au);
192 MD5STEP(MD5H, a, b, c, d, w5, 4, 0xfffa3942u);
193 MD5STEP(MD5H, d, a, b, c, w8, 11, 0x8771f681u);
194 MD5STEP(MD5H, c, d, a, b, w11, 16, 0x6d9d6122u);
195 MD5STEP(MD5H, b, c, d, a, w14, 23, 0xfde5380cu);
196 MD5STEP(MD5H, a, b, c, d, w1, 4, 0xa4beea44u);
197 MD5STEP(MD5H, d, a, b, c, w4, 11, 0x4bdecfa9u);
198 MD5STEP(MD5H, c, d, a, b, w7, 16, 0xf6bb4b60u);
199 MD5STEP(MD5H, b, c, d, a, w10, 23, 0xbebfbc70u);
200 MD5STEP(MD5H, a, b, c, d, w13, 4, 0x289b7ec6u);
201 MD5STEP(MD5H, d, a, b, c, w0, 11, 0xeaa127fau);
202 MD5STEP(MD5H, c, d, a, b, w3, 16, 0xd4ef3085u);
203 MD5STEP(MD5H, b, c, d, a, w6, 23, 0x04881d05u);
204 MD5STEP(MD5H, a, b, c, d, w9, 4, 0xd9d4d039u);
205 MD5STEP(MD5H, d, a, b, c, w12, 11, 0xe6db99e5u);
206 MD5STEP(MD5H, c, d, a, b, w15, 16, 0x1fa27cf8u);
207 MD5STEP(MD5H, b, c, d, a, w2, 23, 0xc4ac5665u);
208 MD5STEP(MD5I, a, b, c, d, w0, 6, 0xf4292244u);
209 MD5STEP(MD5I, d, a, b, c, w7, 10, 0x432aff97u);
210 MD5STEP(MD5I, c, d, a, b, w14, 15, 0xab9423a7u);
211 MD5STEP(MD5I, b, c, d, a, w5, 21, 0xfc93a039u);
212 MD5STEP(MD5I, a, b, c, d, w12, 6, 0x655b59c3u);
213 MD5STEP(MD5I, d, a, b, c, w3, 10, 0x8f0ccc92u);
214 MD5STEP(MD5I, c, d, a, b, w10, 15, 0xffeff47du);
215 MD5STEP(MD5I, b, c, d, a, w1, 21, 0x85845dd1u);
216 MD5STEP(MD5I, a, b, c, d, w8, 6, 0x6fa87e4fu);
217 MD5STEP(MD5I, d, a, b, c, w15, 10, 0xfe2ce6e0u);
218 MD5STEP(MD5I, c, d, a, b, w6, 15, 0xa3014314u);
219 MD5STEP(MD5I, b, c, d, a, w13, 21, 0x4e0811a1u);
220 MD5STEP(MD5I, a, b, c, d, w4, 6, 0xf7537e82u);
221 MD5STEP(MD5I, d, a, b, c, w11, 10, 0xbd3af235u);
222 MD5STEP(MD5I, c, d, a, b, w2, 15, 0x2ad7d2bbu);
223 MD5STEP(MD5I, b, c, d, a, w9, 21, 0xeb86d391u);
224
225 st[0] = a + 0x67452301u;
226 st[1] = b + 0xefcdab89u;
227 st[2] = c + 0x98badcfeu;
228 st[3] = d + 0x10325476u;
229}
230
231static size_t salt_of(slice in, uint8_t salt[32]) {
232 size_t n = in.l;
233 if (n && in.p[n - 1] == '\n') {
234 n--;
235 }
236 if (n && in.p[n - 1] == '\r') {
237 n--;
238 }
239 aoc_assert(n && n <= 32, "bad salt length");
240 for (size_t i = 0; i < n; i++) {
241 aoc_assert((in.p[i] >= 'a' && in.p[i] <= 'z') ||
242 (in.p[i] >= '0' && in.p[i] <= '9'),
243 "bad salt");
244 }
245 memcpy(salt, in.p, n);
246 return n;
247}
248
249static void hash_index(const uint8_t salt[32], size_t len, int index,
250 int extra) {
251 uint8_t block[64] = {0};
252 char digits[AOC_I64_TEXT];
253 char *end = aoc_printf("%d", digits, index);
254 size_t n = (size_t)(end - digits);
255 memcpy(block, salt, len);
256 memcpy(block + len, digits, n);
257 block[len + n] = 128;
258 block[56] = (uint8_t)((len + n) * 8);
259 block[57] = (uint8_t)(((len + n) * 8) >> 8);
260 uint32_t st[4];
261 md5_64(block, st);
262 if (extra) {
263 for (int k = 0; k < 252; k++) {
264 stretch(st);
265 stretch(st);
266 stretch(st);
267 stretch(st);
268 stretch(st);
269 stretch(st);
270 stretch(st);
271 stretch(st);
272 }
273 }
274 uint8_t hex[32];
275 for (int k = 0; k < 16; k++) {
276 uint32_t byte = (st[k / 4] >> (8 * (k % 4))) & 255;
277 hex[2 * k] = (uint8_t)(byte >> 4);
278 hex[2 * k + 1] = (uint8_t)(byte & 15);
279 }
280 triples[index] = -1;
281 quints[index] = 0;
282 for (int k = 0, run = 0; k < 32; k++) {
283 run = k && hex[k] == hex[k - 1] ? run + 1 : 1;
284 if (run == 3 && triples[index] < 0) {
285 triples[index] = (int8_t)hex[k];
286 }
287 if (run >= 5) {
288 quints[index] |= (uint16_t)(1 << hex[k]);
289 }
290 }
291}
292
293static inline __attribute__((always_inline)) void
294count_window_quintuples(int counts[16], int i) {
295 for (int b = 0; b < 16; b++) {
296 counts[b] += (quints[i] >> b) & 1;
297 }
298}
299
300static inline __attribute__((always_inline)) void
301advance_quintuple_window(int counts[16], int i, int next) {
302 for (int b = 0; b < 16; b++) {
303 counts[b] += ((quints[next] >> b) & 1) - ((quints[i + 1] >> b) & 1);
304 }
305}
306
307static slice solve(slice in, int extra) {
308 uint8_t salt[32];
309 size_t len = salt_of(in, salt);
310 static const char hex[] = "0123456789abcdef";
311 for (int k = 0; k < 256; k++) {
312 hexpairs[k] = (uint16_t)((uint8_t)hex[k >> 4] |
313 ((uint16_t)(uint8_t)hex[k & 15] << 8));
314 }
315 int counts[16] = {0}, found = 0;
316 for (int i = 0; i <= LOOKAHEAD; i++) {
317 hash_index(salt, len, i, extra);
318 }
319 for (int i = 1; i <= LOOKAHEAD; i++) {
320 count_window_quintuples(counts, i);
321 }
322 for (int i = 0; i <= INDEXMAX; i++) {
323 if (triples[i] >= 0 && counts[triples[i]] && ++found == 64) {
324 return slice_from_i64(i);
325 }
326 if (i == INDEXMAX) {
327 break;
328 }
329 int next = i + LOOKAHEAD + 1;
330 hash_index(salt, len, next, extra);
331 advance_quintuple_window(counts, i, next);
332 }
333 aoc_assert(0, "no 64 keys within index cap");
334}
335
336slice part1(slice in) {
337 return solve(in, 0);
338}
339
340slice part2(slice in) {
341 return solve(in, 1);
342}
343
344static slice estimate(slice in) {
345 uint8_t salt[32];
346 salt_of(in, salt);
347 return slice_from_i64(INDEXMAX + LOOKAHEAD + 1);
348}
349
350const struct aoc_method aoc_methods[] = {
351 {"estimate", estimate},
352 {0, 0},
353};aoc.h
1#ifndef AOC_H
2#define AOC_H
3
4#include <stdarg.h>
5#include <stddef.h>
6#include <stdint.h>
7#include <stdlib.h>
8#include <string.h>
9
10/* The only required boundary between infra and a solution.
11 * `input` is read-only and is not owned by the solution.
12 * The returned slice is allocated with malloc. It is the only
13 * block still live when the function returns. Infra copies it
14 * and then frees it.
15 */
16typedef struct {
17 uint8_t *p;
18 size_t l;
19} slice;
20
21/* p is null and l is 0. Infra uses this when a run has no output. */
22static const slice empty_slice = {0};
23
24typedef slice (*aoc_fn)(slice);
25
26/* One named method. The name is a C identifier, at most 31
27 * characters, and it is the URL suffix: /YYYY/D/P/<name>.
28 * estimate and span are examples, not a fixed list. A day
29 * exports as many as it wants. The array ends with {0, 0}.
30 * part1 and part2 are not entries in this list.
31 */
32struct aoc_method {
33 const char *name;
34 aoc_fn fn;
35};
36
37slice part1(slice input);
38slice part2(slice input);
39
40extern const struct aoc_method aoc_methods[];
41
42/* Provided by the runtime. Records msg and returns. */
43void aoc_fail(const char *msg);
44
45/* Stops the run when cond is false and reports msg. */
46_Noreturn void aoc_assert_fail(const char *msg);
47#ifndef aoc_assert
48#define aoc_assert(cond, msg) \
49 do { \
50 if (!(cond)) { \
51 aoc_assert_fail(msg); \
52 } \
53 } while (0)
54#endif
55
56/* Decimal text of a uint64_t is at most 20 digits. A sign and a
57 * little spare fit here. Size a number buffer from this.
58 */
59enum { AOC_I64_TEXT = 32 };
60
61/* Decimal digits of v. No sign. d has room for 20 digits. */
62static inline char *aoc_u64_text(char *d, uint64_t v) {
63 char tmp[AOC_I64_TEXT];
64 char *end = tmp + AOC_I64_TEXT;
65 char *p = end;
66 size_t n;
67 do {
68 *--p = (char)('0' + (v % 10ull));
69 v /= 10ull;
70 } while (v);
71 n = (size_t)(end - p);
72 memcpy(d, p, n);
73 return d + n;
74}
75
76/* Decimal digits of v. The minimum int64_t is not negated. */
77static inline char *aoc_i64_text(char *d, int64_t v) {
78 uint64_t u;
79 if (v < 0) {
80 *d++ = '-';
81 u = (uint64_t)(-(v + 1)) + 1ull;
82 } else {
83 u = (uint64_t)v;
84 }
85 return aoc_u64_text(d, u);
86}
87
88/* n values, one space between them. The author knows the order. */
89static inline char *aoc_u64s(char *d, const uint64_t *v, size_t n) {
90 size_t i;
91 for (i = 0; i < n; i++) {
92 if (i) {
93 *d++ = ' ';
94 }
95 d = aoc_u64_text(d, v[i]);
96 }
97 return d;
98}
99
100static inline char *aoc_i64s(char *d, const int64_t *v, size_t n) {
101 size_t i;
102 for (i = 0; i < n; i++) {
103 if (i) {
104 *d++ = ' ';
105 }
106 d = aoc_i64_text(d, v[i]);
107 }
108 return d;
109}
110
111static inline char *aoc_ints(char *d, const int *v, size_t n) {
112 size_t i;
113 for (i = 0; i < n; i++) {
114 if (i) {
115 *d++ = ' ';
116 }
117 d = aoc_i64_text(d, (int64_t)v[i]);
118 }
119 return d;
120}
121
122/* Write fmt at buf. Return the cursor for the next call.
123 * %d takes int. %i takes int64_t. %u takes uint64_t.
124 * %s takes a C string. %% is a percent sign.
125 * A literal run is memcpy.
126 * %D takes (int *, size_t). %I takes (int64_t *, size_t).
127 * %U takes (uint64_t *, size_t). One space between values.
128 */
129static inline char *aoc_vprintf(const char *fmt, char *buf, va_list ap) {
130 while (*fmt) {
131 const char *lit = fmt;
132 size_t n;
133 while (*fmt && *fmt != '%') {
134 fmt++;
135 }
136 n = (size_t)(fmt - lit);
137 if (n) {
138 memcpy(buf, lit, n);
139 buf += n;
140 }
141 if (*fmt != '%') {
142 break;
143 }
144 fmt++;
145 if (*fmt == '%') {
146 *buf++ = '%';
147 fmt++;
148 continue;
149 }
150 if (*fmt == 's') {
151 const char *s = va_arg(ap, const char *);
152 n = strlen(s);
153 if (n) {
154 memcpy(buf, s, n);
155 buf += n;
156 }
157 fmt++;
158 continue;
159 }
160 if (*fmt == 'd') {
161 buf = aoc_i64_text(buf, (int64_t)va_arg(ap, int));
162 fmt++;
163 continue;
164 }
165 if (*fmt == 'i') {
166 buf = aoc_i64_text(buf, va_arg(ap, int64_t));
167 fmt++;
168 continue;
169 }
170 if (*fmt == 'u') {
171 buf = aoc_u64_text(buf, va_arg(ap, uint64_t));
172 fmt++;
173 continue;
174 }
175 if (*fmt == 'D') {
176 const int *v = va_arg(ap, int *);
177 buf = aoc_ints(buf, v, va_arg(ap, size_t));
178 fmt++;
179 continue;
180 }
181 if (*fmt == 'I') {
182 const int64_t *v = va_arg(ap, int64_t *);
183 buf = aoc_i64s(buf, v, va_arg(ap, size_t));
184 fmt++;
185 continue;
186 }
187 if (*fmt == 'U') {
188 const uint64_t *v = va_arg(ap, uint64_t *);
189 buf = aoc_u64s(buf, v, va_arg(ap, size_t));
190 fmt++;
191 continue;
192 }
193 aoc_assert(0, "bad format");
194 }
195 return buf;
196}
197
198static inline char *aoc_printf(const char *fmt, char *buf, ...) {
199 va_list ap;
200 va_start(ap, buf);
201 buf = aoc_vprintf(fmt, buf, ap);
202 va_end(ap);
203 return buf;
204}
205
206/* Copy n bytes of text into a malloc'd slice. */
207static inline slice slice_from_text(const char *s, size_t n) {
208 uint8_t *p = malloc(n);
209 aoc_assert(p, "out of memory");
210 if (n) {
211 memcpy(p, s, n);
212 }
213 return (slice){p, n};
214}
215
216/* Decimal text of v, allocated at AOC_I64_TEXT. The minimum
217 * int64_t is not negated. slice.l is the bytes written.
218 */
219static inline slice slice_from_i64(int64_t v) {
220 char tmp[AOC_I64_TEXT];
221 char *d;
222 uint8_t *p;
223 size_t n;
224 d = aoc_printf("%i", tmp, v);
225 n = (size_t)(d - tmp);
226 p = malloc(AOC_I64_TEXT);
227 aoc_assert(p, "out of memory");
228 memcpy(p, tmp, n);
229 return (slice){p, n};
230}
231
232/* Parse one decimal slice into *out. A leading '-' is allowed.
233 * Returns 1 on success, 0 for invalid text or int64_t overflow.
234 */
235static inline int slice_to_i64(slice s, int64_t *out) {
236 size_t i = 0;
237 int neg = 0;
238 uint64_t u = 0;
239 if (!s.p || s.l == 0 || !out) {
240 return 0;
241 }
242 if (s.p[0] == '-') {
243 neg = 1;
244 i = 1;
245 if (s.l == 1) {
246 return 0;
247 }
248 }
249 for (; i < s.l; i++) {
250 uint8_t c = s.p[i];
251 if (c < '0' || c > '9') {
252 return 0;
253 }
254 uint64_t digit = (uint64_t)(c - '0');
255 uint64_t limit = (uint64_t)INT64_MAX + (uint64_t)neg;
256 if (u > (limit - digit) / 10u) {
257 return 0;
258 }
259 u = u * 10u + digit;
260 }
261 if (neg) {
262 *out = u == (uint64_t)INT64_MAX + 1u ? INT64_MIN : -(int64_t)u;
263 } else {
264 *out = (int64_t)u;
265 }
266 return 1;
267}
268
269#endifThe aha moment
Three equal characters appear in a digest. A key? Not yet. We need a matching run of five somewhere in the next thousand hashes. Start by following that rule literally: take the first triple at index i, then inspect i + 1 through i + 1,000. Even a quintuple in the current digest cannot confirm itself. This is a search whose answer depends on evidence that has not arrived yet.
Now move to the next candidate. Haven't we already hashed nearly everything it wants to inspect? Yes: the lookahead windows overlap almost completely. That waste is our first useful discovery. Compute each digest once and remember its first triple plus every character occurring in a run of at least five. The full hash can then disappear. Be careful to keep all quintuples; the “first only” rule belongs to triples.
The local part-1 search reaches its 64th key at index 35,186. With a complete lookahead window, that requires 36,187 distinct hashes. This gives a concrete baseline for the work. We can scan cached future metadata for each triple, which is already reasonable, or keep sixteen counts describing the current future window. A candidate qualifies when the count for its triple's character is nonzero. Advancing one index removes one digest's quintuple flags and adds one new digest's flags.
A second trap appears if we celebrate keys in the order their confirmations arrive. The example's candidate 92 is confirmed at 200, while candidate 39 waits until 816. Which key came first? The one at 39. Decide candidates in index order after their complete windows are available. One quintuple may validate several earlier candidates, and finding it never consumes that future digest.
Then comes key stretching. The story is candid: everyone gets to wait longer, including us. Each index needs the initial MD5 plus 2,016 more calls, for 2,017 total. Each extra call consumes the previous digest's 32 lowercase hexadecimal characters. The local search uses only 23,430 index digests including lookahead, fewer than part 1, but those represent 47,258,310 MD5 calls. Our earlier cache has become much more valuable. Repeating a thousand-hash lookahead now would mean repeating millions of expensive steps.
There is no demonstrated shortcut through an individual stretching chain: each hash supplies the next one's input. The implementation instead makes that repeated operation cheaper by exploiting its fixed 32-character message shape, packing hexadecimal pairs directly into MD5 words and unrolling rounds. A small trace of the public abc example is a better check of this work than waiting for a complete password and wondering which interpretation was wrong.
Salts are limited to 32 lowercase letters or digits. Candidate indices run through 65,535, with another 1,000 digests available for lookahead; failure to find 64 keys is explicit. The estimate method returns the resulting fixed maximum of 66,536 index digests, not a prediction for the supplied salt. Stretched hashing can exceed the deployed CPU limit despite fitting the separate Wasm work budget, so this bound describes the search supported by the code rather than a promised response time.
See also
Categories for 2015–2024 adapted from u/Boojum’s 500 Stars: A Categorization and Mega-Guide ↗. A day can belong to several categories. Tags from 2025 onward are our additions.
Try your input
One POST, one reply. Your input is not saved.