-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.js
175 lines (157 loc) · 4.9 KB
/
day14.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const { readData } = require('./readfile');
const bitOperation = require('./bitOperation');
// const input = readData('day14Input-simple.txt');
const input = readData('day14Input.txt');
const lines = input.split(/\n/).map(line => line.split(" = "));
const operations = {};
const maskNumber1 = (number, mask) => bitOperation.setNthBit64(number, mask);
const maskNumber0 = (number, mask) => bitOperation.unsetNthBit64(number, mask);
const maskNumber1And0 = (number, mask1, mask0) => {
let res = number;
for (let i = 0; i < mask1.length; i++) {
res = maskNumber1(res, mask1[i]);
}
for (let i = 0; i < mask0.length; i++) {
res = maskNumber0(res, mask0[i]);
}
return res;
}
const maskNumber1s = (number, mask1) => {
let res = number;
for (let i = 0; i < mask1.length; i++) {
res = maskNumber1(res, mask1[i]);
}
return res;
}
const sumMasked = lines => {
let mask1, mask0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line[0] === "mask") {
let m = line[1];
mask1 = [];
mask0 = [];
for (let j = 0; j < m.length; j++) {
if(m[j] === '1') {
mask1.push(m.length-j-1);
} else if(m[j] === '0') {
mask0.push(m.length-j-1);
}
}
// console.log(mask1, mask0);
continue;
}
const value = parseInt(line[1])
const index = /\[(.+)\]/g.exec(line[0])[1];
const masked = maskNumber1And0(value, mask1, mask0);
// console.log("mem", index, value, masked);
if(masked < 0) {
// console.log(line[1], mask1, mask0, masked)
}
operations[`s${index}`] = masked;
}
const res = Object.values(operations).reduce((acc, cur) => acc + cur, 0)
return res;
}
// console.log(t);
// t = bitOperation.setNthBit64(483, 31);
// console.log(t);
// t = bitOperation.setNthBit64(483, 32);
// console.log(t);
// t = bitOperation.unsetNthBit64(4294967779, 32);
// console.log(t);
// t = bitOperation.unsetNthBit64(2147484131, 31);
// console.log(t);
// t = bitOperation.unsetNthBit64(1073742307, 30);
// console.log(t);
// let masked1 = bitOperation.setNthBit64(474293, 33);
// console.log(masked1);
// let masked2 = bitOperation.unsetNthBit64(masked1, 28);
// console.log(masked2);
// const testUnset = (x, n) => {
// let res = bitOperation.unsetNthBit64(x, n);
// console.log(res)
// }
// 0x8fffffff = 2415919103;
// testUnset(2415919103, 27);
// testUnset(2415919103, 28);
// testUnset(2415919103, 29);
// testUnset(2415919103, 30);
// testUnset(2415919103, 31);
// testUnset(2415919103, 32);
// testUnset(2415919103, 33);
// 7728987414911
// mask1 = [
// 31, 30, 29, 26, 25, 24,
// 21, 12, 11, 9, 8, 5,
// 1, 0
// ]
// mask0 = [
// 34, 33, 32, 28, 27, 23, 20,
// 19, 18, 17, 16, 14, 13, 10,
// 4, 3, 2
// ]
// num = 4114776
// mask1 = [
// 35, 34, 32, 31, 29, 27,
// 23, 20, 14, 13, 8, 5,
// 2
// ]
// mask0 = [
// 33, 28, 26, 22, 21, 19, 18,
// 16, 15, 12, 11, 10, 6, 4,
// 3, 1, 0
// ]
// num = 356438366
// maskNumber1And0(num, mask1, mask0)
// console.log(sumMasked(lines));
const getAddresses = (address, masks) => {
let addresses = [address];
// console.log(`maskX's number: ${masks.length}`);
for (let i = 0; i < masks.length; i++) {
const len = addresses.length;
for (let j = 0; j < len; j++) {
const cur = addresses.shift();
addresses.push(maskNumber0(cur, masks[i]));
addresses.push(maskNumber1(cur, masks[i]));
}
}
return addresses;
}
const sumDecoded = lines => {
let mask1, maskX;
let sum = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line[0] === "mask") {
let m = line[1];
mask1 = [];
maskX = [];
for (let j = 0; j < m.length; j++) {
if(m[j] === '1') {
mask1.push(m.length-j-1);
} else if(m[j] === 'X') {
maskX.push(m.length-j-1);
}
}
// console.log(mask1, maskX);
continue;
}
const value = parseInt(line[1])
const index = parseInt(/\[(.+)\]/g.exec(line[0])[1]);
const maskedAddress = maskNumber1s(index, mask1);
const addresses = getAddresses(maskedAddress, maskX);
// console.log("mem", value, index, maskedAddress, addresses);
// console.log(value, addresses.length, maskX.length)
// sum += addresses.length * value;
addresses.forEach(address => {
operations[address] = value;
})
}
// console.log(sum);
// console.log(JSON.stringify(Object.values(operations)))
const res = Object.values(operations).reduce((acc, cur) => acc + cur, 0)
console.log(res);
return res;
}
sumDecoded(lines)