adventofcode-2017/day10/part2.js

28 lines
852 B
JavaScript
Raw Normal View History

2017-12-10 12:18:49 +00:00
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(16))).map((a)=>((a.length == 1) ? ("0" + a) : (a))).join("");
}
console.log(hash("97,167,54,178,2,11,209,174,119,248,254,0,255,1,64,190"));