-
Notifications
You must be signed in to change notification settings - Fork 1
/
day_3.ts
51 lines (43 loc) · 2.01 KB
/
day_3.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
import { loadInput } from './helpers';
type countsArray = Array<number>;
const lineToCountsArray = (line: string): countsArray => {
return line.split('').map((char) => char === "1" ? 1 : 0);
}
const input = loadInput('day_3.input');
const countsArrays = input.split("\n").map(lineToCountsArray);
const threshold = countsArrays.length / 2;
const calcPart1 = (countsArrays: Array<countsArray>): number => {
const addCountsArrays = (a: countsArray, b: countsArray): countsArray => {
const result = [];
for (let index = 0; index < a.length; index++) {
result.push(a[index] + b[index]);
}
return result;
}
const summed = countsArrays.reduce(addCountsArrays);
const [gamma, epsilon] = summed.reduce(([gamma, epsilon], total) => {
if (total > threshold) {
return [gamma + "1", epsilon + "0"];
} else {
return [gamma + "0", epsilon + "1"];
}
}, ["", ""]);
return parseInt(gamma, 2) * parseInt(epsilon, 2);
}
const calcPart2 = (countsArrays: Array<countsArray>): number => {
const calcCandidate = (countsArrays: Array<countsArray>, index: number, calcFilterBit: (totalCount: number, sum: number) => 0|1) => {
const sumAtIndex = countsArrays.map(arr => arr[index]).reduce((a, b) => a + b);
const filterBit = calcFilterBit(countsArrays.length, sumAtIndex);
const candidates = countsArrays.filter((arr) => arr[index] == filterBit);
if (candidates.length === 1) {
return parseInt(candidates[0].map(n => n.toString()).join(""), 2);
} else {
return calcCandidate(candidates, index + 1, calcFilterBit);
}
}
const oxygenGeneratorRating = calcCandidate(countsArrays, 0, (total, sum) => sum >= total / 2 ? 1 : 0);
const co2ScrubberRating = calcCandidate(countsArrays, 0, (total, sum) => sum >= total / 2 ? 0 : 1);
return oxygenGeneratorRating * co2ScrubberRating;
}
console.log("Part 1", calcPart1(countsArrays));
console.log("Part 2", calcPart2(countsArrays));