Created
December 6, 2015 20:56
-
-
Save Gregoor/40776dc44512e4976afe to your computer and use it in GitHub Desktop.
Advent of Code - Day 4: The Ideal Stocking Stuffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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