-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.ts
34 lines (28 loc) · 993 Bytes
/
03.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
import {loadData, sum} from '../shared/utils';
export type DataType = Array<number>;
/* istanbul ignore next */
export const parse = async (): Promise<Array<DataType>> =>
(await loadData(2016, 3)).split('\n').map(row =>
row
.split(/\ +/)
.filter(piece => piece.trim().length > 0)
.map(Number)
);
export const isValidTriangle = (sides: DataType): boolean => {
return sides.every(
(side, index) => side < sum(sides.filter((_, i) => i !== index))
);
};
export const convertData = (data: Array<DataType>): Array<DataType> => {
const convertedData: Array<DataType> = [];
for (let i = 0; i < data.length; i += 3) {
for (let j = 0; j < 3; j += 1) {
convertedData.push(data.slice(i, i + 3).map(d => d[j]));
}
}
return convertedData;
};
export const part1 = (data: Array<DataType>): number =>
data.filter(isValidTriangle).length;
export const part2 = (data: Array<DataType>): number =>
convertData(data).filter(isValidTriangle).length;