Skip to content

Commit 83c9eed

Browse files
author
Hank
committed
Day 14. Part 2
1 parent 6de7943 commit 83c9eed

File tree

3 files changed

+91
-11
lines changed

3 files changed

+91
-11
lines changed

2020/14/solve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ const solve2 = () => {
1818
};
1919

2020
solve1();
21-
//solve2();
21+
solve2();

2020/14/test.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { util1, util2, write } = require('./utils');
1+
const { util1, util2, write, getAdresses } = require('./utils');
22

33
describe('it should solve part 1 and part 2', () => {
44
const input1 = [
@@ -8,14 +8,24 @@ describe('it should solve part 1 and part 2', () => {
88
'mem[8] = 0',
99
];
1010

11+
const input2 = [
12+
'mask = 000000000000000000000000000000X1001X',
13+
'mem[42] = 100',
14+
'mask = 00000000000000000000000000000000X0XX',
15+
'mem[26] = 1',
16+
];
17+
1118
test('it should solve example 1', () => {
1219
expect(write(11, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X')).toEqual(73);
1320
expect(write(101, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X')).toEqual(101);
1421
expect(write(0, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X')).toEqual(64);
1522
expect(util1(input1)).toEqual(165);
1623
});
1724

18-
/* test('it should solve example 2', () => {
19-
expect(util2(input1)).toEqual(0);
20-
}); */
25+
test('it should solve example 2', () => {
26+
expect(getAdresses(42, '000000000000000000000000000000X1001X')).toEqual([26, 27, 58, 59]);
27+
expect(getAdresses(26, '00000000000000000000000000000000X0XX')).toEqual([16, 17, 18, 19, 24, 25, 26, 27]);
28+
29+
expect(util2(input2)).toEqual(208);
30+
});
2131
});

2020/14/utils.js

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,67 @@
11
/* eslint-disable no-bitwise */
22

3+
const intToBin = (int) => (int >>> 0).toString(2).padStart(36, 0);
4+
35
const bitwiseAnd = (value, mask) => {
4-
const bin = (value >>> 0).toString(2).padStart(36, 0);
6+
const bin = intToBin(value);
57
const andMask = mask.replace(/X/g, '1');
6-
return andMask.split('').map((bit, index) => bin[index] & bit).join('');
8+
return andMask
9+
.split('')
10+
.map((bit, index) => bin[index] & bit)
11+
.join('');
712
};
813
const bitwiseOr = (bin, mask) => {
914
const orMask = mask.replace(/X/g, '0');
10-
return orMask.split('').map((bit, index) => bin[index] | bit).join('');
15+
return orMask
16+
.split('')
17+
.map((bit, index) => bin[index] | bit)
18+
.join('');
19+
};
20+
21+
const applyMaskToAdress = (address, mask) =>
22+
intToBin(address)
23+
.split('')
24+
.map((bit, i) => {
25+
if (mask[i] === '0') {
26+
return bit;
27+
}
28+
if (mask[i] === '1') {
29+
return 1;
30+
}
31+
return 'X';
32+
})
33+
.join('');
34+
35+
const getNumberOfAddresses = (mask) =>
36+
2 *
37+
mask.split('').reduce((sum, bit) => {
38+
if (bit === 'X') {
39+
sum += 1;
40+
}
41+
return sum;
42+
}, 0);
43+
44+
const replaceX = (addressMask) => {
45+
const index = addressMask.indexOf('X');
46+
if (index === -1) {
47+
return addressMask;
48+
}
49+
return [
50+
replaceX(
51+
`${addressMask.substring(0, index)}0${addressMask.substring(index + 1)}`,
52+
),
53+
replaceX(
54+
`${addressMask.substring(0, index)}1${addressMask.substring(index + 1)}`,
55+
),
56+
];
57+
};
58+
59+
const getAdresses = (address, mask) => {
60+
const addressMask = applyMaskToAdress(address, mask);
61+
62+
return replaceX(addressMask)
63+
.flat(20)
64+
.map((address) => parseInt(address, 2));
1165
};
1266

1367
const write = (value, mask) => {
@@ -44,8 +98,24 @@ const util1 = (input) => {
4498
};
4599

46100
const util2 = (input) => {
47-
console.log(input);
48-
return input;
101+
const result = input.reduce(({ memory, currentMask }, row) => {
102+
const [, mask] = row.match(/mask = ([X01]+)/) || [];
103+
const [, address, value] = row.match(/mem\[(\d+)\] = (\d+)/) || [];
104+
if (mask !== undefined) {
105+
currentMask = mask;
106+
}
107+
if (address) {
108+
const addresses = getAdresses(address, currentMask);
109+
addresses.forEach((address) => {
110+
memory[address] = value;
111+
});
112+
}
113+
return { memory, currentMask };
114+
}, { memory: {}, currentMask: '' });
115+
return Object.values(result.memory).reduce((sum, value) => {
116+
sum += Number(value);
117+
return sum;
118+
}, 0);
49119
};
50120

51-
module.exports = { util1, util2, write };
121+
module.exports = { util1, util2, write, getAdresses };

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy