-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.js
44 lines (37 loc) · 878 Bytes
/
day3.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
const input = `00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010`
const parsedInput = input.split('\n').map((s) => s.split('').map(Number));
function toDec(bin) {
return parseInt(bin.join(''), 2);
}
function getMLBits(input, pos) {
const hash = {0: 0, 1: 0};
input.forEach((i) => hash[i[pos]]++);
if (hash[1] >= hash[0]) {
return [1, 0];
}
return [0, 1];
}
function filterCollectionByMLIndex(input, idx) {
let collection = input;
let bitPos = 0;
while (collection.length > 1) {
const mlBits = getMLBits(collection, bitPos);
collection = collection.filter((i) => i[bitPos] === mlBits[idx])
bitPos++;
}
return collection[0];
}
const oxy = filterCollectionByMLIndex(parsedInput, 0);
const co2 = filterCollectionByMLIndex(parsedInput, 1);
console.log(toDec(oxy) * toDec(co2));