-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
56 lines (45 loc) · 1.04 KB
/
index.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
interface Signature {
[key: string]: number;
}
const signature: Signature = {
children: 3,
cats: 7,
samoyeds: 2,
pomeranians: 3,
akitas: 0,
vizslas: 0,
goldfish: 5,
trees: 3,
cars: 2,
perfumes: 1,
};
function mapThing(thing: string): [string, number] {
const [item, count] = thing.split(': ');
return [item, parseInt(count, 10)];
}
function parseInput(input: string) {
const { groups } = input.match(/Sue (?<number>\d+): (?<things>(\w+: \d+(, )?){3})/) || [];
if (!groups) {
throw new Error('Invalid input');
}
const { number, things } = groups;
return {
number: parseInt(number, 10),
things: things.split(', ').map(mapThing),
};
}
function part1(input: string[]): number {
for (let i = 0; i < input.length; i += 1) {
const { number, things } = parseInput(input[i]);
const [one, two, three] = things;
if (
signature[one[0]] === one[1] &&
signature[two[0]] === two[1] &&
signature[three[0]] === three[1]
) {
return number;
}
}
return -1;
}
export { part1 };