-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem14b.js
70 lines (65 loc) · 1.85 KB
/
problem14b.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require('fs');
const data = fs.readFileSync('input14.txt', 'utf8');
// const data = `mask = 000000000000000000000000000000X1001X
// mem[42] = 100
// mask = 00000000000000000000000000000000X0XX
// mem[26] = 1`;
const program = data.split('\n').map(line => line.trim()).filter(line => line.length).map(line => line.split(' = '));
const memory = {};
let sum = 0n;
let mask;
let exes = 0n;
for (instruction of program) {
if (instruction[0] === 'mask') {
exes = 0n;
mask = instruction[1];
console.log(mask);
for (let i = 0; (i < mask.length); i++) {
if (mask.charAt(i) === 'X') {
exes++;
}
}
} else if (instruction[0].startsWith('mem')) {
let [ dummy, index ] = instruction[0].match(/^mem\[(\d+)\]$/);
index = BigInt(index);
const value = BigInt(instruction[1]);
let range = 1n << exes;
console.log(range);
for (let i = 0n; (i < range); i++) {
// console.log(i);
let exBit = 1n;
let bit = 1n;
let andWith = 0n;
let orWith = 0n;
for (let j = mask.length - 1; (j >= 0); j--) {
const c = mask.charAt(j);
if (c === '0') {
// Leave this bit
andWith += bit;
} else if (c === '1') {
// Override this bit
orWith += bit;
} else if (c === 'X') {
// Floating bit
if (i & exBit) {
orWith += bit;
} else {
// Leaving it out of both andWith and orWith zeroes it
}
exBit <<= 1n;
}
bit <<= 1n;
}
const effectiveIndex = (index & andWith) | orWith;
console.log(`[${range}] ${index} ${effectiveIndex} ${andWith} ${orWith}`);
memory[effectiveIndex] = value;
}
}
}
// console.log(memory);
for (const value of Object.values(memory)) {
if ((typeof value) === 'bigint') {
sum += value;
}
}
console.log(sum);