[ya-day1] swimmer problem 1
This commit is contained in:
@@ -7,60 +7,18 @@ export function toGetClosestPoint(
|
|||||||
sx: number,
|
sx: number,
|
||||||
sy: number,
|
sy: number,
|
||||||
) {
|
) {
|
||||||
let direction: Direction = "N";
|
let direction: Direction = "" as Direction;
|
||||||
|
|
||||||
const sectors: Record<Direction, number[]> = {
|
if (sx >= x1 && sx <= x2 && sy >= y2) direction = "N";
|
||||||
NW: [x1, y2],
|
if (sx >= x2 && sy <= y2 && sy >= y1) direction = "E";
|
||||||
N: [x1, y2],
|
if (sx >= x1 && sx <= x2 && sy <= y1) direction = "S";
|
||||||
NE: [x2, y2],
|
if (sx <= x1 && sy <= y2 && sy >= y1) direction = "W";
|
||||||
E: [x2, y2],
|
|
||||||
SE: [x2, y1],
|
|
||||||
S: [x1, y1],
|
|
||||||
SW: [x1, y1],
|
|
||||||
W: [x1, y2],
|
|
||||||
};
|
|
||||||
|
|
||||||
for (let key in sectors) {
|
if (sx <= x1 && sy >= y2) direction = "NW";
|
||||||
const [x, y] = sectors[key];
|
if (sx >= x2 && sy >= y2) direction = "NE";
|
||||||
|
|
||||||
if (sx <= x && sy >= y) {
|
if (sx <= x1 && sy <= y1) direction = "SW";
|
||||||
direction = "NW";
|
if (sx >= x2 && sy <= y1) direction = "SE";
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (sx >= x && sy > y && sx <= x2) {
|
|
||||||
direction = "N";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (sx >= x && sy >= y) {
|
|
||||||
direction = "NE";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sx >= x && sy <= y && sy >= y1) {
|
|
||||||
direction = "E";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sx >= x && sy <= y && sx <= x2) {
|
|
||||||
direction = "S";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sx >= x && sy <= y) {
|
|
||||||
direction = "SE";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sy <= y && sx <= x) {
|
|
||||||
direction = "SW";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sy <= y2 && sy >= y1 && sx <= x) {
|
|
||||||
direction = "W";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return direction;
|
return direction;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user