This commit is contained in:
Tim Stallard 2016-12-16 07:24:15 +00:00
parent 485bb2f210
commit f8cf05425a
2 changed files with 29 additions and 0 deletions

14
day16/part1.js Normal file
View File

@ -0,0 +1,14 @@
curve = (state)=>(state + "0" + state.split("").reverse().map((char)=>((char == "0") ? "1" : "0")).join(""));
curveLoop = (str, len)=>{
next = curve(str);
return (next.length >= len) ? (next.substr(0, len)) : curveLoop(next, len);
};
checksum = (str)=>(str.split("").reduce((a, b, i, arr)=>{
if(i%2 == 0){
a.push([b, arr[i+1]]);
}
return a;
}, [])).map((pair)=>((pair[0] == pair[1]) ? "1" : "0")).join("");
checksumLoop = (str) => {var sum = checksum(str); return (sum.length%2 == 0) ? (checksumLoop(sum)) : (sum)}
console.log(checksumLoop(curveLoop("01000100010010111", 272)));

15
day16/part2.js Normal file
View File

@ -0,0 +1,15 @@
curve = (state)=>(state + "0" + state.split("").reverse().map((char)=>((char == "0") ? "1" : "0")).join(""));
curveLoop = (str, len)=>{
next = curve(str);
console.log(next.length);
return (next.length >= len) ? (next.substr(0, len)) : curveLoop(next, len);
};
checksum = (str)=>(str.split("").reduce((a, b, i, arr)=>{
if(i%2 == 0){
a.push([b, arr[i+1]]);
}
return a;
}, [])).map((pair)=>((pair[0] == pair[1]) ? "1" : "0")).join("");
checksumLoop = (str) => {var sum = checksum(str); return (sum.length%2 == 0) ? (checksumLoop(sum)) : (sum)}
console.log(checksumLoop(curveLoop("01000100010010111", 35651584)));