Skip to content

Instantly share code, notes, and snippets.

@Gregoor
Created December 6, 2015 20:56
Show Gist options
  • Save Gregoor/40776dc44512e4976afe to your computer and use it in GitHub Desktop.
Save Gregoor/40776dc44512e4976afe to your computer and use it in GitHub Desktop.
Advent of Code - Day 4: The Ideal Stocking Stuffer
import MD5 from 'crypto-js/md5';
const findNumberForPrefixedMD5 = (str, prefix = '00000') => {
let i = 0;
let hash;
do {
i += 1;
hash = MD5(str + i).toString();
} while (!hash.startsWith(prefix));
return i;
};
const expect = (actual, expected) => {
if (actual !== expected) {
console.error(`Expected ${expected} got ${actual}`);
return false;
}
return true;
};
const tests = [
['abcdef', 609043],
['pqrstuv', 1048970]
].map(([str, expected]) => {
return expect(findNumberForPrefixedMD5(str), expected);
});
if (tests.every(Boolean)) console.log('All tests passed!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment