-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharea.js
49 lines (41 loc) · 1.35 KB
/
area.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
38
39
40
41
42
43
44
45
46
47
48
49
const distance = (p1, p2) => Math.abs(p2.x - p1.x) + Math.abs(p2.y - p1.y);
const area = (input) => {
const coordinates = input
.split('\n')
.map((x) => {
const parts = x.split(',');
return {
x: +parts[0],
y: +parts[1],
points: [],
};
});
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];
for (let y = y1; y < y2; y++) {
for (let x = x1; x < x2; x++) {
const distances = coordinates
.map((origin) => {
return {
x: origin.x,
y: origin.y,
distance: distance(origin, { x, y }),
};
})
.sort((a, b) => a.distance - b.distance);
if (distances[0].distance < distances[1].distance) {
coordinates
.find((coord) => coord.x === distances[0].x &&
coord.y === distances[0].y)
.points.push({ x, y });
}
}
}
const innerAreas = coordinates
.filter(({ points }) => points
.every(({ x, y }) => x > x1 && x < x2 && y > y1 && y < y2));
return Math.max(...innerAreas.map(({ points }) => points.length));
};
module.exports = area;