Part 1 JS solution, Part 2 coming soon. I am angry with myself because I got stuck for 30 minutes on why my code wasn't giving the correct solution when I forgot to reset the bitmask when a new one is declared -_-
function bin(dec) { return (dec >>> 0).toString(2); } function pad(n, width, z) { z = z || '0'; n = n + ''; return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; } const fs = require("fs") let data = fs.readFileSync("sinput.txt", "utf8").split("\n"); let memslots = {}; let mymask = {}; data.forEach(e => { if (e.split(" = ")[0] == "mask") { let amask = e.split(" = ")[1] mymask = {} for (let i = 0; i < amask.length; i++) { if (amask[i] != "X") { mymask[i] = amask[i]; } } } else { let memtype = e.split("mem[")[1].split("]")[0]; let integr = pad(bin(e.split(" = ")[1]), 36).split("") for (const [idx, val] of Object.entries(mymask)) { integr[Number(idx)] = val; } memslots[memtype] = integr.join("") } }) let total = 0; for (let num of Object.values(memslots)) { total += parseInt(num, 2) } console.log(total)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Part 1 JS solution, Part 2 coming soon. I am angry with myself because I got stuck for 30 minutes on why my code wasn't giving the correct solution when I forgot to reset the bitmask when a new one is declared -_-