day 10
This commit is contained in:
parent
3b686f4759
commit
492bdb29fd
18
day10/part1.js
Normal file
18
day10/part1.js
Normal file
@ -0,0 +1,18 @@
|
||||
var list = [];
|
||||
for(var i = 0; i < 256; i++){
|
||||
list.push(i);
|
||||
}
|
||||
var pos = 0;
|
||||
var lengths = [97,167,54,178,2,11,209,174,119,248,254,0,255,1,64,190];
|
||||
var skip = 0;
|
||||
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;
|
||||
skip += 1;
|
||||
}
|
||||
console.log(list[0] * list[1]);
|
27
day10/part2.js
Normal file
27
day10/part2.js
Normal file
@ -0,0 +1,27 @@
|
||||
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"));
|
Loading…
x
Reference in New Issue
Block a user