-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem14a.js
55 lines (50 loc) · 1.37 KB
/
problem14a.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
const fs = require('fs');
const data = fs.readFileSync('input14.txt', 'utf8');
// const data = `
// mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
// mem[8] = 11
// mem[7] = 101
// mem[8] = 0`;
const program = data.split('\n').map(line => line.trim()).filter(line => line.length).map(line => line.split(' = '));
const memory = [];
let andWith = 0n;
let orWith = 0n;
let sum = 0n;
let mask;
for (instruction of program) {
if (instruction[0] === 'mask') {
let bit = 1n;
mask = instruction[1];
andWith = 0n;
orWith = 0n;
for (let i = mask.length - 1; (i >= 0); i--) {
const c = mask.charAt(i);
if (c === 'X') {
// Leave this bit
andWith += bit;
} else if (c === '1') {
// Override this bit
orWith += bit;
} else if (c === '0') {
// Drop this bit
}
bit <<= 1n;
}
} else if (instruction[0].startsWith('mem')) {
let [ dummy, index ] = instruction[0].match(/^mem\[(\d+)\]$/);
const value = BigInt(instruction[1]);
console.log(value);
console.log(`>${value.toString(2).padStart(36, '0')}`);
console.log(`#${mask}#`);
index = parseInt(index);
memory[index] = (value & andWith) | orWith;
console.log(`<${memory[index].toString(2)}`);
}
}
// console.log(memory);
for (const value of memory) {
if ((typeof value) === 'bigint') {
sum += value;
}
}
console.log(sum);