-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-binary-diag.ts
120 lines (112 loc) · 3.29 KB
/
3-binary-diag.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import axios from "axios";
const getData = async () =>
await axios.get("https://adventofcode.com/2021/day/3/input", {
headers: {
Cookie:
"session=53616c7465645f5fcd01739818d6d32714847f740139d63198a1338a6890b2efde467b312dc5da5164680836385cb744",
},
});
const getMostCommonBit = (input: string[], position: number) => {
let ones = 0;
let zeros = 0;
const row = input.map((str) => str[position]);
row.forEach((entry) => {
if (entry === "1") {
ones++;
} else if (entry === "0") {
zeros++;
}
});
if (ones > zeros) {
return "1";
} else {
return "0";
}
};
(async () => {
try {
const res = await getData();
const input = res.data;
const inputArray = input.split("\n").filter((input) => input);
const positions = inputArray[0].length;
let gammaString = "";
for (let i = 0; i < positions; i++) {
getMostCommonBit(inputArray, 0);
gammaString = gammaString + getMostCommonBit(inputArray, i);
}
let gammaDec = parseInt(gammaString, 2);
let epsilon = gammaString
.split("")
.map((x) => (x === "1" ? "0" : "1"))
.join("");
let epsilonDec = parseInt(epsilon, 2);
console.log(gammaDec * epsilonDec);
} catch (e) {
console.error(e);
}
})();
const oxygenCriteria = (
input: string[],
bits: string[],
bitPosition: number
) => {
// most common value and fallback is 1
let ones = bits.filter((bit) => bit === "1").length;
let zeros = bits.filter((bit) => bit === "0").length;
let filterBit = zeros > ones ? "0" : "1";
if (zeros === ones) {
filterBit = "1";
}
return input.filter((input) => input.charAt(bitPosition) !== filterBit);
};
const carbonDyoxideCriteria = (
input: string[],
bits: string[],
bitPosition: number
) => {
// least common value and fallback is 0
let ones = bits.filter((bit) => bit === "1").length;
let zeros = bits.filter((bit) => bit === "0").length;
let filterBit = zeros > ones ? "1" : "0";
if (zeros === ones) {
filterBit = "0";
}
return input.filter((input) => input.charAt(bitPosition) !== filterBit);
};
const findRating = (filterBitCriteria: Function, input: string[]) => {
let bitPosition = 0;
let maxBits = input[0].length;
while (input.length != 1) {
let newInput: string[] = [];
const currentBits = input.map((number) => number.charAt(bitPosition));
newInput = filterBitCriteria(input, currentBits, bitPosition);
input = newInput;
if (bitPosition < maxBits) {
bitPosition++;
} else {
return "nothing found";
}
}
return input[0];
};
const findOxygenRating = (input: string[]) => {
return findRating(oxygenCriteria, input);
};
(async () => {
try {
const res = await getData();
const input = res.data.split("\n").filter((input) => input);
const oxygenRatingBinary = findRating(oxygenCriteria, input);
const oxygenRatingDec = parseInt(oxygenRatingBinary, 2);
const carbonDyoxideRatingBinary = findRating(carbonDyoxideCriteria, input);
const carbonDyoxideRatingDec = parseInt(carbonDyoxideRatingBinary, 2);
if (isNaN(oxygenRatingDec) || isNaN(carbonDyoxideRatingDec)) {
console.error("No number found");
}
console.log(
`Life Support Rating is: ${oxygenRatingDec * carbonDyoxideRatingDec}`
);
} catch (e) {
console.error(e);
}
})();