adventofcode-2017/day14/part2.js

62 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-12-16 12:12:56 +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)=>(("00000000" + a.toString(2)).slice(-8).split("").map((b)=>(parseInt(b))))).reduce((a, b)=>(a.concat(b)));
}
var grid = [];
for(var i = 0; i < 128; i++){
grid.push(hash("xlqgujun-" + i));
}
var seen = [];
function calc(x, y){
return (x + (128 * y));
}
function scanNeighbours(x, y){
if(grid[x]){
if(grid[x][y]){
if(seen.indexOf(calc(x,y)) == -1){
seen.push(calc(x,y));
scanNeighbours(x+1,y);
scanNeighbours(x-1,y);
scanNeighbours(x,y+1);
scanNeighbours(x,y-1);
}
}
}
}
var total = 0;
for(var x = 0; x < 128; x++){
for(var y = 0; y < 128; y++){
if(grid[x][y]){
if(seen.indexOf(calc(x,y)) == -1){
total++;
scanNeighbours(x,y);
}
}
}
}
console.log(total);