-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpuzzle2.ts
32 lines (23 loc) · 865 Bytes
/
puzzle2.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
import { times } from 'lodash';
import { readInput } from '../utils';
const input = readInput('./input');
const inputAsDigits = input.map((s) => s.split('').map(Number));
function digitsToDecimal(digits: number[]) {
return parseInt(digits.join(''), 2);
}
function findNumber(comparison: (n: number) => boolean) {
const [digits] = times(inputAsDigits[0].length).reduce((readings, _c, i) => {
const commonValue = readings
.map((data) => data[i])
.reduce((acc, curr) => acc + (curr || -1), 0);
const sign = Number(comparison(commonValue));
if (readings.length === 1) {
return readings;
}
return readings.filter((data) => data[i] === sign);
}, inputAsDigits);
return digitsToDecimal(digits);
}
const oxygen = findNumber((n) => n >= 0);
const co2 = findNumber((n) => n < 0);
console.log(oxygen, co2, oxygen * co2);