Published
Edited
Dec 16, 2020
Insert cell
Insert cell
applyMask = mask => {
const bitMaskValues = mask.split('').reduce(
(acc, val, idx) => {
if (val === 'X') return acc;
const num = BigInt(Math.pow(2, mask.length - idx - 1));
if (val === '1') {
acc[1].push(num);
} else {
acc[0].push(num);
}
return acc;
},
[[], []]
);
return value => {
let newValue = BigInt(value);
newValue = bitMaskValues[1].reduce((val, mask) => val | mask, newValue);
newValue = bitMaskValues[0].reduce((val, mask) => val & ~mask, newValue); // is this a NAND?
return newValue;
};
}
Insert cell
parseInt('10XXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X'.replaceAll('X', 0), 2)
Insert cell
applyMask('XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X')(11)
Insert cell
applyMask('XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X')(101)
Insert cell
applyMask('XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X')(0)
Insert cell
applyMask('1111XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')(0) // this should be a big number
Insert cell
Insert cell
run = input => {
const memory = new Map();
let masker;
input.split('\n').forEach(line => {
if (line.startsWith('mask = ')) {
masker = applyMask(line.substring(7));
} else {
const matches = line.match(/mem\[(\d+)\] = (\d+)$/);
memory.set(parseInt(matches[1]), masker(parseInt(matches[2])));
}
});
// return memory;
return [...memory.values()].reduce((acc, val) => acc + val);
}
Insert cell
run(testInput)
Insert cell
testInput.substring(7)
Insert cell
Insert cell
Insert cell
run(input)
Insert cell
Insert cell
Math.pow(2, 36)
Insert cell
{
let sum = 0;
for (let i = 0; i < 36; i++) {
sum += Math.pow(2, i);
}
return sum;
}
Insert cell
thirtySixBit = n => n & 68719476735n
Insert cell
bitMasksOn = [
0b000000000000000000000000000000000001n,
0b000000000000000000000000000000000010n,
0b000000000000000000000000000000000100n,
0b000000000000000000000000000000001000n,
0b000000000000000000000000000000010000n,
0b000000000000000000000000000000100000n,
0b000000000000000000000000000001000000n,
0b000000000000000000000000000010000000n,
0b000000000000000000000000000100000000n,
0b000000000000000000000000001000000000n,
0b000000000000000000000000010000000000n,
0b000000000000000000000000100000000000n,
0b000000000000000000000001000000000000n,
0b000000000000000000000010000000000000n,
0b000000000000000000000100000000000000n,
0b000000000000000000001000000000000000n,
0b000000000000000000010000000000000000n,
0b000000000000000000100000000000000000n,
0b000000000000000001000000000000000000n,
0b000000000000000010000000000000000000n,
0b000000000000000100000000000000000000n,
0b000000000000001000000000000000000000n,
0b000000000000010000000000000000000000n,
0b000000000000100000000000000000000000n,
0b000000000001000000000000000000000000n,
0b000000000010000000000000000000000000n,
0b000000000100000000000000000000000000n,
0b000000001000000000000000000000000000n,
0b000000010000000000000000000000000000n,
0b000000100000000000000000000000000000n,
0b000001000000000000000000000000000000n,
0b000010000000000000000000000000000000n,
0b000100000000000000000000000000000000n,
0b001000000000000000000000000000000000n,
0b010000000000000000000000000000000000n,
0b100000000000000000000000000000000000n
]
Insert cell
bitMasksOff = [
0b111111111111111111111111111111111110n,
0b111111111111111111111111111111111101n,
0b111111111111111111111111111111111011n,
0b111111111111111111111111111111110111n,
0b111111111111111111111111111111101111n,
0b111111111111111111111111111111011111n,
0b111111111111111111111111111110111111n,
0b111111111111111111111111111101111111n,
0b111111111111111111111111111011111111n,
0b111111111111111111111111110111111111n,
0b111111111111111111111111101111111111n,
0b111111111111111111111111011111111111n,
0b111111111111111111111110111111111111n,
0b111111111111111111111101111111111111n,
0b111111111111111111111011111111111111n,
0b111111111111111111110111111111111111n,
0b111111111111111111101111111111111111n,
0b111111111111111111011111111111111111n,
0b111111111111111110111111111111111111n,
0b111111111111111101111111111111111111n,
0b111111111111111011111111111111111111n,
0b111111111111110111111111111111111111n,
0b111111111111101111111111111111111111n,
0b111111111111011111111111111111111111n,
0b111111111110111111111111111111111111n,
0b111111111101111111111111111111111111n,
0b111111111011111111111111111111111111n,
0b111111110111111111111111111111111111n,
0b111111101111111111111111111111111111n,
0b111111011111111111111111111111111111n,
0b111110111111111111111111111111111111n,
0b111101111111111111111111111111111111n,
0b111011111111111111111111111111111111n,
0b110111111111111111111111111111111111n,
0b101111111111111111111111111111111111n,
0b011111111111111111111111111111111111n
]
Insert cell
// recursive function to get all the bitmasks for Xs
function merryXMask(masks, value) {
let addresses = [value];
for (let i = 0; i < masks.length; i++) {
const mask0 = bitMasksOff[masks[i]],
mask1 = bitMasksOn[masks[i]];
addresses = addresses.flatMap(addr => [addr | mask1, addr & mask0]);
}
return addresses;
// const [head, ...tail] = masks;
// if (tail.length === 0) {
// return [bitMasksOn[head], bitMasksOff[head]];
// }
// const children = merryXMask(tail);

// return children.flatMap(child => {
// return [child | bitMasksOn[head], child & bitMasksOff[head]];
// });
}
Insert cell
BigInt(Math.pow(2, 36)) | BigInt(Math.pow(2, 64))
Insert cell
merryXMask([5n, 0n], 42n | 18n) // Need to | the address with the mask
Insert cell
42n.toString(2) + '\n ' + 18n.toString(2) + '\n ' + (42n | 18n).toString(2)
Insert cell
BigInt(parseInt("000000000000000000000000000000X1001X".replaceAll('X', '0'), 2))
Insert cell
{
const addresses = merryXMask([5n, 1n], 42n | 18n);
// return addresses;
return addresses.map(n => n.toString(2));
}
Insert cell
merryXMask([1n, 2n, 8n], 26n)
Insert cell
"000000000000000000000000000000X1001X"
.split('')
.reduce(
(acc, char, idx, arr) =>
char === 'X' ? [arr.length - idx - 1, ...acc] : acc,
[]
)
Insert cell
applyAddressMask = mask => {
const exes = mask
.split('')
.reduce(
(acc, char, idx, arr) =>
char === 'X' ? [arr.length - idx - 1, ...acc] : acc,
[]
);
const addrBitmask = BigInt(parseInt(mask.replaceAll('X', '0'), 2));

return value => merryXMask(exes, value | addrBitmask);
}
Insert cell
applyAddressMask('000000000000000000000000000000X1001X')(42n)
Insert cell
applyAddressMask('00000000000000000000000000000000X0XX')(26n).sort()
Insert cell
run2 = input => {
const memory = new Map();
let addressMasker;
input.split('\n').forEach(line => {
if (line.startsWith('mask = ')) {
const mask = line.substring(7);
addressMasker = applyAddressMask(mask);
} else {
const matches = line.match(/mem\[(\d+)\] = (\d+)$/);
addressMasker(BigInt(parseInt(matches[1]))).forEach(addr =>
memory.set(addr, parseInt(matches[2]))
);
}
});
// return memory;
return [...memory.values()].reduce((acc, val) => acc + val);
}
Insert cell
run2(`mask = 000000000000000000000000000000X1001X
mem[42] = 100
mask = 00000000000000000000000000000000X0XX
mem[26] = 1`)
Insert cell
run2(input)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more