forked from urfu-2017/javascript-task-3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobbery.js
More file actions
317 lines (265 loc) · 7.6 KB
/
robbery.js
File metadata and controls
317 lines (265 loc) · 7.6 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
'use strict';
/**
* Сделано задание на звездочку
* Реализовано оба метода и tryLater
*/
exports.isStar = true;
const MIN_IN_HOUR = 60;
const HOUR_IN_DAY = 24;
const MIN_IN_DAY = MIN_IN_HOUR * HOUR_IN_DAY;
const DAYS = {
'ПН': MIN_IN_DAY,
'ВТ': MIN_IN_DAY * 2,
'СР': MIN_IN_DAY * 3
};
/**
* @param {Object} schedule – Расписание Банды
* @param {Number} duration - Время на ограбление в минутах
* @param {Object} workingHours – Время работы банка
* @param {String} workingHours.from – Время открытия, например, "10:00+5"
* @param {String} workingHours.to – Время закрытия, например, "18:00+5"
* @returns {Object}
*/
exports.getAppropriateMoment = function (schedule, duration, workingHours) {
// console.info(schedule, duration, workingHours);
var TIME_ZONE = getTimeZone(workingHours.from);
var bankWorkingHours = getTimeInterval(workingHours);
var gangSchedule = getWorkingSchedule(schedule, TIME_ZONE);
var possibleStarts = tryToGetTime(gangSchedule, bankWorkingHours, duration);
var timeForCrime = formatList(createTimeList(possibleStarts, duration));
var lastChance = timeForCrime.slice(-1);
function changeTemplate() {
let time;
if (isEmpty()) {
time = lastChance;
} else {
time = timeForCrime[0];
timeForCrime.splice(0, 1);
}
return time;
}
function isEmpty() {
return timeForCrime.length === 0;
}
return {
/**
* Найдено ли время
* @returns {Boolean}
*/
exists: function () {
return (!isEmpty);
},
/**
* Возвращает отформатированную строку с часами для ограбления
* Например,
* "Начинаем в %HH:%MM (%DD)" -> "Начинаем в 14:59 (СР)"
* @param {String} template
* @returns {String}
*/
format: function (template) {
let time = changeTemplate();
if (!isEmpty()) {
return template
.replace('%HH', time[1])
.replace('%MM', time[2])
.replace('%DD', time[0]);
}
return '';
},
/**
* Попробовать найти часы для ограбления позже [*]
* @star
* @returns {Boolean}
*/
tryLater: function () {
return (!isEmpty);
}
};
};
function getTimeZone(time) {
return parseInt((time.slice(6)));
}
function getMinutes(time) {
return parseInt(time.slice(3, 5));
}
function getHours(time) {
return parseInt(time.slice(0, 2));
}
function convertToMinutes(time, timeZone) {
let minutes = 0;
minutes += getHours(time) * MIN_IN_HOUR;
minutes += getMinutes(time);
minutes += (timeZone - getTimeZone(time)) * MIN_IN_HOUR;
return minutes;
}
function getDay(day) {
if (day === 'ВТ') {
return (DAYS.ПН);
}
if (day === 'СР') {
return (DAYS.ВТ);
}
return 0;
}
function getTimeInterval(record, timeZone) {
let interval = [];
let startTime = (record.from).split(' ');
let endTime = (record.to).split(' ');
let start = convertToMinutes(startTime[1], timeZone);
let end = convertToMinutes(endTime[1], timeZone);
start += getDay(startTime[0]);
end += getDay(endTime[0]);
interval.push(start);
interval.push(end);
return interval;
}
function compareElements(a, b) {
return a[0] - b[0];
}
function getSchedule(schedule, timeZone) {
let reversedSchedule = [];
for (let man of Object.keys(schedule)) {
for (let record of schedule[man]) {
reversedSchedule.push(getTimeInterval(record, timeZone));
}
}
return reversedSchedule.sort(compareElements);
}
function getMax(a, b) {
if (a > b) {
return a;
}
return b;
}
function getWorkingSchedule(schedule, timeZone) {
let oldSchedule = getSchedule(schedule, timeZone);
let resultSchedule = [];
if (oldSchedule[0][0] >= 0) {
resultSchedule.push([0, oldSchedule[0][0]]);
}
let currentMax = oldSchedule[0][1];
for (let i = 1; i < oldSchedule.length; i++) {
if (oldSchedule[i][0] + 1 <= currentMax) {
currentMax = getMax(currentMax, oldSchedule[i][1]);
} else {
resultSchedule.push([currentMax, oldSchedule[i][0]]);
currentMax = oldSchedule[i][1];
}
}
if (currentMax < DAYS.СР) {
resultSchedule.push([currentMax, DAYS.СР]);
}
return resultSchedule;
}
function tryToGetTime(gangSchedule, bankSchedule, duration) {
var startCrime = [];
for (let i = 0; i < gangSchedule.length; i++) {
for (let j = 0; j < bankSchedule.length; j++) {
startCrime.push(checkInterval(
gangSchedule[i], bankSchedule[j], duration));
}
}
return startCrime;
}
function getMin(a, b) {
if (a < b) {
return a;
}
return b;
}
function checkLeft(a, b, duration) {
let result = [];
if ((a[0] <= b[0]) && (a[1] > b[0])) {
if (((a[1] < b[1]) && (a[1] - b[0] >= duration)) ||
((a[1] >= b[1]) && (b[1] - b[0] >= duration))) {
result.push(b[0]);
result.push(getMin(a[1], b[1]));
}
}
return result;
}
function checkRight(a, b, duration) {
let result = [];
if ((b[0] <= a[0]) && (b[1] > a[0])) {
if (((a[1] < b[1]) && (a[1] - a[0] >= duration)) ||
((a[1] >= b[1]) && (b[1] - a[0] >= duration))) {
result.push(a[0]);
result.push(getMin(a[1], b[1]));
}
}
return result;
}
function checkInterval(gang, bank, min) {
let result = [];
let left = checkLeft(gang, bank, min);
let right = checkRight(gang, bank, min);
if (left.length !== 0) {
result = left;
}
if (right.length !== 0) {
result = right;
}
return result;
}
function check(records) {
if (records.length !== 0) {
return true;
}
return false;
}
function showList(records, duration) {
let timelist = [];
let n;
for (let i = 0; i < records.length; i++) {
n = (records[i][0]);
while ((n < records[i][1]) &&
(records[i][1] - n >= duration)) {
timelist.push(n);
n += 30;
n += duration;
}
}
return timelist;
}
function createTimeList(records, duration) {
let timelist = [];
if (check(records)) {
timelist = showList(records, duration);
}
return timelist;
}
function transform(number) {
let result = '';
if (number < 10) {
result = ('0' + number);
} else {
result = number;
}
return result;
}
function changeFormate(number) {
let time = [];
let oldTime = number;
if (oldTime > DAYS.ВТ && oldTime <= DAYS.СР) {
time.push('СР');
oldTime -= DAYS.ВТ;
}
if (oldTime > DAYS.ПН && oldTime <= DAYS.ВТ) {
time.push('ВТ');
oldTime -= DAYS.ПН;
} else {
time.push('ПН');
}
let hours = (oldTime - (oldTime % MIN_IN_HOUR)) / MIN_IN_HOUR;
let minutes = oldTime % MIN_IN_HOUR;
time.push(transform (hours));
time.push(transform(minutes));
return time;
}
function formatList(timeList) {
let result = [];
for (let i = 0; i < timeList.length; i++) {
result.push(changeFormate(timeList[i]));
}
return result;
}