diff --git a/day16/part1.js b/day16/part1.js new file mode 100644 index 0000000..f9a4771 --- /dev/null +++ b/day16/part1.js @@ -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))); diff --git a/day16/part2.js b/day16/part2.js new file mode 100644 index 0000000..7d1e8c5 --- /dev/null +++ b/day16/part2.js @@ -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)));