-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
62 lines (47 loc) · 1.53 KB
/
index.ts
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
function countZerosAndOnes(binaryNumbers: string[], position: number) {
let zeros = 0;
let ones = 0;
binaryNumbers.forEach((binaryNumber) => {
const bit = binaryNumber[position];
if (bit === '0') {
zeros += 1;
} else {
ones += 1;
}
});
return { zeros, ones };
}
function part1(binaryNumbers: string[]): number {
let mostCommon = '';
let leastCommon = '';
for (let i = 0; i < binaryNumbers[0].length; i += 1) {
const { zeros, ones } = countZerosAndOnes(binaryNumbers, i);
if (zeros > ones) {
mostCommon += '0';
leastCommon += '1';
} else {
mostCommon += '1';
leastCommon += '0';
}
}
const gammaRate = parseInt(mostCommon, 2);
const epsilonRate = parseInt(leastCommon, 2);
return gammaRate * epsilonRate;
}
function calculateRating(numbers: string[], isMost = true, position = 0): string {
if (numbers.length === 1) {
return numbers[0];
}
const { zeros, ones } = countZerosAndOnes(numbers, position);
const filteredNumbers =
zeros > ones
? numbers.filter((number) => number[position] === (isMost ? '0' : '1'))
: numbers.filter((number) => number[position] === (isMost ? '1' : '0'));
return calculateRating(filteredNumbers, isMost, position + 1);
}
function part2(binaryNumbers: string[]): number {
const oxygenGeneratorRating = parseInt(calculateRating(binaryNumbers), 2);
const co2ScrubberRating = parseInt(calculateRating(binaryNumbers, false), 2);
return oxygenGeneratorRating * co2ScrubberRating;
}
export { part1, part2 };