From 15dca75c0761e635f8c04ed0d7a2116fcbe9c29c Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 28 Oct 2018 17:46:26 +0500 Subject: [PATCH 1/7] =?UTF-8?q?=D0=93=D0=BE=D0=BC=D0=B7=D1=8F=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 107 insertions(+), 3 deletions(-) diff --git a/robbery.js b/robbery.js index ed84b5d..aa1498f 100755 --- a/robbery.js +++ b/robbery.js @@ -6,6 +6,81 @@ */ const isStar = true; +let timeLine = []; + +function ConvertDayToMinuts(day) { + if (day === "") { + + return 24 * 60; + } + if (day === "") { + + return 48 * 60; + } + if (day === "") { + + return 72 * 60; + } +} + +function ConvertminutsToFormat(minuts, bankTimeZone) { + minuts += bankTimeZone * 60; + let day = Math.trunc(minuts / 24 * 60); + minuts -= day * 24 * 60; + let hours = Math.trunc(minuts / 60); + minuts -= hours * 60; + let time = { + hours : hours, + minuts : minuts, + }; + if (day === 1) { + time.day = 'ПН'; + } + if (day === 2) { + time.day = 'ВТ'; + } + if (day === 3) { + time.day = 'СР'; + } + + return time; +} + +function ConvertToMinuts(timeString) { + let minuts = 0; + minuts += ConvertDayToMinuts(timeString.slice(0, 2)); + let hours = parseInt(timeString.match("(\\d\\d):(\\d\\d)")[1]); + minuts += parseInt(timeString.match("(\\d\\d):(\\d\\d)")[2]); + let timeZone = parseInt(timeString.match("\+(\\d)")[1]); + minuts += (hours - timeZone) * 60; + + return minuts; +} + +function AddWorkingHoursToTimeLine(workingHours) { + timeLine.push({ first : ConvertToMinuts("ПН " + workingHours.from), second : 1 }); + timeLine.push({ first : ConvertToMinuts("ПН " + workingHours.to), second : -1 }); + timeLine.push({ first : ConvertToMinuts("ВТ " + workingHours.from), second : 1 }); + timeLine.push({ first : ConvertToMinuts("ВТ " + workingHours.to), second : -1 }); + timeLine.push({ first : ConvertToMinuts("СР " + workingHours.from), second : 1 }); + timeLine.push({ first : ConvertToMinuts("СР " + workingHours.to), second : -1 }); +} + +function scanLine(duration, lastAppropriateMoment) { + let notBusy = 0; + for (let i = 0; i < timeLine.length - 1; i++) { + notBusy += timeLine[i].second; + if (notBusy === 4 && + timeLine[i + 1].first - timeLine[i].first >= duration && + timeLine[i].first > lastAppropriateMoment) { + + return timeLine[i].first; + } + + } + return -1; +} + /** * @param {Object} schedule – Расписание Банды * @param {Number} duration - Время на ограбление в минутах @@ -14,8 +89,21 @@ const isStar = true; * @param {String} workingHours.to – Время закрытия, например, "18:00+5" * @returns {Object} */ + function getAppropriateMoment(schedule, duration, workingHours) { console.info(schedule, duration, workingHours); + let bankTimeZone = parseInt(workingHours.from.match('\+(\\d)')[1]); + let keys = Object.keys(schedule); + for (let i = 0; i < keys.length; i++) { + let roberSchedule = schedule[keys[i]]; + for (let j = 0; j < roberSchedule; j++) { + timeLine.push({ first : ConvertToMinuts(roberSchedule[j].from), second : 1 }); + timeLine.push({ first : ConvertToMinuts(roberSchedule[j].to), second : -1 }) + } + } + AddWorkingHoursToTimeLine(workingHours); + timeLine.sort((a, b) => a.first - b.first); + let approproateMoment = scanLine(duration, -1); return { @@ -24,7 +112,7 @@ function getAppropriateMoment(schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - return false; + return approproateMoment !== -1; }, /** @@ -34,7 +122,16 @@ function getAppropriateMoment(schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - return template; + if (approproateMoment !== -1) { + let time = ConvertminutsToFormat(approproateMoment, bankTimeZone); + template.replace('%HH', time.hours.toString()); + template.replace('%MM', time.minuts.toString()); + template.replace('%DD', time.day); + + return template; + } else { + return ''; + } }, /** @@ -43,7 +140,14 @@ function getAppropriateMoment(schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { - return false; + let newApproproateMoment = scanLine(duration, approproateMoment); + if (newApproproateMoment !== -1) { + approproateMoment = newApproproateMoment; + + return true; + } else { + return false; + } } }; } From ec7d545f4a41a7357faa52c2ee573953955a4163 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 28 Oct 2018 18:01:41 +0500 Subject: [PATCH 2/7] =?UTF-8?q?=D0=92=D1=82=D0=BE=D1=80=D0=BE=D0=B9=20?= =?UTF-8?q?=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=20=D1=80=D0=B5=D1=88?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 59 +++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/robbery.js b/robbery.js index aa1498f..0d40b15 100755 --- a/robbery.js +++ b/robbery.js @@ -8,30 +8,30 @@ const isStar = true; let timeLine = []; -function ConvertDayToMinuts(day) { - if (day === "") { +function convertDayToMinuts(day) { + if (day === 'ПН') { return 24 * 60; } - if (day === "") { + if (day === 'ВТ') { return 48 * 60; } - if (day === "") { + if (day === 'СР') { return 72 * 60; } } -function ConvertminutsToFormat(minuts, bankTimeZone) { +function сonvertminutsToFormat(minuts, bankTimeZone) { minuts += bankTimeZone * 60; let day = Math.trunc(minuts / 24 * 60); minuts -= day * 24 * 60; let hours = Math.trunc(minuts / 60); minuts -= hours * 60; let time = { - hours : hours, - minuts : minuts, + hours: hours, + minuts: minuts }; if (day === 1) { time.day = 'ПН'; @@ -46,24 +46,24 @@ function ConvertminutsToFormat(minuts, bankTimeZone) { return time; } -function ConvertToMinuts(timeString) { +function convertToMinuts(timeString) { let minuts = 0; - minuts += ConvertDayToMinuts(timeString.slice(0, 2)); - let hours = parseInt(timeString.match("(\\d\\d):(\\d\\d)")[1]); - minuts += parseInt(timeString.match("(\\d\\d):(\\d\\d)")[2]); - let timeZone = parseInt(timeString.match("\+(\\d)")[1]); + minuts += convertDayToMinuts(timeString.slice(0, 2)); + let hours = parseInt(timeString.match('(\\d\\d):(\\d\\d)')[1]); + minuts += parseInt(timeString.match('(\\d\\d):(\\d\\d)')[2]); + let timeZone = parseInt(timeString.match('\\+(\\d)')[1]); minuts += (hours - timeZone) * 60; return minuts; } -function AddWorkingHoursToTimeLine(workingHours) { - timeLine.push({ first : ConvertToMinuts("ПН " + workingHours.from), second : 1 }); - timeLine.push({ first : ConvertToMinuts("ПН " + workingHours.to), second : -1 }); - timeLine.push({ first : ConvertToMinuts("ВТ " + workingHours.from), second : 1 }); - timeLine.push({ first : ConvertToMinuts("ВТ " + workingHours.to), second : -1 }); - timeLine.push({ first : ConvertToMinuts("СР " + workingHours.from), second : 1 }); - timeLine.push({ first : ConvertToMinuts("СР " + workingHours.to), second : -1 }); +function addWorkingHoursToTimeLine(workingHours) { + timeLine.push({ first: convertToMinuts('ПН ' + workingHours.from), second: 1 }); + timeLine.push({ first: convertToMinuts('ПН ' + workingHours.to), second: -1 }); + timeLine.push({ first: convertToMinuts('ВТ ' + workingHours.from), second: 1 }); + timeLine.push({ first: convertToMinuts('ВТ ' + workingHours.to), second: -1 }); + timeLine.push({ first: convertToMinuts('СР ' + workingHours.from), second: 1 }); + timeLine.push({ first: convertToMinuts('СР ' + workingHours.to), second: -1 }); } function scanLine(duration, lastAppropriateMoment) { @@ -78,6 +78,7 @@ function scanLine(duration, lastAppropriateMoment) { } } + return -1; } @@ -92,16 +93,16 @@ function scanLine(duration, lastAppropriateMoment) { function getAppropriateMoment(schedule, duration, workingHours) { console.info(schedule, duration, workingHours); - let bankTimeZone = parseInt(workingHours.from.match('\+(\\d)')[1]); + let bankTimeZone = parseInt(workingHours.from.match('\\+(\\d)')[1]); let keys = Object.keys(schedule); for (let i = 0; i < keys.length; i++) { let roberSchedule = schedule[keys[i]]; for (let j = 0; j < roberSchedule; j++) { - timeLine.push({ first : ConvertToMinuts(roberSchedule[j].from), second : 1 }); - timeLine.push({ first : ConvertToMinuts(roberSchedule[j].to), second : -1 }) + timeLine.push({ first: convertToMinuts(roberSchedule[j].from), second: 1 }); + timeLine.push({ first: convertToMinuts(roberSchedule[j].to), second: -1 }); } } - AddWorkingHoursToTimeLine(workingHours); + addWorkingHoursToTimeLine(workingHours); timeLine.sort((a, b) => a.first - b.first); let approproateMoment = scanLine(duration, -1); @@ -123,15 +124,14 @@ function getAppropriateMoment(schedule, duration, workingHours) { */ format: function (template) { if (approproateMoment !== -1) { - let time = ConvertminutsToFormat(approproateMoment, bankTimeZone); + let time = сonvertminutsToFormat(approproateMoment, bankTimeZone); template.replace('%HH', time.hours.toString()); template.replace('%MM', time.minuts.toString()); template.replace('%DD', time.day); - return template; - } else { - return ''; } + + return ''; }, /** @@ -143,11 +143,10 @@ function getAppropriateMoment(schedule, duration, workingHours) { let newApproproateMoment = scanLine(duration, approproateMoment); if (newApproproateMoment !== -1) { approproateMoment = newApproproateMoment; - return true; - } else { - return false; } + + return false; } }; } From 01320703f49e167b767bb68dd5660a5aee6d15b6 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 28 Oct 2018 18:04:11 +0500 Subject: [PATCH 3/7] =?UTF-8?q?=D0=A2=D1=80=D0=B5=D1=82=D0=B8=D0=B9=20?= =?UTF-8?q?=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=20=D1=80=D0=B5=D1=88?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/robbery.js b/robbery.js index 0d40b15..09c6462 100755 --- a/robbery.js +++ b/robbery.js @@ -128,6 +128,7 @@ function getAppropriateMoment(schedule, duration, workingHours) { template.replace('%HH', time.hours.toString()); template.replace('%MM', time.minuts.toString()); template.replace('%DD', time.day); + return template; } @@ -143,6 +144,7 @@ function getAppropriateMoment(schedule, duration, workingHours) { let newApproproateMoment = scanLine(duration, approproateMoment); if (newApproproateMoment !== -1) { approproateMoment = newApproproateMoment; + return true; } From 4e73b27ee28d332c61b32d648731778f5b09053d Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 28 Oct 2018 19:23:43 +0500 Subject: [PATCH 4/7] =?UTF-8?q?=D1=82=D1=80=D0=B5=D1=82=D0=B8=D0=B9=20?= =?UTF-8?q?=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=20=D1=80=D0=B5=D1=88?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/robbery.js b/robbery.js index 09c6462..321fdc7 100755 --- a/robbery.js +++ b/robbery.js @@ -25,7 +25,7 @@ function convertDayToMinuts(day) { function сonvertminutsToFormat(minuts, bankTimeZone) { minuts += bankTimeZone * 60; - let day = Math.trunc(minuts / 24 * 60); + let day = Math.trunc(minuts / (24 * 60)); minuts -= day * 24 * 60; let hours = Math.trunc(minuts / 60); minuts -= hours * 60; @@ -71,7 +71,7 @@ function scanLine(duration, lastAppropriateMoment) { for (let i = 0; i < timeLine.length - 1; i++) { notBusy += timeLine[i].second; if (notBusy === 4 && - timeLine[i + 1].first - timeLine[i].first >= duration && + timeLine[i + 1].first - timeLine[i].first > duration && timeLine[i].first > lastAppropriateMoment) { return timeLine[i].first; @@ -92,12 +92,12 @@ function scanLine(duration, lastAppropriateMoment) { */ function getAppropriateMoment(schedule, duration, workingHours) { - console.info(schedule, duration, workingHours); + //console.info(schedule, duration, workingHours); let bankTimeZone = parseInt(workingHours.from.match('\\+(\\d)')[1]); let keys = Object.keys(schedule); for (let i = 0; i < keys.length; i++) { let roberSchedule = schedule[keys[i]]; - for (let j = 0; j < roberSchedule; j++) { + for (let j = 0; j < roberSchedule.length; j++) { timeLine.push({ first: convertToMinuts(roberSchedule[j].from), second: 1 }); timeLine.push({ first: convertToMinuts(roberSchedule[j].to), second: -1 }); } @@ -125,11 +125,11 @@ function getAppropriateMoment(schedule, duration, workingHours) { format: function (template) { if (approproateMoment !== -1) { let time = сonvertminutsToFormat(approproateMoment, bankTimeZone); - template.replace('%HH', time.hours.toString()); - template.replace('%MM', time.minuts.toString()); - template.replace('%DD', time.day); + let answer = template.replace(/%HH/, time.hours.toString()) + .replace(/%MM/gi, time.minuts.toString()) + .replace(/%DD/gi, time.day); - return template; + return answer; } return ''; From 2abab618921c25f800865fa9a79c178a29825d52 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 28 Oct 2018 19:26:46 +0500 Subject: [PATCH 5/7] =?UTF-8?q?=D1=87=D0=B5=D1=82=D0=B2=D0=B5=D1=80=D1=82?= =?UTF-8?q?=D1=8B=D0=B9=20=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82=20?= =?UTF-8?q?=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/robbery.js b/robbery.js index 321fdc7..502d316 100755 --- a/robbery.js +++ b/robbery.js @@ -92,7 +92,7 @@ function scanLine(duration, lastAppropriateMoment) { */ function getAppropriateMoment(schedule, duration, workingHours) { - //console.info(schedule, duration, workingHours); + console.info(schedule, duration, workingHours); let bankTimeZone = parseInt(workingHours.from.match('\\+(\\d)')[1]); let keys = Object.keys(schedule); for (let i = 0; i < keys.length; i++) { @@ -126,8 +126,8 @@ function getAppropriateMoment(schedule, duration, workingHours) { if (approproateMoment !== -1) { let time = сonvertminutsToFormat(approproateMoment, bankTimeZone); let answer = template.replace(/%HH/, time.hours.toString()) - .replace(/%MM/gi, time.minuts.toString()) - .replace(/%DD/gi, time.day); + .replace(/%MM/gi, time.minuts.toString()) + .replace(/%DD/gi, time.day); return answer; } From f8165ed707ab688e67022f8b5c308e286a8fd5a2 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 30 Oct 2018 11:21:05 +0500 Subject: [PATCH 6/7] fix bags --- robbery.js | 62 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/robbery.js b/robbery.js index 502d316..61c60da 100755 --- a/robbery.js +++ b/robbery.js @@ -6,7 +6,7 @@ */ const isStar = true; -let timeLine = []; + function convertDayToMinuts(day) { if (day === 'ПН') { @@ -57,21 +57,21 @@ function convertToMinuts(timeString) { return minuts; } -function addWorkingHoursToTimeLine(workingHours) { - timeLine.push({ first: convertToMinuts('ПН ' + workingHours.from), second: 1 }); - timeLine.push({ first: convertToMinuts('ПН ' + workingHours.to), second: -1 }); - timeLine.push({ first: convertToMinuts('ВТ ' + workingHours.from), second: 1 }); - timeLine.push({ first: convertToMinuts('ВТ ' + workingHours.to), second: -1 }); - timeLine.push({ first: convertToMinuts('СР ' + workingHours.from), second: 1 }); - timeLine.push({ first: convertToMinuts('СР ' + workingHours.to), second: -1 }); +function addWorkingHoursToTimeLine(timeLine, workingHours) { + timeLine.push({ first: convertToMinuts('ПН ' + workingHours.from), second: 3 }); + timeLine.push({ first: convertToMinuts('ПН ' + workingHours.to), second: -3 }); + timeLine.push({ first: convertToMinuts('ВТ ' + workingHours.from), second: 3 }); + timeLine.push({ first: convertToMinuts('ВТ ' + workingHours.to), second: -3 }); + timeLine.push({ first: convertToMinuts('СР ' + workingHours.from), second: 3 }); + timeLine.push({ first: convertToMinuts('СР ' + workingHours.to), second: -3 }); } -function scanLine(duration, lastAppropriateMoment) { - let notBusy = 0; +function scanLine(timeLine, duration, lastAppropriateMoment) { + let busy = 0; for (let i = 0; i < timeLine.length - 1; i++) { - notBusy += timeLine[i].second; - if (notBusy === 4 && - timeLine[i + 1].first - timeLine[i].first > duration && + busy += timeLine[i].second; + if (busy === 3 && + (timeLine[i + 1].first - timeLine[i].first) >= duration && timeLine[i].first > lastAppropriateMoment) { return timeLine[i].first; @@ -92,20 +92,23 @@ function scanLine(duration, lastAppropriateMoment) { */ function getAppropriateMoment(schedule, duration, workingHours) { + let timeLine = []; console.info(schedule, duration, workingHours); let bankTimeZone = parseInt(workingHours.from.match('\\+(\\d)')[1]); let keys = Object.keys(schedule); for (let i = 0; i < keys.length; i++) { let roberSchedule = schedule[keys[i]]; for (let j = 0; j < roberSchedule.length; j++) { - timeLine.push({ first: convertToMinuts(roberSchedule[j].from), second: 1 }); - timeLine.push({ first: convertToMinuts(roberSchedule[j].to), second: -1 }); + timeLine.push({ first: convertToMinuts(roberSchedule[j].from), second: -1 }); + timeLine.push({ first: convertToMinuts(roberSchedule[j].to), second: 1 }); } } - addWorkingHoursToTimeLine(workingHours); - timeLine.sort((a, b) => a.first - b.first); - let approproateMoment = scanLine(duration, -1); - + addWorkingHoursToTimeLine(timeLine, workingHours); + timeLine.sort((a, b) => a !== b ? a.first - b.first : b.second - a.second); + let approproateMoment = scanLine(timeLine, duration, -1); + timeLine: timeLine; + approproateMoment: approproateMoment; + return { /** @@ -125,8 +128,21 @@ function getAppropriateMoment(schedule, duration, workingHours) { format: function (template) { if (approproateMoment !== -1) { let time = сonvertminutsToFormat(approproateMoment, bankTimeZone); - let answer = template.replace(/%HH/, time.hours.toString()) - .replace(/%MM/gi, time.minuts.toString()) + let hours; + let minuts; + if (time.hours < 10) { + hours = '0' + time.hours.toString(); + } else { + hours = time.hours.toString(); + } + if (time.minuts < 10) { + minuts = '0' + time.minuts.toString(); + } else { + minuts = time.minuts.toString(); + } + + let answer = template.replace(/%HH/, hours) + .replace(/%MM/gi, minuts) .replace(/%DD/gi, time.day); return answer; @@ -141,7 +157,9 @@ function getAppropriateMoment(schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { - let newApproproateMoment = scanLine(duration, approproateMoment); + timeLine.push({ first: approproateMoment + 30, second: 0 }); + timeLine.sort((a, b) => a !== b ? a.first - b.first : b.second - a.second); + let newApproproateMoment = scanLine(timeLine, duration, approproateMoment); if (newApproproateMoment !== -1) { approproateMoment = newApproproateMoment; From 075bacdf262e471cd259768ea2e17e78e81bf5c0 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 30 Oct 2018 11:26:35 +0500 Subject: [PATCH 7/7] Update robbery.js --- robbery.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/robbery.js b/robbery.js index 61c60da..2273f57 100755 --- a/robbery.js +++ b/robbery.js @@ -7,7 +7,6 @@ const isStar = true; - function convertDayToMinuts(day) { if (day === 'ПН') { @@ -106,9 +105,7 @@ function getAppropriateMoment(schedule, duration, workingHours) { addWorkingHoursToTimeLine(timeLine, workingHours); timeLine.sort((a, b) => a !== b ? a.first - b.first : b.second - a.second); let approproateMoment = scanLine(timeLine, duration, -1); - timeLine: timeLine; - approproateMoment: approproateMoment; - + return { /** @@ -140,7 +137,6 @@ function getAppropriateMoment(schedule, duration, workingHours) { } else { minuts = time.minuts.toString(); } - let answer = template.replace(/%HH/, hours) .replace(/%MM/gi, minuts) .replace(/%DD/gi, time.day);