15 lines
594 B
JavaScript
15 lines
594 B
JavaScript
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)));
|