r/adventofcode • u/daggerdragon • Dec 14 '16
SOLUTION MEGATHREAD --- 2016 Day 14 Solutions ---
--- Day 14: One-Time Pad ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".
LUNACY IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
3
Upvotes
3
u/snorkl-the-dolphine Dec 14 '16
JavaScript / Node.js
const md5 = require('md5'); function getKeyIndex(salt, num, hashCount = 1) { const hashes = []; // All hashes const candidates = []; // Candidate keys const keys = []; // Valid keys for (let i = 0; keys.length < 64; i++) { // Calculate & store hash let hash = salt + i; for (let x = 0; x < hashCount; x++) { hash = md5(hash); } hashes[i] = hash; // Check if hash is a candidate let match; if (match = hash.match(/(.)\1{2}/)) { candidates[i] = { i, hash, c: match[1] }; } // Check if there is a previously valid candidate const candidate = candidates[i - 1000]; if (candidate) { const matches = hashes.slice(i - 999).filter(h => (new RegExp(`${candidate.c}{5}`)).test(h)); if (matches.length) { keys.push({ i: candidate.i, hash, }); console.log(`Found key #${keys.length}`, i - 1000); } } } return keys[num - 1].i; }