From 8aed3893380cfcb9b22e054dba86a7feb56dd8bf Mon Sep 17 00:00:00 2001 From: AKuznecoff Date: Tue, 30 Oct 2018 01:31:57 +0500 Subject: [PATCH 1/4] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=B2=D0=B0=D1=80=D0=B8=D0=B0=D0=BD=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index ed84b5d..77a9c9f 100755 --- a/robbery.js +++ b/robbery.js @@ -5,6 +5,10 @@ * Реализовано оба метода и tryLater */ const isStar = true; +const days = ['ПН', 'ВТ', 'СР']; +const minutesInHour = 60; +const minutesInDay = minutesInHour * 24; + /** * @param {Object} schedule – Расписание Банды @@ -16,15 +20,20 @@ const isStar = true; */ function getAppropriateMoment(schedule, duration, workingHours) { console.info(schedule, duration, workingHours); + const bankTimeZone = getTimeZone(workingHours); + const bankWorkingMinutes = getBankWorkingMinutes(workingHours); + const scheduleInMinutes = getScheduleInMinutes(schedule, bankTimeZone); + const posibleTime = getPosibleTimes(scheduleInMinutes, duration, bankWorkingMinutes); return { + time: posibleTime, /** * Найдено ли время * @returns {Boolean} */ exists: function () { - return false; + return this.time.length !== 0; }, /** @@ -34,7 +43,17 @@ function getAppropriateMoment(schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - return template; + if (this.exists()) { + const day = days[Math.floor(this.time[0] / minutesInDay)]; + const hours = Math.floor((this.time[0] % minutesInDay) / minutesInHour); + const minutes = (this.time[0] % minutesInDay) % minutesInHour; + + return template.replace('%DD', day) + .replace('%HH', getNumber(hours)) + .replace('%MM', getNumber(minutes)); + } + + return ''; }, /** @@ -43,11 +62,102 @@ function getAppropriateMoment(schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { + if (this.time.length > 1) { + this.time.shift(); + + return true; + } + return false; } }; } +function getTimeZone(time) { + return parseInt(time.from.match(/\d+$/)[0]); +} + +function getTimeInMinutes(time) { + let hours = parseInt(time.substr(0, 2)); + let minutes = parseInt(time.substr(3, 2)); + + return hours * minutesInHour + minutes; +} + +function getRobberScheduleInMinutes(schedule, bankTimeZone) { + let scheduleInMinutes = []; + let differenceTimeZone = bankTimeZone - getTimeZone(schedule[0]); + schedule.forEach(time => scheduleInMinutes.push({ + from: convertDayTimeToMinutes(time.from) + differenceTimeZone * minutesInHour, + to: convertDayTimeToMinutes(time.to) + differenceTimeZone * minutesInHour + })); + + return scheduleInMinutes; +} + +function convertDayTimeToMinutes(dayTime) { + let dayCoefficient = days.indexOf(dayTime.substr(0, 2)); + let timeInMinutes = getTimeInMinutes(dayTime.substr(3, 5)); + timeInMinutes += dayCoefficient * minutesInDay; + + return timeInMinutes; +} + +function getBankWorkingMinutes(workingHours) { + let openTimeMinutes = getTimeInMinutes(workingHours.from); + let closeTimeMinutes = getTimeInMinutes(workingHours.to); + + return [ + { from: openTimeMinutes, to: closeTimeMinutes }, + { from: openTimeMinutes + minutesInDay, to: closeTimeMinutes + minutesInDay }, + { from: openTimeMinutes + 2 * minutesInDay, to: closeTimeMinutes + 2 * minutesInDay } + ]; +} + +function getScheduleInMinutes(schedule, bankTimeZone) { + let scheduleInMinutes = {}; + let robbers = Object.keys(schedule); + for (let i = 0; i < robbers.length; i++) { + let robber = robbers[i]; + scheduleInMinutes[robber] = getRobberScheduleInMinutes(schedule[robber], bankTimeZone); + } + + return scheduleInMinutes; +} + +function checkTime(time, start, duration) { + return (time.from < start && start < time.to) || + (time.from < start + duration && start + duration < time.to); +} + +function getPosibleTimes(schedule, duration, workingMinutes) { + const posibleTime = []; + const keys = Object.keys(schedule); + for (let i = workingMinutes[0].from; i <= workingMinutes[workingMinutes.length - 1].to;) { + if (keys.some(key => + schedule[key].some(time => checkTime(time, i, duration))) || + workingMinutes.every(time => i < time.from || time.to < i + duration)) { + i++; + continue; + } + if (posibleTime.length === 0 || i - posibleTime[posibleTime.length - 1] >= 30) { + posibleTime.push(i); + } + i++; + } + + return posibleTime; +} + +function getNumber(number) { + if (number < 10) { + + return `0${number}`; + } + + return `${number}`; +} + module.exports = { getAppropriateMoment, From cf6b1826741ed308fd5e5562b18c6b33e4ace850 Mon Sep 17 00:00:00 2001 From: Kuznecoff Date: Tue, 30 Oct 2018 17:00:02 +0500 Subject: [PATCH 2/4] =?UTF-8?q?=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/robbery.js b/robbery.js index 77a9c9f..f77df21 100755 --- a/robbery.js +++ b/robbery.js @@ -26,14 +26,13 @@ function getAppropriateMoment(schedule, duration, workingHours) { const posibleTime = getPosibleTimes(scheduleInMinutes, duration, bankWorkingMinutes); return { - time: posibleTime, /** * Найдено ли время * @returns {Boolean} */ exists: function () { - return this.time.length !== 0; + return posibleTime.length !== 0; }, /** @@ -44,9 +43,9 @@ function getAppropriateMoment(schedule, duration, workingHours) { */ format: function (template) { if (this.exists()) { - const day = days[Math.floor(this.time[0] / minutesInDay)]; - const hours = Math.floor((this.time[0] % minutesInDay) / minutesInHour); - const minutes = (this.time[0] % minutesInDay) % minutesInHour; + const day = days[Math.floor(posibleTime[0] / minutesInDay)]; + const hours = Math.floor((posibleTime[0] % minutesInDay) / minutesInHour); + const minutes = (posibleTime[0] % minutesInDay) % minutesInHour; return template.replace('%DD', day) .replace('%HH', getNumber(hours)) @@ -62,8 +61,8 @@ function getAppropriateMoment(schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { - if (this.time.length > 1) { - this.time.shift(); + if (posibleTime.length > 1) { + posibleTime.shift(); return true; } From 123bd6d9f5ffba52f88d89c25e9f2e511459be92 Mon Sep 17 00:00:00 2001 From: Kuznecoff Date: Tue, 30 Oct 2018 17:24:10 +0500 Subject: [PATCH 3/4] =?UTF-8?q?=D1=82=D1=80=D0=B5=D1=82=D1=8C=D1=8F=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=8B=D1=82=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/robbery.js b/robbery.js index f77df21..17ef7a1 100755 --- a/robbery.js +++ b/robbery.js @@ -85,11 +85,13 @@ function getTimeInMinutes(time) { function getRobberScheduleInMinutes(schedule, bankTimeZone) { let scheduleInMinutes = []; - let differenceTimeZone = bankTimeZone - getTimeZone(schedule[0]); - schedule.forEach(time => scheduleInMinutes.push({ - from: convertDayTimeToMinutes(time.from) + differenceTimeZone * minutesInHour, - to: convertDayTimeToMinutes(time.to) + differenceTimeZone * minutesInHour - })); + if (schedule.length !== 0) { + let differenceTimeZone = bankTimeZone - getTimeZone(schedule[0]); + schedule.forEach(time => scheduleInMinutes.push({ + from: convertDayTimeToMinutes(time.from) + differenceTimeZone * minutesInHour, + to: convertDayTimeToMinutes(time.to) + differenceTimeZone * minutesInHour + })); + } return scheduleInMinutes; } From 1716cac5cf03f6c130727dd872e85f554a63134e Mon Sep 17 00:00:00 2001 From: AKuznecoff Date: Mon, 5 Nov 2018 00:20:49 +0500 Subject: [PATCH 4/4] fix --- robbery.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 17ef7a1..9d463dd 100755 --- a/robbery.js +++ b/robbery.js @@ -128,7 +128,8 @@ function getScheduleInMinutes(schedule, bankTimeZone) { function checkTime(time, start, duration) { return (time.from < start && start < time.to) || - (time.from < start + duration && start + duration < time.to); + (time.from < start + duration && start + duration < time.to) || + (time.from > start && time.from < start + duration); } function getPosibleTimes(schedule, duration, workingMinutes) {