-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.ts
225 lines (209 loc) · 4.28 KB
/
crawler.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
enum Direction {
UP = "UP",
RIGHT = "RIGHT",
LEFT = "LEFT",
DOWN = "DOWN",
}
export interface HistoryItem {
xStart: number;
yStart: number;
xEnd: number;
yEnd: number;
direction: Direction;
traceX: number;
traceY: number;
}
class History {
_store: HistoryItem[] = [];
lastTracedIndex = 0;
push(val: HistoryItem) {
this._store.push(val);
}
getLast(): HistoryItem | undefined {
return this._store[this._store.length - 1];
}
getStore() {
return this._store;
}
getFirstUntraced() {
return this.getStore()[this.lastTracedIndex];
}
nextUntraced() {
this.lastTracedIndex++;
}
}
export const getCrawler = (
hasMineFunction: (x: number, y: number) => boolean
) => (fieldAddListener?: (field: Partial<HistoryItem>) => any): number => {
let x = 0,
y = 0,
sumOfFields = 0;
let borderFields = 0;
const history = new History();
let traceBackDirection;
const pushNewMoveToStack = (historyItem: Partial<HistoryItem>) => {
fieldAddListener && fieldAddListener(historyItem);
sumOfFields++;
const lastMove = history.getLast();
if (
!lastMove ||
lastMove.direction !== historyItem.direction ||
traceBackDirection
) {
historyItem.traceX = historyItem.xStart;
historyItem.traceY = historyItem.yStart;
history.push(historyItem as HistoryItem);
} else {
lastMove.yEnd = historyItem.yEnd;
lastMove.xEnd = historyItem.xEnd;
}
traceBackDirection = undefined;
if (historyItem.xEnd === 0 || historyItem.yEnd === 0) {
borderFields++;
}
};
const historyBlock = (x, y): boolean => {
return history.getStore().some((item) => {
if (
item.direction === Direction.UP &&
item.yStart <= y &&
item.yEnd >= y &&
x === item.xStart
) {
return true;
}
if (
item.direction === Direction.DOWN &&
item.yStart >= y &&
item.yEnd <= y &&
x === item.xStart
) {
return true;
}
if (
item.direction === Direction.RIGHT &&
item.xStart <= x &&
item.xEnd >= x &&
y === item.yStart
) {
return true;
}
if (
item.direction === Direction.LEFT &&
item.xStart >= x &&
item.xEnd <= x &&
y === item.yStart
) {
return true;
}
return false;
});
};
const isBlocked = (x, y) => {
const blockedByConstraint = x < 0 || y < 0 || hasMineFunction(x, y);
return blockedByConstraint || historyBlock(x, y);
};
pushNewMoveToStack({
xEnd: 0,
xStart: 0,
yStart: 0,
yEnd: 0,
direction: Direction.UP,
});
while (true) {
//DOWN
if (!isBlocked(x, y - 1)) {
pushNewMoveToStack({
xEnd: x,
xStart: x,
yStart: y - 1,
yEnd: y - 1,
direction: Direction.DOWN,
});
y = y - 1;
continue;
}
//LEFT
if (!isBlocked(x - 1, y)) {
pushNewMoveToStack({
xEnd: x - 1,
xStart: x - 1,
yStart: y,
yEnd: y,
direction: Direction.LEFT,
});
x = x - 1;
continue;
}
//UP
if (!isBlocked(x, y + 1)) {
pushNewMoveToStack({
xEnd: x,
xStart: x,
yStart: y + 1,
yEnd: y + 1,
direction: Direction.UP,
});
y = y + 1;
continue;
}
//RIGHT
if (!isBlocked(x + 1, y)) {
pushNewMoveToStack({
xEnd: x + 1,
xStart: x + 1,
yStart: y,
yEnd: y,
direction: Direction.RIGHT,
});
x = x + 1;
continue;
}
//Tracing back
if (history.getFirstUntraced()) {
const last = history.getFirstUntraced();
traceBackDirection = last.direction;
switch (traceBackDirection) {
case Direction.DOWN:
y = last.traceY;
x = last.xStart;
last.traceY--;
if (last.traceY < last.yEnd) {
history.nextUntraced();
}
continue;
case Direction.LEFT:
x = last.traceX;
y = last.yStart;
last.traceX--;
if (last.traceX < last.xEnd) {
history.nextUntraced();
}
continue;
case Direction.UP:
y = last.traceY;
x = last.xStart;
last.traceY++;
if (last.traceY > last.yEnd) {
history.nextUntraced();
}
continue;
case Direction.RIGHT:
x = last.traceX;
y = last.yStart;
last.traceX++;
if (last.traceX > last.xEnd) {
history.nextUntraced();
}
continue;
}
}
break;
}
let result = sumOfFields * 4;
//Subtract for border points where x or y = 0
result = result - 2 * borderFields;
//Subtract one extra for 0:0 point, which is in all planes
result = result - 1;
return result;
};