adventofcode-2016/day14/part2.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-12-14 08:51:20 +00:00
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++;
}