diff --git a/day14/part1.js b/day14/part1.js new file mode 100644 index 0000000..f7a87c1 --- /dev/null +++ b/day14/part1.js @@ -0,0 +1,46 @@ +var salt = "ihaygndm"; + +var hashes = []; +var toCheck = []; +var checked = []; + +tripleChar = (str)=>(str.match(/(.)\1{2}/) ? (str.match(/(.)\1{2}/)[1]) : false); +fiveChar = (str, char)=>(str.indexOf(char.repeat(5)) > -1); +md5 = (str)=>{ + var md5sum = require("crypto").createHash("md5"); + md5sum.update(str); + return md5sum.digest("hex"); +} + +var i = 0; +while(checked.length < 64){ + var hash = md5(salt + i); + hashes.push(hash); + if(tripleChar(hash)){ + toCheck.push({ + i: i, + char: tripleChar(hash), + hash: hash + }); + } + + var toCheckNow = toCheck.filter((item)=>((item.i + 999) == i)); + toCheck = toCheck.filter((item)=>((item.i + 1000) > i)); + + for(var item of toCheckNow){ + var valid = false; + for(var x = item.i + 1; x < i; x++){ + if(fiveChar(hashes[x], item.char)){ + item.match = x; + item.matchHash = hashes[x]; + valid = true; + } + } + if(valid){ + console.log(checked.length + 1, item); + checked.push(item); + } + } + + i++; +} diff --git a/day14/part2.js b/day14/part2.js new file mode 100644 index 0000000..5e7822a --- /dev/null +++ b/day14/part2.js @@ -0,0 +1,47 @@ +var salt = "ihaygndm"; + +var hashes = []; +var toCheck = []; +var checked = []; + +tripleChar = (str)=>(str.match(/(.)\1{2}/) ? (str.match(/(.)\1{2}/)[1]) : false); +fiveChar = (str, char)=>(str.indexOf(char.repeat(5)) > -1); +md5 = (str)=>{ + var md5sum = require("crypto").createHash("md5"); + md5sum.update(str); + return md5sum.digest("hex"); +} +md5repeat = (str, times)=>((times==0) ? (str) : (md5(md5repeat(str, times-1)))); + +var i = 0; +while(checked.length < 64){ + var hash = md5repeat(salt + i, 2017); + hashes.push(hash); + if(tripleChar(hash)){ + toCheck.push({ + i: i, + char: tripleChar(hash), + hash: hash + }); + } + + var toCheckNow = toCheck.filter((item)=>((item.i + 999) == i)); + toCheck = toCheck.filter((item)=>((item.i + 1000) > i)); + + for(var item of toCheckNow){ + var valid = false; + for(var x = item.i + 1; x < i; x++){ + if(fiveChar(hashes[x], item.char)){ + item.match = x; + item.matchHash = hashes[x]; + valid = true; + } + } + if(valid){ + console.log(checked.length + 1, item); + checked.push(item); + } + } + + i++; +}