Day 16
This commit is contained in:
parent
485bb2f210
commit
f8cf05425a
14
day16/part1.js
Normal file
14
day16/part1.js
Normal 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
15
day16/part2.js
Normal 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)));
|
Loading…
Reference in New Issue
Block a user