-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.ts
54 lines (50 loc) · 1.23 KB
/
16.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
import {loadData} from '../shared/utils';
const properties: {[key: string]: number} = {
children: 3,
cats: 7,
samoyeds: 2,
pomeranians: 3,
akitas: 0,
vizslas: 0,
goldfish: 5,
trees: 3,
cars: 2,
perfumes: 1,
};
export const part1 = (sues: Array<{[key: string]: number}>): number => {
return (
sues.findIndex(sue => {
return Object.keys(sue).every(key => sue[key] === properties[key]);
}) + 1
);
};
export const part2 = (sues: Array<{[key: string]: number}>): number => {
return (
sues.findIndex(sue => {
return Object.keys(sue).every(key => {
switch (key) {
case 'cats':
case 'trees':
return sue[key] > properties[key];
case 'pomeranians':
case 'goldfish':
return sue[key] < properties[key];
default:
return sue[key] === properties[key];
}
});
}) + 1
);
};
/* istanbul ignore next */
export const parse = async () =>
(await loadData(2015, 16)).split('\n').map(line => {
const res = /^Sue \d+: (\w+): (\d+), (\w+): (\d+), (\w+): (\d+)$/i.exec(
line
)!;
return {
[res[1]]: Number(res[2]),
[res[3]]: Number(res[4]),
[res[5]]: Number(res[6]),
};
});