-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
55 lines (43 loc) · 1.01 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
interface Claim {
id: string;
x: number;
y: number;
width: number;
height: number;
}
function parseClaim(claim: string): Claim {
const { groups } =
claim.match(/#(?<id>\d+) @ (?<x>\d+),(?<y>\d+): (?<width>\d+)x(?<height>\d+)/) || [];
if (!groups) {
throw new Error('Invalid input');
}
const { id, x, y, width, height } = groups;
return {
id,
x: parseInt(x, 10),
y: parseInt(y, 10),
width: parseInt(width, 10),
height: parseInt(height, 10),
};
}
function part1(claims: string[]): number {
const map = new Map();
const parsedClaims = claims.map(parseClaim);
parsedClaims.forEach((claim) => {
const { x, y, width, height } = claim;
for (let i = x; i < x + width; i += 1) {
for (let j = y; j < y + height; j += 1) {
const key = [i, j].toString();
map.set(key, (map.get(key) || 0) + 1);
}
}
});
let counter = 0;
map.forEach((entry) => {
if (entry !== 1) {
counter += 1;
}
});
return counter;
}
export { part1 };