-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththree.js
More file actions
73 lines (58 loc) · 1.72 KB
/
Copy paththree.js
File metadata and controls
73 lines (58 loc) · 1.72 KB
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
import { readFile } from 'fs/promises';
// 3a
// async function init() {
// const input = await readFile('three.txt', 'utf8');
// const data = input
// .split('\n')
// .map((d) => d.split('').map((s) => parseInt(s)));
// const len = data.length;
// const sums = data.reduce((acc, d, i) => {
// if (i !== 0) {
// acc.forEach((e, j) => {
// acc[j] += d[j];
// });
// }
// return acc;
// }, data[0]);
// // console.log(len / 2);
// // console.log(sums);
// const gamma = parseInt(sums.map((d) => (d > len / 2 ? 1 : 0)).join(''), 2);
// const epsilon = parseInt(sums.map((d) => (d < len / 2 ? 1 : 0)).join(''), 2);
// console.log({ gamma, epsilon });
// console.log(gamma * epsilon);
// }
// 3b
function calcGamma(list, bit) {
const len = list.length;
const sum = list.reduce((acc, d) => acc + d[bit], 0);
return sum >= len / 2 ? 1 : 0;
}
function calcEpsilon(list, bit) {
const len = list.length;
const sum = list.reduce((acc, d) => acc + d[bit], 0);
return sum < len / 2 ? 1 : 0;
}
async function init() {
const input = await readFile('three.txt', 'utf8');
const data = input
.split('\n')
.map((d) => d.split('').map((s) => parseInt(s)));
let ox = [...data];
let index = 0;
while (ox.length > 1 && index < data[0].length) {
const gamma = calcGamma(ox, index);
ox = ox.filter((d) => d[index] === gamma);
index++;
}
let co2 = [...data];
index = 0;
while (co2.length > 1 && index < data[0].length) {
const epsilon = calcEpsilon(co2, index);
co2 = co2.filter((d) => d[index] === epsilon);
index++;
}
ox = parseInt(ox[0].join(''), 2);
co2 = parseInt(co2[0].join(''), 2);
console.log({ ox, co2, abc: ox * co2 });
}
init();