-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
172 lines (150 loc) · 4.65 KB
/
utils.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import type { Point, PathFindingPoint } from './types';
import { ORTHOGONAL_DIRECTIONS, EIGHT_WAY_DIRECTIONS, NINE_WAY_DIRECTIONS } from './constants';
export const getNeighbors = <T>(
input: T[][],
arg1: number | Point,
arg2?: number,
directions = ORTHOGONAL_DIRECTIONS,
filterUndefined = true,
): T[] => {
let x: number, y: number;
if (typeof arg1 === 'number' && typeof arg2 === 'number') {
x = arg1;
y = arg2;
} else if (typeof arg1 === 'object') {
x = arg1.x;
y = arg1.y;
} else {
throw new Error('Invalid arguments');
}
const result = directions.map((direction) => input[y + direction.y]?.[x + direction.x]);
return filterUndefined ? result.filter((n) => n !== undefined) : result;
};
export const getEightNeighbors = <T>(input: T[][], arg1: number | Point, arg2?: number): T[] => {
return getNeighbors(input, arg1, arg2, EIGHT_WAY_DIRECTIONS);
};
export const getEightNeighborsUnfiltered = <T>(
input: T[][],
arg1: number | Point,
arg2?: number,
): T[] => {
return getNeighbors(input, arg1, arg2, EIGHT_WAY_DIRECTIONS, false);
};
export const getNightNeighbors = <T>(input: T[][], arg1: number | Point, arg2?: number): T[] => {
return getNeighbors(input, arg1, arg2, NINE_WAY_DIRECTIONS);
};
export const getNightNeighborsUnfiltered = <T>(
input: T[][],
arg1: number | Point,
arg2?: number,
): T[] => {
return getNeighbors(input, arg1, arg2, NINE_WAY_DIRECTIONS, false);
};
export const getNeighborPositions = <T>(
input: T[][],
arg1: number | Point,
arg2?: number,
): Point[] => {
let x: number, y: number;
if (typeof arg1 === 'number' && typeof arg2 === 'number') {
x = arg1;
y = arg2;
} else if (typeof arg1 === 'object') {
x = arg1.x;
y = arg1.y;
} else {
throw new Error('Invalid arguments');
}
return ORTHOGONAL_DIRECTIONS.map((direction) => ({
x: x + direction.x,
y: y + direction.y,
})).filter((point) => input[point.y]?.[point.x]);
};
export const parsePoint = (str: string) => {
const [x, y] = str.split(',').map(Number);
return { x, y } as Point;
};
export const pointToString = (p: Point) => `${p.x},${p.y}`;
export const drawPoints = (points: Point[]) => {
let maxX = Math.max(...points.map((p) => p.x)) + 1;
let maxY = Math.max(...points.map((p) => p.y)) + 1;
let board = new Array(maxY).fill(null).map(() => new Array(maxX).fill('.'));
points.forEach((p) => (board[p.y][p.x] = '#'));
drawBoard(board);
};
export const drawBoard = (board: any[][]) => {
console.log(board.map((row) => row.join('')).join('\n'));
};
export const findPathScore = (
board: PathFindingPoint[][],
start: PathFindingPoint,
isEnd: (current: Point) => boolean,
findMin = true,
) => {
const queue = [{ x: start.x, y: start.y, score: start.score }];
const visited = new Map<string, number>();
let result = Infinity;
while (queue.length > 0) {
const current = queue.shift();
if (isEnd(current)) {
result = Math.min(current.score, result);
continue;
}
const neighbors = getNeighbors(board, current).filter((neighbor) => !neighbor.isWall);
let searches = neighbors
.map((neighbor) => ({
x: neighbor.x,
y: neighbor.y,
score: current.score + neighbor.score,
}))
.filter((search) => {
const key = `${search.x},${search.y}`;
return (
!visited.has(key) ||
(findMin ? visited.get(key) > search.score : visited.get(key) < search.score)
);
});
queue.push(...searches);
queue.sort((a, b) => (a.score - b.score) * (findMin ? 1 : -1));
searches.forEach((search) => visited.set(`${search.x},${search.y}`, search.score));
}
return result;
};
export const findPathVisited = (
board: PathFindingPoint[][],
start: PathFindingPoint,
isEnd: (current: Point) => boolean,
findMin = true,
) => {
const queue = [{ x: start.x, y: start.y, score: start.score, visited: [start] }];
const visited = new Map<string, number>();
let score = Infinity;
let result = [start];
while (queue.length > 0) {
const current = queue.shift();
if (isEnd(current)) {
score = Math.min(current.score, score);
result = current.visited;
continue;
}
const neighbors = getNeighbors(board, current).filter((neighbor) => !neighbor.isWall);
let searches = neighbors
.map((neighbor) => ({
x: neighbor.x,
y: neighbor.y,
score: current.score + neighbor.score,
visited: [...current.visited, neighbor],
}))
.filter((search) => {
const key = `${search.x},${search.y}`;
return (
!visited.has(key) ||
(findMin ? visited.get(key) > search.score : visited.get(key) < search.score)
);
});
queue.push(...searches);
queue.sort((a, b) => (a.score - b.score) * (findMin ? 1 : -1));
searches.forEach((search) => visited.set(`${search.x},${search.y}`, search.score));
}
return result;
};