-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.js
76 lines (64 loc) · 1.79 KB
/
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
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
const fs = require('fs');
const assert = require('assert');
const rows = fs.readFileSync('./data/day3.txt', 'utf8').split('\n');
const output = rows.reduce((acc, item, outerIndex) => {
[...item].forEach((value, index) => {
if (acc[index]) {
if (acc[index][value]) {
acc[index][value]++;
} else {
acc = { ...acc, [index]: { ...acc[index], [value]: 1 } };
}
} else {
acc = { ...acc, [index]: { ...acc[index], [value]: 1 } };
}
});
return acc;
}, {});
epsilon = [];
gamma = [];
Object.values(output).forEach((item, index) => {
if (item['0'] > item['1']) {
gamma.push(0);
epsilon.push(1);
} else {
gamma.push(1);
epsilon.push(0);
}
});
epsilonInt = parseInt(epsilon.join(''), 2);
gammaInt = parseInt(gamma.join(''), 2);
console.log(`part1 answer: ${gammaInt * epsilonInt}`);
// part2
const getMostFrequentCharAtIndex = (items, index) => {
let counts = items.reduce((acc, item) => {
const char = item.trim().charAt(index);
if (acc[char]) {
acc[char]++;
} else {
acc[char] = 1;
}
return acc;
}, {});
return counts[0] > counts[1] ? 0 : 1;
};
const filterRows = (rows, index, most) => {
const mostFrequentChar = getMostFrequentCharAtIndex(rows, index);
const filteredRows = rows.filter((row) => {
if (most) {
return parseInt(row.charAt(index), 2) === mostFrequentChar;
} else {
return parseInt(row.charAt(index), 2) !== mostFrequentChar;
}
});
if (filteredRows.length == 1) {
value = parseInt(filteredRows[0], 2);
return value;
}
index = index + 1;
return filterRows(filteredRows, index++, most);
};
const oxy_gen = filterRows(rows, 0, true);
const co2_scrub = filterRows(rows, 0, false);
ls_rating = oxy_gen * co2_scrub;
console.log(`part two answer: ${ls_rating}`);