-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharea2.js
37 lines (29 loc) · 890 Bytes
/
area2.js
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
const distance = (p1, p2) => Math.abs(p2.x - p1.x) + Math.abs(p2.y - p1.y);
const area = (input, limit) => {
const coordinates = input
.split('\n')
.map((x) => {
const parts = x.split(',');
return {
x: +parts[0],
y: +parts[1],
};
});
const x1 = coordinates.map(({ x }) => x).sort((a, b) => a - b)[0];
const y1 = coordinates.map(({ y }) => y).sort((a, b) => a - b)[0];
const x2 = coordinates.map(({ x }) => x).sort((a, b) => b - a)[0];
const y2 = coordinates.map(({ y }) => y).sort((a, b) => b - a)[0];
let area = 0;
for (let y = y1; y < y2; y++) {
for (let x = x1; x < x2; x++) {
const totalDistance = coordinates
.map((coord) => distance({ x, y }, coord))
.reduce((a, b) => a + b, 0);
if (totalDistance < limit) {
area++;
}
}
}
return area;
};
module.exports = area;