/
day3.mjs
76 lines (63 loc) · 2.32 KB
/
day3.mjs
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
import { getTask } from "./lib.js";
const task = await getTask(3);
const diagnosticReport = task.split("\n");
const lines = diagnosticReport.map((line) => line.split("").map(Number));
const lineWidth = lines[0].length;
const calcBitCount = (lines) => {
const bitsCount = lines.reduce((prev, line) => {
line.forEach((value, index) => (prev[index] += value));
return prev;
}, Array(lineWidth).fill(0));
return bitsCount;
};
const calcMostCommonBits = (bitsCount, halfLinesLength) => {
return bitsCount.map((line) => (line >= halfLinesLength ? 1 : 0));
};
const calcLessCommonBits = (bitsCount, halfLinesLength) => {
return bitsCount.map((line) => (line >= halfLinesLength ? 0 : 1));
};
const calcPowerConsumption = () => {
const bitCount = calcBitCount(lines);
const halfLinesLength = lines.length / 2;
const mostCommonBits = calcMostCommonBits(bitCount, halfLinesLength);
const lessCommonBits = calcLessCommonBits(bitCount, halfLinesLength);
const gammaRate = mostCommonBits.join("");
const gammaRateDecimal = parseInt(gammaRate, 2);
const epsilonRate = lessCommonBits.join("");
const epsilonRateDecimal = parseInt(epsilonRate, 2);
return gammaRateDecimal * epsilonRateDecimal;
};
const powerConsumption = calcPowerConsumption();
console.log(
`--- Part One ---\nWhat is the power consumption of the submarine?\n${powerConsumption}`
);
const calcRating = (lines, bitCriteria) => {
const linesClone = [...lines];
for (let i = 0; i < lineWidth; i++) {
const bitCount = calcBitCount(linesClone);
const commonBits = bitCriteria(bitCount, linesClone.length / 2);
if (linesClone.length > 1) {
for (let j = 0; j < linesClone.length; j++) {
if (linesClone[j][i] !== commonBits[i]) {
linesClone.splice(j, 1);
if (linesClone.length === 1) {
break;
}
j--;
}
}
}
}
const rating = linesClone[0].join("");
const ratingDecimal = parseInt(rating, 2);
return ratingDecimal;
};
const calcLifeSupportRating = () => {
const oxygenRating = calcRating(lines, calcMostCommonBits);
const co2Rating = calcRating(lines, calcLessCommonBits);
return oxygenRating * co2Rating;
};
const lifeSupportRating = calcLifeSupportRating();
console.log(
`-- Part Two --\nWhat is the life support rating of the submarine?\n${lifeSupportRating}`
);