adventofcode-2017/day14/part1.js
2017-12-16 12:12:56 +00:00

33 lines
898 B
JavaScript

function hash(input){
var list = [];
for(var i = 0; i < 256; i++){
list.push(i);
}
var pos = 0;
var lengths = input.split("").filter((a)=>(a)).map((a)=>(a.charCodeAt(0))).concat([17, 31, 73, 47, 23]);
var skip = 0;
for(var x = 0; x < 64; x++){
for(var length of lengths){
for(var i = 0; i < (length/2); i++){
var a = list[(pos+i)%list.length];
var b = list[(pos+length-1-i)%list.length];
list[(pos+i)%list.length] = b;
list[(pos+length-1-i)%list.length] = a;
}
pos += length + skip;
pos = pos % list.length;
skip += 1;
}
}
var hash = list.reduce((arr, a, i)=>{arr[Math.floor(i/16)] = arr[Math.floor(i/16)] ^ a; return arr;}, []);
return hash.map((a)=>(a.toString(2).split("").map((b)=>(parseInt(b))).reduce((a,b)=>(a+b)))).reduce((a, b)=>(a+b));
}
var total = 0;
for(var i = 0; i < 128; i++){
total += hash("xlqgujun-" + i);
}
console.log(total);