From b41fabf92154c2ac49f82d69478b2aeb254c1a15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Tue, 25 Oct 2016 23:38:18 +0500 Subject: [PATCH 01/23] =?UTF-8?q?=D0=94=D1=80=D1=83=D0=B7=D1=8C=D1=8F?= =?UTF-8?q?=D1=88=D0=BA=D0=B8=20=D0=9E=D1=83=D1=88=D0=B5=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 254 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 250 insertions(+), 4 deletions(-) diff --git a/robbery.js b/robbery.js index 4a8309d..015ba08 100644 --- a/robbery.js +++ b/robbery.js @@ -15,7 +15,15 @@ exports.isStar = true; * @returns {Object} */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - console.info(schedule, duration, workingHours); + + var bankIntervals = getBankIntervals(workingHours); + var bankUTC = parseInt(workingHours.from.split('+')[1]); + var gangIntervals = getGangIntervals(schedule, bankUTC); + + var robberyIntervals = findRobberyIntervals(bankIntervals, gangIntervals); + var goodTimes = getGoodTimesForRobbery(robberyIntervals, duration); + var exists = goodTimes.length !== 0; + var time = goodTimes.shift(); return { @@ -24,7 +32,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - return false; + return exists; }, /** @@ -35,7 +43,19 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - return template; + if (!exists) { + return ''; + } + + var day = ['ПН', 'ВТ', 'СР'][Math.floor(time / (24 * 60))]; + var hour = Math.floor((time % (24 * 60)) / 60); + hour = (hour < 10 ? '0' : '') + hour; + var minute = (time % (24 * 60)) % 60; + minute = (minute < 10 ? '0' : '') + minute; + + return template.replace('%DD', day) + .replace('%HH', hour) + .replace('%MM', minute); }, /** @@ -44,7 +64,233 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { - return false; + var oldTime = time; + time = goodTimes.shift() || time; + + return time !== oldTime; } }; }; + +function getBankIntervals(workingHours) { + var bankIntervals = []; + + var bankFrom = getDateFromString(workingHours.from); + var bankTo = getDateFromString(workingHours.to); + + bankIntervals.push({ + from: getTimeFromDate(bankFrom, 'ПН'), + to: getTimeFromDate(bankTo, 'ПН') + }); + + bankIntervals.push({ + from: getTimeFromDate(bankFrom, 'ВТ'), + to: getTimeFromDate(bankTo, 'ВТ') + }); + + bankIntervals.push({ + from: getTimeFromDate(bankFrom, 'СР'), + to: getTimeFromDate(bankTo, 'СР') + }); + + return bankIntervals; +} + +function getDateFromString(string) { + var components = string.split(' '); + var date = { + day: (components.length > 1) ? components[0] : '', + time: (components.length > 1) ? components[1] : components[0] + }; + + components = date.time.split(/\D/); + date.time = { + hours: parseInt(components[0]), + minutes: parseInt(components[1]), + utc: parseInt(components[2]) + }; + + return date; +} + +function getTimeFromDate(date, day) { + var minutes = convertTimeInMinutes(date.time); + var minutesWithDay; + if (day === undefined) { + minutesWithDay = addDayToTime(minutes, date.day); + } else { + minutesWithDay = addDayToTime(minutes, day); + } + + return minutesWithDay; +} + +function convertTimeInMinutes(time) { + return 60 * time.hours + time.minutes; +} + +function addDayToTime(time, day) { + if (day === 'ВТ') { + time += 24 * 60; + } else if (day === 'СР') { + time += 2 * 24 * 60; + } + + return time; +} + +function getGangIntervals(shedule, bankUTC) { + var gangIntervals = []; + + pushGangIntervals(gangIntervals, shedule.Danny, bankUTC); + pushGangIntervals(gangIntervals, shedule.Rusty, bankUTC); + pushGangIntervals(gangIntervals, shedule.Linus, bankUTC); + + gangIntervals.sort(function (interval1, interval2) { + return interval1.from > interval2.from; + }); + + return gangIntervals; +} + +function pushGangIntervals(intervals, someonesShedule, bankUTC) { + for (var i = 0; i < someonesShedule.length; i++) { + pushGangInterval(intervals, someonesShedule[i], bankUTC); + } +} + +function pushGangInterval(intervals, lineOfShedule, bankUTC) { + var lineFrom = getDateFromString(lineOfShedule.from); + var lineTo = getDateFromString(lineOfShedule.to); + + var timeFromInMinutes = getTimeFromDate(lineFrom); + var timeToInMinutes = getTimeFromDate(lineTo); + + var timeFromWithUTC = addUTCToTime(timeFromInMinutes, lineFrom.time.utc, bankUTC); + var timeToWithUTC = addUTCToTime(timeToInMinutes, lineTo.time.utc, bankUTC); + + intervals.push({ + from: correctTime(timeFromWithUTC), + to: correctTime(timeToWithUTC) + }); +} + +function addUTCToTime(time, utc, bankUTC) { + return time - 60 * (utc - bankUTC); +} + +function correctTime(time) { + if (time < 0) { + time = 0; + } else if (time > 3 * 24 * 60) { + time = 3 * 24 * 60; + } + + return time; +} + +function findNewStart(gangIntervals, start) { + return gangIntervals.filter(function (interval) { + return interval.from <= start && start <= interval.to; + }); +} + +function findBadStart(gangIntervals, start, end) { + return gangIntervals.filter(function (interval) { + return start <= interval.from && interval.to <= end; + }); +} + +function exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd) { + while (robberyStart <= robberyEnd) { + var badStart = findBadStart(gangIntervals, robberyStart, robberyEnd)[0]; + + var from = badStart ? badStart.from : robberyEnd; + + robberyIntervals.push({ + from: robberyStart, + to: from + }); + + if (from === robberyEnd) { + break; + } + + robberyStart = badStart ? badStart.to : robberyEnd; + + robberyStart = findNewStart(gangIntervals, robberyStart) + .map(function (elem) { + return elem.to; + }) + .sort(function (a, b) { + return b > a; + })[0]; + } +} + +function findRobberyIntervals(bankIntervals, gangIntervals) { + var robberyIntervals = []; + + for (var i = 0; i < bankIntervals.length; i++) { + var bankInterval = bankIntervals[i]; + var robberyInterval = findPossibleRobberyInterval(gangIntervals, bankInterval); + var robberyStart = robberyInterval[0]; + var robberyEnd = robberyInterval[1]; + + exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd); + // while (robberyStart <= robberyEnd) { + // var badStart = findBadStart(gangIntervals, robberyStart, robberyEnd)[0]; + + // var from = badStart ? badStart.from : robberyEnd; + + // robberyIntervals.push({ + // from: robberyStart, + // to: from + // }); + + // if (from === robberyEnd) { + // break; + // } + + // robberyStart = badStart ? badStart.to : robberyEnd; + + // robberyStart = findNewStart(gangIntervals, robberyStart) + // .map(function (elem) { + // return elem.to; + // }) + // .sort(function (a, b) { + // return b > a; + // })[0]; + + // } + } + + return robberyIntervals; +} + +function findPossibleRobberyInterval(gangIntervals, bankInterval) { + var robberyStart = gangIntervals.filter(function (interval) { + return interval.from <= bankInterval.from && bankInterval.from <= interval.to; + }).slice(-1)[0]; + + var robberyEnd = gangIntervals.filter(function (interval) { + return interval.from <= bankInterval.to && bankInterval.to <= interval.to; + })[0]; + + robberyStart = robberyStart ? robberyStart.to : bankInterval.from; + robberyEnd = robberyEnd ? robberyEnd.from : bankInterval.to; + + return [robberyStart, robberyEnd]; +} + +function getGoodTimesForRobbery(robberyIntervals, duration) { + var goodTimes = []; + robberyIntervals.forEach(function (time) { + while (time.from + duration <= time.to) { + goodTimes.push(time.from); + time.from += 30; + } + }); + + return goodTimes; +} From a1b919489396c0996815efd6719685454b415639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Tue, 25 Oct 2016 23:55:26 +0500 Subject: [PATCH 02/23] =?UTF-8?q?=D0=93=D1=80=D0=B0=D0=BD=D0=B8=D1=87?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20=D0=B7=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 29 ++--------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/robbery.js b/robbery.js index 015ba08..8ff56a8 100644 --- a/robbery.js +++ b/robbery.js @@ -238,31 +238,6 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { var robberyEnd = robberyInterval[1]; exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd); - // while (robberyStart <= robberyEnd) { - // var badStart = findBadStart(gangIntervals, robberyStart, robberyEnd)[0]; - - // var from = badStart ? badStart.from : robberyEnd; - - // robberyIntervals.push({ - // from: robberyStart, - // to: from - // }); - - // if (from === robberyEnd) { - // break; - // } - - // robberyStart = badStart ? badStart.to : robberyEnd; - - // robberyStart = findNewStart(gangIntervals, robberyStart) - // .map(function (elem) { - // return elem.to; - // }) - // .sort(function (a, b) { - // return b > a; - // })[0]; - - // } } return robberyIntervals; @@ -270,11 +245,11 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { function findPossibleRobberyInterval(gangIntervals, bankInterval) { var robberyStart = gangIntervals.filter(function (interval) { - return interval.from <= bankInterval.from && bankInterval.from <= interval.to; + return interval.from <= bankInterval.from && bankInterval.from < interval.to; }).slice(-1)[0]; var robberyEnd = gangIntervals.filter(function (interval) { - return interval.from <= bankInterval.to && bankInterval.to <= interval.to; + return interval.from < bankInterval.to && bankInterval.to < interval.to; })[0]; robberyStart = robberyStart ? robberyStart.to : bankInterval.from; From 0b6d431b88185e0fab9620dbb20a0758ae789082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 12:23:28 +0500 Subject: [PATCH 03/23] =?UTF-8?q?=D0=9A=D0=BE=D0=BB=D0=B8=D1=87=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B2=D0=BE=20=D0=B1=D0=B0=D0=BD=D0=B4=D0=B8=D1=82?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/robbery.js b/robbery.js index 8ff56a8..238592d 100644 --- a/robbery.js +++ b/robbery.js @@ -142,9 +142,14 @@ function addDayToTime(time, day) { function getGangIntervals(shedule, bankUTC) { var gangIntervals = []; - pushGangIntervals(gangIntervals, shedule.Danny, bankUTC); - pushGangIntervals(gangIntervals, shedule.Rusty, bankUTC); - pushGangIntervals(gangIntervals, shedule.Linus, bankUTC); + var bandits = Object.keys(shedule); + for (var i = 0; i < bandits.length; i++) { + var bandit = bandits[i]; + pushGangIntervals(gangIntervals, shedule[bandit], bankUTC); + } + // pushGangIntervals(gangIntervals, shedule.Danny, bankUTC); + // pushGangIntervals(gangIntervals, shedule.Rusty, bankUTC); + // pushGangIntervals(gangIntervals, shedule.Linus, bankUTC); gangIntervals.sort(function (interval1, interval2) { return interval1.from > interval2.from; From cf7ab2d4b91bb742efa9db408cb92301fcb509a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 13:59:35 +0500 Subject: [PATCH 04/23] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=81=D0=B8=D1=82=D1=83=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F,=20=D0=BA=D0=BE=D0=B3=D0=B4=D0=B0=20=D0=B1=D0=B0=D0=BD?= =?UTF-8?q?=D0=BA=20=D0=B2=D0=BE=D0=BE=D0=B1=D1=89=D0=B5=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/robbery.js b/robbery.js index 238592d..de138d7 100644 --- a/robbery.js +++ b/robbery.js @@ -16,14 +16,33 @@ exports.isStar = true; */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { + if (!workingHours || Object.keys(workingHours) === 0) { + return { + + exists: function () { + return false; + }, + + format: function (template) { + template = ''; + + return template; + }, + + tryLater: function () { + return false; + } + }; + } + var bankIntervals = getBankIntervals(workingHours); var bankUTC = parseInt(workingHours.from.split('+')[1]); var gangIntervals = getGangIntervals(schedule, bankUTC); var robberyIntervals = findRobberyIntervals(bankIntervals, gangIntervals); var goodTimes = getGoodTimesForRobbery(robberyIntervals, duration); - var exists = goodTimes.length !== 0; - var time = goodTimes.shift(); + var isExistsTime = goodTimes.length !== 0; + var robberyTime = goodTimes.shift(); return { @@ -32,7 +51,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - return exists; + return isExistsTime; }, /** @@ -43,14 +62,14 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - if (!exists) { + if (!isExistsTime) { return ''; } - var day = ['ПН', 'ВТ', 'СР'][Math.floor(time / (24 * 60))]; - var hour = Math.floor((time % (24 * 60)) / 60); + var day = ['ПН', 'ВТ', 'СР'][Math.floor(robberyTime / (24 * 60))]; + var hour = Math.floor((robberyTime % (24 * 60)) / 60); hour = (hour < 10 ? '0' : '') + hour; - var minute = (time % (24 * 60)) % 60; + var minute = (robberyTime % (24 * 60)) % 60; minute = (minute < 10 ? '0' : '') + minute; return template.replace('%DD', day) @@ -64,10 +83,10 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { - var oldTime = time; - time = goodTimes.shift() || time; + var oldTime = robberyTime; + robberyTime = goodTimes.shift() || robberyTime; - return time !== oldTime; + return robberyTime !== oldTime; } }; }; @@ -147,9 +166,6 @@ function getGangIntervals(shedule, bankUTC) { var bandit = bandits[i]; pushGangIntervals(gangIntervals, shedule[bandit], bankUTC); } - // pushGangIntervals(gangIntervals, shedule.Danny, bankUTC); - // pushGangIntervals(gangIntervals, shedule.Rusty, bankUTC); - // pushGangIntervals(gangIntervals, shedule.Linus, bankUTC); gangIntervals.sort(function (interval1, interval2) { return interval1.from > interval2.from; From 019d6b15abf22696aa8a88ea9083116c2aa1ae82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 14:15:37 +0500 Subject: [PATCH 05/23] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=81=D0=B8=D1=82=D1=83=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F,=20=D0=BA=D0=BE=D0=B3=D0=B4=D0=B0=20=D0=B1=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D0=B8=D1=82=D1=8B=20=D1=81=D0=B2=D0=BE=D0=B1=D0=BE=D0=B4?= =?UTF-8?q?=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index de138d7..30b11f1 100644 --- a/robbery.js +++ b/robbery.js @@ -16,7 +16,7 @@ exports.isStar = true; */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - if (!workingHours || Object.keys(workingHours) === 0) { + if (!workingHours || Object.keys(workingHours).length === 0) { return { exists: function () { @@ -161,6 +161,10 @@ function addDayToTime(time, day) { function getGangIntervals(shedule, bankUTC) { var gangIntervals = []; + if (!shedule || Object.keys(shedule).length === 0) { + return gangIntervals; + } + var bandits = Object.keys(shedule); for (var i = 0; i < bandits.length; i++) { var bandit = bandits[i]; From 1f7108d510e656243108d4b04936b8d989c80766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 16:45:38 +0500 Subject: [PATCH 06/23] =?UTF-8?q?=D0=91=D0=B5=D0=B7=20=D0=B7=D0=B2=D1=91?= =?UTF-8?q?=D0=B7=D0=B4=D0=BE=D1=87=D0=BA=D0=B8?= 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 30b11f1..d11269b 100644 --- a/robbery.js +++ b/robbery.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализовано оба метода и tryLater */ -exports.isStar = true; +exports.isStar = false; /** * @param {Object} schedule – Расписание Банды @@ -270,11 +270,11 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { function findPossibleRobberyInterval(gangIntervals, bankInterval) { var robberyStart = gangIntervals.filter(function (interval) { - return interval.from <= bankInterval.from && bankInterval.from < interval.to; + return interval.from <= bankInterval.from && bankInterval.from <= interval.to; }).slice(-1)[0]; var robberyEnd = gangIntervals.filter(function (interval) { - return interval.from < bankInterval.to && bankInterval.to < interval.to; + return interval.from <= bankInterval.to && bankInterval.to <= interval.to; })[0]; robberyStart = robberyStart ? robberyStart.to : bankInterval.from; From c224a4875d7aa6e2a15e63ee8e0cc4ad2a853f49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 17:36:07 +0500 Subject: [PATCH 07/23] =?UTF-8?q?=D0=92=D0=B5=D1=80=D0=BD=D1=83=D0=BB?= =?UTF-8?q?=D0=B0=20=D0=B7=D0=B2=D1=91=D0=B7=D0=B4=D0=BE=D1=87=D0=BA=D1=83?= =?UTF-8?q?,=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=87=D0=BD=D1=8B=D0=B5=20=D0=B7?= =?UTF-8?q?=D0=BD=D0=B0=D1=87=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 d11269b..72c8bc9 100644 --- a/robbery.js +++ b/robbery.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализовано оба метода и tryLater */ -exports.isStar = false; +exports.isStar = true; /** * @param {Object} schedule – Расписание Банды @@ -270,11 +270,11 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { function findPossibleRobberyInterval(gangIntervals, bankInterval) { var robberyStart = gangIntervals.filter(function (interval) { - return interval.from <= bankInterval.from && bankInterval.from <= interval.to; + return interval.from < bankInterval.from && bankInterval.from < interval.to; }).slice(-1)[0]; var robberyEnd = gangIntervals.filter(function (interval) { - return interval.from <= bankInterval.to && bankInterval.to <= interval.to; + return interval.from < bankInterval.to && bankInterval.to < interval.to; })[0]; robberyStart = robberyStart ? robberyStart.to : bankInterval.from; From da9f5cbf68aece64f66003d3af21b41a5dede0a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 18:05:50 +0500 Subject: [PATCH 08/23] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=20=D0=B8?= =?UTF-8?q?=D0=BD=D1=82=D0=B5=D1=80=D0=B2=D0=B0=D0=BB=D0=BE=D0=B2=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BE=D0=B3=D1=80=D0=B0=D0=B1=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/robbery.js b/robbery.js index 72c8bc9..88d6fb2 100644 --- a/robbery.js +++ b/robbery.js @@ -271,14 +271,28 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { function findPossibleRobberyInterval(gangIntervals, bankInterval) { var robberyStart = gangIntervals.filter(function (interval) { return interval.from < bankInterval.from && bankInterval.from < interval.to; - }).slice(-1)[0]; + }) + .map(function (intervals) { + return intervals.to; + }) + .sort(function (a, b) { + return a > b; + })[0]; var robberyEnd = gangIntervals.filter(function (interval) { return interval.from < bankInterval.to && bankInterval.to < interval.to; + }) + .map(function (intervals) { + return intervals.from; + }) + .sort(function (a, b) { + return a < b; })[0]; - robberyStart = robberyStart ? robberyStart.to : bankInterval.from; - robberyEnd = robberyEnd ? robberyEnd.from : bankInterval.to; + // robberyStart = robberyStart ? robberyStart.to : bankInterval.from; + // robberyEnd = robberyEnd ? robberyEnd.from : bankInterval.to; + robberyStart = robberyStart ? robberyStart : bankInterval.from; + robberyEnd = robberyEnd ? robberyEnd : bankInterval.to; return [robberyStart, robberyEnd]; } From 1daf182d4a95f92fdaaff02c0159df649cb1e62d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 18:20:28 +0500 Subject: [PATCH 09/23] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=86?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index 88d6fb2..f4a2641 100644 --- a/robbery.js +++ b/robbery.js @@ -270,7 +270,7 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { function findPossibleRobberyInterval(gangIntervals, bankInterval) { var robberyStart = gangIntervals.filter(function (interval) { - return interval.from < bankInterval.from && bankInterval.from < interval.to; + return interval.from < bankInterval.from && bankInterval.from <= interval.to; }) .map(function (intervals) { return intervals.to; @@ -280,7 +280,7 @@ function findPossibleRobberyInterval(gangIntervals, bankInterval) { })[0]; var robberyEnd = gangIntervals.filter(function (interval) { - return interval.from < bankInterval.to && bankInterval.to < interval.to; + return interval.from <= bankInterval.to && bankInterval.to < interval.to; }) .map(function (intervals) { return intervals.from; From 102e8d52b5d5e442d3d431193c886ebf66719d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 18:24:44 +0500 Subject: [PATCH 10/23] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=86?= =?UTF-8?q?=D1=8B=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index f4a2641..ef5f881 100644 --- a/robbery.js +++ b/robbery.js @@ -216,13 +216,13 @@ function correctTime(time) { function findNewStart(gangIntervals, start) { return gangIntervals.filter(function (interval) { - return interval.from <= start && start <= interval.to; + return interval.from < start && start <= interval.to; }); } function findBadStart(gangIntervals, start, end) { return gangIntervals.filter(function (interval) { - return start <= interval.from && interval.to <= end; + return start <= interval.from && interval.to < end; }); } From d0338132b09910db1553d330c0c0223402cdbd3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 18:28:43 +0500 Subject: [PATCH 11/23] =?UTF-8?q?=D0=9E=D1=82=D0=BA=D0=B0=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index ef5f881..f4a2641 100644 --- a/robbery.js +++ b/robbery.js @@ -216,13 +216,13 @@ function correctTime(time) { function findNewStart(gangIntervals, start) { return gangIntervals.filter(function (interval) { - return interval.from < start && start <= interval.to; + return interval.from <= start && start <= interval.to; }); } function findBadStart(gangIntervals, start, end) { return gangIntervals.filter(function (interval) { - return start <= interval.from && interval.to < end; + return start <= interval.from && interval.to <= end; }); } From 5a497accb3b3d355c68b0a9ae06d31ed0809b018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 18:58:59 +0500 Subject: [PATCH 12/23] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=20=D1=84=D0=BE=D1=80=D0=BC=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D0=B2=D0=B0=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/robbery.js b/robbery.js index f4a2641..7ca39a2 100644 --- a/robbery.js +++ b/robbery.js @@ -16,7 +16,8 @@ exports.isStar = true; */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - if (!workingHours || Object.keys(workingHours).length === 0) { + if (!workingHours || Object.keys(workingHours).length === 0 || + workingHours.from === workingHours.to) { return { exists: function () { @@ -185,19 +186,22 @@ function pushGangIntervals(intervals, someonesShedule, bankUTC) { } function pushGangInterval(intervals, lineOfShedule, bankUTC) { - var lineFrom = getDateFromString(lineOfShedule.from); - var lineTo = getDateFromString(lineOfShedule.to); + if (lineOfShedule.from !== lineOfShedule.to) { - var timeFromInMinutes = getTimeFromDate(lineFrom); - var timeToInMinutes = getTimeFromDate(lineTo); + var lineFrom = getDateFromString(lineOfShedule.from); + var lineTo = getDateFromString(lineOfShedule.to); - var timeFromWithUTC = addUTCToTime(timeFromInMinutes, lineFrom.time.utc, bankUTC); - var timeToWithUTC = addUTCToTime(timeToInMinutes, lineTo.time.utc, bankUTC); + var timeFromInMinutes = getTimeFromDate(lineFrom); + var timeToInMinutes = getTimeFromDate(lineTo); - intervals.push({ - from: correctTime(timeFromWithUTC), - to: correctTime(timeToWithUTC) - }); + var timeFromWithUTC = addUTCToTime(timeFromInMinutes, lineFrom.time.utc, bankUTC); + var timeToWithUTC = addUTCToTime(timeToInMinutes, lineTo.time.utc, bankUTC); + + intervals.push({ + from: correctTime(timeFromWithUTC), + to: correctTime(timeToWithUTC) + }); + } } function addUTCToTime(time, utc, bankUTC) { From c29ceb195bc04be9b8805b84fc95fd64a5f88078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 19:29:42 +0500 Subject: [PATCH 13/23] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=86?= =?UTF-8?q?=D1=8B=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/robbery.js b/robbery.js index 7ca39a2..6a6afae 100644 --- a/robbery.js +++ b/robbery.js @@ -226,7 +226,7 @@ function findNewStart(gangIntervals, start) { function findBadStart(gangIntervals, start, end) { return gangIntervals.filter(function (interval) { - return start <= interval.from && interval.to <= end; + return start < interval.from && interval.to <= end; }); } @@ -274,23 +274,23 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { function findPossibleRobberyInterval(gangIntervals, bankInterval) { var robberyStart = gangIntervals.filter(function (interval) { - return interval.from < bankInterval.from && bankInterval.from <= interval.to; + return interval.from <= bankInterval.from && bankInterval.from < interval.to; }) .map(function (intervals) { return intervals.to; }) .sort(function (a, b) { - return a > b; + return a < b; })[0]; var robberyEnd = gangIntervals.filter(function (interval) { - return interval.from <= bankInterval.to && bankInterval.to < interval.to; + return interval.from < bankInterval.to && bankInterval.to <= interval.to; }) .map(function (intervals) { return intervals.from; }) .sort(function (a, b) { - return a < b; + return a > b; })[0]; // robberyStart = robberyStart ? robberyStart.to : bankInterval.from; From d63e3e0d2ac602034106feecd8ae3f2b089dd4c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 20:26:12 +0500 Subject: [PATCH 14/23] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=20=D1=84=D0=BE=D1=80=D0=BC=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D0=B2=D0=B0=D0=BB=D0=BE=D0=B2=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 68 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/robbery.js b/robbery.js index 6a6afae..25a1b3b 100644 --- a/robbery.js +++ b/robbery.js @@ -262,9 +262,9 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { for (var i = 0; i < bankIntervals.length; i++) { var bankInterval = bankIntervals[i]; - var robberyInterval = findPossibleRobberyInterval(gangIntervals, bankInterval); - var robberyStart = robberyInterval[0]; - var robberyEnd = robberyInterval[1]; + // var robberyInterval = findPossibleRobberyInterval(gangIntervals, bankInterval); + var robberyStart = findRobberyStart(gangIntervals, bankInterval); + var robberyEnd = findRobberyEnd(gangIntervals, bankInterval); exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd); } @@ -272,8 +272,10 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { return robberyIntervals; } -function findPossibleRobberyInterval(gangIntervals, bankInterval) { - var robberyStart = gangIntervals.filter(function (interval) { +function findRobberyStart(gangIntervals, bankInterval) { + var robberyStart = bankInterval.from; + + var newRobberyStart = gangIntervals.filter(function (interval) { return interval.from <= bankInterval.from && bankInterval.from < interval.to; }) .map(function (intervals) { @@ -283,7 +285,21 @@ function findPossibleRobberyInterval(gangIntervals, bankInterval) { return a < b; })[0]; - var robberyEnd = gangIntervals.filter(function (interval) { + if (newRobberyStart && newRobberyStart !== robberyStart && newRobberyStart < bankInterval.to) { + var newInterval = { + from: newRobberyStart, + to: bankInterval.to + }; + robberyStart = findRobberyStart(gangIntervals, newInterval); + } + + return robberyStart; +} + +function findRobberyEnd(gangIntervals, bankInterval) { + var robberyEnd = bankInterval.to; + + var newRobberyEnd = gangIntervals.filter(function (interval) { return interval.from < bankInterval.to && bankInterval.to <= interval.to; }) .map(function (intervals) { @@ -293,14 +309,44 @@ function findPossibleRobberyInterval(gangIntervals, bankInterval) { return a > b; })[0]; - // robberyStart = robberyStart ? robberyStart.to : bankInterval.from; - // robberyEnd = robberyEnd ? robberyEnd.from : bankInterval.to; - robberyStart = robberyStart ? robberyStart : bankInterval.from; - robberyEnd = robberyEnd ? robberyEnd : bankInterval.to; + if (newRobberyEnd && newRobberyEnd !== robberyEnd && newRobberyEnd >= bankInterval.from) { + var newInterval = { + from: bankInterval.from, + to: newRobberyEnd + }; + robberyEnd = findRobberyEnd(gangIntervals, newInterval); + } - return [robberyStart, robberyEnd]; + return robberyEnd; } +// function findPossibleRobberyInterval(gangIntervals, bankInterval) { + // var robberyStart = gangIntervals.filter(function (interval) { + // return interval.from <= bankInterval.from && bankInterval.from < interval.to; + // }) + // .map(function (intervals) { + // return intervals.to; + // }) + // .sort(function (a, b) { + // return a < b; + // })[0]; + + // var robberyEnd = gangIntervals.filter(function (interval) { + // return interval.from < bankInterval.to && bankInterval.to <= interval.to; + // }) + // .map(function (intervals) { + // return intervals.from; + // }) + // .sort(function (a, b) { + // return a > b; + // })[0]; + + // robberyStart = robberyStart ? robberyStart : bankInterval.from; + // robberyEnd = robberyEnd ? robberyEnd : bankInterval.to; + + // return [robberyStart, robberyEnd]; +// } + function getGoodTimesForRobbery(robberyIntervals, duration) { var goodTimes = []; robberyIntervals.forEach(function (time) { From 8b5739fbd8c4e1e4d3825e39af5f8efe47e00309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 20:53:42 +0500 Subject: [PATCH 15/23] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=20=D1=84=D0=BE=D1=80=D0=BC=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D0=B2=D0=B0=D0=BB=D0=BE=D0=B2=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index 25a1b3b..755237c 100644 --- a/robbery.js +++ b/robbery.js @@ -285,7 +285,7 @@ function findRobberyStart(gangIntervals, bankInterval) { return a < b; })[0]; - if (newRobberyStart && newRobberyStart !== robberyStart && newRobberyStart < bankInterval.to) { + if (newRobberyStart && newRobberyStart !== robberyStart) { var newInterval = { from: newRobberyStart, to: bankInterval.to @@ -309,7 +309,7 @@ function findRobberyEnd(gangIntervals, bankInterval) { return a > b; })[0]; - if (newRobberyEnd && newRobberyEnd !== robberyEnd && newRobberyEnd >= bankInterval.from) { + if (newRobberyEnd && newRobberyEnd !== robberyEnd) { var newInterval = { from: bankInterval.from, to: newRobberyEnd From 6024a9840c1b626986ba1daa04d24ba2f4921614 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 21:03:10 +0500 Subject: [PATCH 16/23] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=86?= =?UTF-8?q?=D1=8B=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/robbery.js b/robbery.js index 755237c..78ecca1 100644 --- a/robbery.js +++ b/robbery.js @@ -265,6 +265,9 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { // var robberyInterval = findPossibleRobberyInterval(gangIntervals, bankInterval); var robberyStart = findRobberyStart(gangIntervals, bankInterval); var robberyEnd = findRobberyEnd(gangIntervals, bankInterval); + if (robberyStart >= bankInterval.to || robberyEnd <= robberyStart) { + continue; + } exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd); } From 595dff2f8eeef7ebf23fd81e00a70e5cecdb752f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 22:07:59 +0500 Subject: [PATCH 17/23] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=20=D1=84=D0=BE=D1=80=D0=BC=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=BD=D1=82=D0=B5?= =?UTF-8?q?=D1=80=D0=B2=D0=B0=D0=BB=D0=BE=D0=B2=206?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 154 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 119 insertions(+), 35 deletions(-) diff --git a/robbery.js b/robbery.js index 78ecca1..035029e 100644 --- a/robbery.js +++ b/robbery.js @@ -218,43 +218,117 @@ function correctTime(time) { return time; } -function findNewStart(gangIntervals, start) { - return gangIntervals.filter(function (interval) { - return interval.from <= start && start <= interval.to; +// function findNewStart(gangIntervals, start) { + // return gangIntervals.filter(function (interval) { + // return interval.from <= start && start <= interval.to; + // }); +// } + +// function findBadStart(gangIntervals, start, end) { + // return gangIntervals.filter(function (interval) { + // return start < interval.from && interval.to <= end; + // }); +// } + +// function exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd) { + // while (robberyStart <= robberyEnd) { + // var badStart = findBadStart(gangIntervals, robberyStart, robberyEnd)[0]; + + // var from = badStart ? badStart.from : robberyEnd; + + // robberyIntervals.push({ + // from: robberyStart, + // to: from + // }); + + // if (from === robberyEnd) { + // break; + // } + + // robberyStart = badStart ? badStart.to : robberyEnd; + + // robberyStart = findNewStart(gangIntervals, robberyStart) + // .map(function (elem) { + // return elem.to; + // }) + // .sort(function (a, b) { + // return b > a; + // })[0]; + // } +// } + +function exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd) { + if (robberyStart === robberyEnd) { + return undefined; + } + + var possibleRobberyEnd = gangIntervals.filter(function (interval) { + return robberyStart < interval.from && interval.to <= robberyEnd; + }) + .map(function (interval) { + return interval.from; + }) + .sort(function (a, b) { + return a > b; + })[0]; + + var trueRobberyEnd = possibleRobberyEnd ? possibleRobberyEnd : robberyEnd; + + robberyIntervals.push({ + from: robberyStart, + to: trueRobberyEnd }); -} -function findBadStart(gangIntervals, start, end) { - return gangIntervals.filter(function (interval) { - return start < interval.from && interval.to <= end; + if (trueRobberyEnd === robberyEnd) { + return undefined; + } + + var trueRobberyStart = gangIntervals.filter(function (interval) { + return robberyStart < interval.from && interval.to <= robberyEnd; + }) + .map(function (interval) { + return interval.to; + }) + .sort(function (a, b) { + return a < b; + })[0]; + + robberyIntervals.push({ + from: trueRobberyStart, + to: robberyEnd }); -} -function exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd) { - while (robberyStart <= robberyEnd) { - var badStart = findBadStart(gangIntervals, robberyStart, robberyEnd)[0]; + var newInterval = { + from: trueRobberyEnd, + to: trueRobberyStart + }; - var from = badStart ? badStart.from : robberyEnd; + findPossibleRobberyInterval(gangIntervals, robberyIntervals, newInterval); - robberyIntervals.push({ - from: robberyStart, - to: from - }); + // while (robberyStart <= robberyEnd) { + // var badStart = findBadStart(gangIntervals, robberyStart, robberyEnd)[0]; - if (from === robberyEnd) { - break; - } + // var from = badStart ? badStart.from : robberyEnd; - robberyStart = badStart ? badStart.to : robberyEnd; + // robberyIntervals.push({ + // from: robberyStart, + // to: from + // }); - robberyStart = findNewStart(gangIntervals, robberyStart) - .map(function (elem) { - return elem.to; - }) - .sort(function (a, b) { - return b > a; - })[0]; - } + // if (from === robberyEnd) { + // break; + // } + + // robberyStart = badStart ? badStart.to : robberyEnd; + + // robberyStart = findNewStart(gangIntervals, robberyStart) + // .map(function (elem) { + // return elem.to; + // }) + // .sort(function (a, b) { + // return b > a; + // })[0]; + // } } function findRobberyIntervals(bankIntervals, gangIntervals) { @@ -263,18 +337,28 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { for (var i = 0; i < bankIntervals.length; i++) { var bankInterval = bankIntervals[i]; // var robberyInterval = findPossibleRobberyInterval(gangIntervals, bankInterval); - var robberyStart = findRobberyStart(gangIntervals, bankInterval); - var robberyEnd = findRobberyEnd(gangIntervals, bankInterval); - if (robberyStart >= bankInterval.to || robberyEnd <= robberyStart) { - continue; - } - - exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd); + // var robberyStart = findRobberyStart(gangIntervals, bankInterval); + // var robberyEnd = findRobberyEnd(gangIntervals, bankInterval); + // if (robberyStart >= bankInterval.to || robberyEnd <= robberyStart) { + // continue; + // } + + // exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd); + findPossibleRobberyInterval(gangIntervals, robberyIntervals, bankInterval); } return robberyIntervals; } +function findPossibleRobberyInterval(gangIntervals, robberyIntervals, bankInterval) { + var robberyStart = findRobberyStart(gangIntervals, bankInterval); + var robberyEnd = findRobberyEnd(gangIntervals, bankInterval); + // if (robberyStart >= bankInterval.to || robberyEnd <= robberyStart) { + // continue; + // } + exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd); +} + function findRobberyStart(gangIntervals, bankInterval) { var robberyStart = bankInterval.from; From fd21c5a271d74e3a871769fb2f1f12498189b96f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 22:19:16 +0500 Subject: [PATCH 18/23] =?UTF-8?q?=D0=9E=D1=82=D0=BA=D0=B0=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 154 ++++++++++++----------------------------------------- 1 file changed, 35 insertions(+), 119 deletions(-) diff --git a/robbery.js b/robbery.js index 035029e..78ecca1 100644 --- a/robbery.js +++ b/robbery.js @@ -218,117 +218,43 @@ function correctTime(time) { return time; } -// function findNewStart(gangIntervals, start) { - // return gangIntervals.filter(function (interval) { - // return interval.from <= start && start <= interval.to; - // }); -// } - -// function findBadStart(gangIntervals, start, end) { - // return gangIntervals.filter(function (interval) { - // return start < interval.from && interval.to <= end; - // }); -// } - -// function exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd) { - // while (robberyStart <= robberyEnd) { - // var badStart = findBadStart(gangIntervals, robberyStart, robberyEnd)[0]; - - // var from = badStart ? badStart.from : robberyEnd; - - // robberyIntervals.push({ - // from: robberyStart, - // to: from - // }); - - // if (from === robberyEnd) { - // break; - // } - - // robberyStart = badStart ? badStart.to : robberyEnd; - - // robberyStart = findNewStart(gangIntervals, robberyStart) - // .map(function (elem) { - // return elem.to; - // }) - // .sort(function (a, b) { - // return b > a; - // })[0]; - // } -// } - -function exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd) { - if (robberyStart === robberyEnd) { - return undefined; - } - - var possibleRobberyEnd = gangIntervals.filter(function (interval) { - return robberyStart < interval.from && interval.to <= robberyEnd; - }) - .map(function (interval) { - return interval.from; - }) - .sort(function (a, b) { - return a > b; - })[0]; - - var trueRobberyEnd = possibleRobberyEnd ? possibleRobberyEnd : robberyEnd; - - robberyIntervals.push({ - from: robberyStart, - to: trueRobberyEnd +function findNewStart(gangIntervals, start) { + return gangIntervals.filter(function (interval) { + return interval.from <= start && start <= interval.to; }); +} - if (trueRobberyEnd === robberyEnd) { - return undefined; - } - - var trueRobberyStart = gangIntervals.filter(function (interval) { - return robberyStart < interval.from && interval.to <= robberyEnd; - }) - .map(function (interval) { - return interval.to; - }) - .sort(function (a, b) { - return a < b; - })[0]; - - robberyIntervals.push({ - from: trueRobberyStart, - to: robberyEnd +function findBadStart(gangIntervals, start, end) { + return gangIntervals.filter(function (interval) { + return start < interval.from && interval.to <= end; }); +} - var newInterval = { - from: trueRobberyEnd, - to: trueRobberyStart - }; - - findPossibleRobberyInterval(gangIntervals, robberyIntervals, newInterval); - - // while (robberyStart <= robberyEnd) { - // var badStart = findBadStart(gangIntervals, robberyStart, robberyEnd)[0]; +function exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd) { + while (robberyStart <= robberyEnd) { + var badStart = findBadStart(gangIntervals, robberyStart, robberyEnd)[0]; - // var from = badStart ? badStart.from : robberyEnd; + var from = badStart ? badStart.from : robberyEnd; - // robberyIntervals.push({ - // from: robberyStart, - // to: from - // }); + robberyIntervals.push({ + from: robberyStart, + to: from + }); - // if (from === robberyEnd) { - // break; - // } + if (from === robberyEnd) { + break; + } - // robberyStart = badStart ? badStart.to : robberyEnd; + robberyStart = badStart ? badStart.to : robberyEnd; - // robberyStart = findNewStart(gangIntervals, robberyStart) - // .map(function (elem) { - // return elem.to; - // }) - // .sort(function (a, b) { - // return b > a; - // })[0]; - // } + robberyStart = findNewStart(gangIntervals, robberyStart) + .map(function (elem) { + return elem.to; + }) + .sort(function (a, b) { + return b > a; + })[0]; + } } function findRobberyIntervals(bankIntervals, gangIntervals) { @@ -337,28 +263,18 @@ function findRobberyIntervals(bankIntervals, gangIntervals) { for (var i = 0; i < bankIntervals.length; i++) { var bankInterval = bankIntervals[i]; // var robberyInterval = findPossibleRobberyInterval(gangIntervals, bankInterval); - // var robberyStart = findRobberyStart(gangIntervals, bankInterval); - // var robberyEnd = findRobberyEnd(gangIntervals, bankInterval); - // if (robberyStart >= bankInterval.to || robberyEnd <= robberyStart) { - // continue; - // } - - // exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd); - findPossibleRobberyInterval(gangIntervals, robberyIntervals, bankInterval); + var robberyStart = findRobberyStart(gangIntervals, bankInterval); + var robberyEnd = findRobberyEnd(gangIntervals, bankInterval); + if (robberyStart >= bankInterval.to || robberyEnd <= robberyStart) { + continue; + } + + exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd); } return robberyIntervals; } -function findPossibleRobberyInterval(gangIntervals, robberyIntervals, bankInterval) { - var robberyStart = findRobberyStart(gangIntervals, bankInterval); - var robberyEnd = findRobberyEnd(gangIntervals, bankInterval); - // if (robberyStart >= bankInterval.to || robberyEnd <= robberyStart) { - // continue; - // } - exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyEnd); -} - function findRobberyStart(gangIntervals, bankInterval) { var robberyStart = bankInterval.from; From 34a7f2725e9f0d2fe2e40ae87c78cbd25cfff2a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Wed, 26 Oct 2016 22:47:26 +0500 Subject: [PATCH 19/23] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=D0=B0=20?= =?UTF-8?q?=D0=BB=D0=B8=D1=88=D0=BD=D1=83=D1=8E=20=D0=BA=D0=BE=D1=80=D1=80?= =?UTF-8?q?=D0=B5=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/robbery.js b/robbery.js index 78ecca1..6855510 100644 --- a/robbery.js +++ b/robbery.js @@ -198,8 +198,10 @@ function pushGangInterval(intervals, lineOfShedule, bankUTC) { var timeToWithUTC = addUTCToTime(timeToInMinutes, lineTo.time.utc, bankUTC); intervals.push({ - from: correctTime(timeFromWithUTC), - to: correctTime(timeToWithUTC) + // from: correctTime(timeFromWithUTC), + // to: correctTime(timeToWithUTC) + from: timeFromWithUTC, + to: timeToWithUTC }); } } @@ -208,15 +210,15 @@ function addUTCToTime(time, utc, bankUTC) { return time - 60 * (utc - bankUTC); } -function correctTime(time) { - if (time < 0) { - time = 0; - } else if (time > 3 * 24 * 60) { - time = 3 * 24 * 60; - } +// function correctTime(time) { + // if (time < 0) { + // time = 0; + // } else if (time > 3 * 24 * 60) { + // time = 3 * 24 * 60; + // } - return time; -} + // return time; +// } function findNewStart(gangIntervals, start) { return gangIntervals.filter(function (interval) { From 5068dbdd6213bb9dc5344d92b4be2c44ce148da0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Mon, 31 Oct 2016 21:11:46 +0500 Subject: [PATCH 20/23] =?UTF-8?q?=D0=9D=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D0=BD=D0=B3=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 182 ++++++++++++++++++++++++++--------------------------- 1 file changed, 91 insertions(+), 91 deletions(-) diff --git a/robbery.js b/robbery.js index 6855510..024492a 100644 --- a/robbery.js +++ b/robbery.js @@ -6,6 +6,10 @@ */ exports.isStar = true; +var MINUTES_IN_HOUR = 60; +var HOURS_IN_DAY = 24; +var MINUTES_IN_DAY = MINUTES_IN_HOUR * HOURS_IN_DAY; + /** * @param {Object} schedule – Расписание Банды * @param {Number} duration - Время на ограбление в минутах @@ -16,28 +20,22 @@ exports.isStar = true; */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - if (!workingHours || Object.keys(workingHours).length === 0 || - workingHours.from === workingHours.to) { + if (!isWorkingHours(workingHours) || (!duration)) { return { - exists: function () { return false; }, - - format: function (template) { - template = ''; - - return template; + format: function () { + return ''; }, - tryLater: function () { return false; } }; } + var bankUTC = getBankUTC(workingHours.from); var bankIntervals = getBankIntervals(workingHours); - var bankUTC = parseInt(workingHours.from.split('+')[1]); var gangIntervals = getGangIntervals(schedule, bankUTC); var robberyIntervals = findRobberyIntervals(bankIntervals, gangIntervals); @@ -92,71 +90,80 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { }; }; -function getBankIntervals(workingHours) { - var bankIntervals = []; +function isWorkingHours(hours) { + return (hours) && (Object.keys(hours).length !== 0); +} - var bankFrom = getDateFromString(workingHours.from); - var bankTo = getDateFromString(workingHours.to); +function getBankUTC(dateAsString) { + return parseInt(dateAsString.split('+')[1]); +} - bankIntervals.push({ - from: getTimeFromDate(bankFrom, 'ПН'), - to: getTimeFromDate(bankTo, 'ПН') - }); +function getBankIntervals(workingHours) { + var bankIntervals = []; - bankIntervals.push({ - from: getTimeFromDate(bankFrom, 'ВТ'), - to: getTimeFromDate(bankTo, 'ВТ') - }); + var bankStart = transformStringToDate(workingHours.from); + var bankEnd = transformStringToDate(workingHours.to); - bankIntervals.push({ - from: getTimeFromDate(bankFrom, 'СР'), - to: getTimeFromDate(bankTo, 'СР') - }); + bankIntervals.push( + { + from: convertDateToMinutes(bankStart, 'ПН'), + to: convertDateToMinutes(bankEnd, 'ПН') + }, + { + from: convertDateToMinutes(bankStart, 'ВТ'), + to: convertDateToMinutes(bankEnd, 'ВТ') + }, + { + from: convertDateToMinutes(bankStart, 'СР'), + to: convertDateToMinutes(bankEnd, 'СР') + } + ); return bankIntervals; } -function getDateFromString(string) { - var components = string.split(' '); - var date = { - day: (components.length > 1) ? components[0] : '', - time: (components.length > 1) ? components[1] : components[0] - }; +function transformStringToDate(dateAsString) { + var dateComponents = dateAsString.split(/\s|:|\+/); - components = date.time.split(/\D/); - date.time = { - hours: parseInt(components[0]), - minutes: parseInt(components[1]), - utc: parseInt(components[2]) - }; + if (dateComponents.length === 3) { + return { + day: '', + hour: parseInt(dateComponents[0]), + minute: parseInt(dateComponents[1]), + utc: parseInt(dateComponents[2]) + }; + } - return date; + return { + day: dateComponents[0], + hour: parseInt(dateComponents[1]), + minute: parseInt(dateComponents[2]), + utc: parseInt(dateComponents[3]) + }; } -function getTimeFromDate(date, day) { - var minutes = convertTimeInMinutes(date.time); - var minutesWithDay; - if (day === undefined) { - minutesWithDay = addDayToTime(minutes, date.day); - } else { - minutesWithDay = addDayToTime(minutes, day); +function convertDateToMinutes(date, day) { + if (day) { + date.day = day; } - return minutesWithDay; + return getMinutes(date); } -function convertTimeInMinutes(time) { - return 60 * time.hours + time.minutes; -} +function getMinutes(date) { + var minutes = date.minute + date.hour * MINUTES_IN_HOUR; -function addDayToTime(time, day) { - if (day === 'ВТ') { - time += 24 * 60; - } else if (day === 'СР') { - time += 2 * 24 * 60; + if (date.day === 'ПН') { + minutes += 0; + } else if (date.day === 'ВТ') { + minutes += MINUTES_IN_DAY; + } else if (date.day === 'СР') { + minutes += 2 * MINUTES_IN_DAY; + } else { + minutes += 3 * MINUTES_IN_DAY; } - return time; + return minutes; } function getGangIntervals(shedule, bankUTC) { @@ -167,9 +174,10 @@ function getGangIntervals(shedule, bankUTC) { } var bandits = Object.keys(shedule); + for (var i = 0; i < bandits.length; i++) { var bandit = bandits[i]; - pushGangIntervals(gangIntervals, shedule[bandit], bankUTC); + gangIntervals = gangIntervals.concat(getBanditShedule(shedule[bandit], bankUTC)); } gangIntervals.sort(function (interval1, interval2) { @@ -179,47 +187,35 @@ function getGangIntervals(shedule, bankUTC) { return gangIntervals; } -function pushGangIntervals(intervals, someonesShedule, bankUTC) { - for (var i = 0; i < someonesShedule.length; i++) { - pushGangInterval(intervals, someonesShedule[i], bankUTC); +function getBanditShedule(banditShedule, bankUTC) { + var banditIntervals = []; + for (var i = 0; i < banditShedule.length; i++) { + banditIntervals.push(getBanditInterval(banditShedule[i], bankUTC)); } -} -function pushGangInterval(intervals, lineOfShedule, bankUTC) { - if (lineOfShedule.from !== lineOfShedule.to) { + return banditIntervals; +} - var lineFrom = getDateFromString(lineOfShedule.from); - var lineTo = getDateFromString(lineOfShedule.to); +function getBanditInterval(elementOfShedule, bankUTC) { + var dateStart = transformStringToDate(elementOfShedule.from); + var dateEnd = transformStringToDate(elementOfShedule.to); - var timeFromInMinutes = getTimeFromDate(lineFrom); - var timeToInMinutes = getTimeFromDate(lineTo); + var minutesStart = convertDateToMinutes(dateStart); + var minutesEnd = convertDateToMinutes(dateEnd); - var timeFromWithUTC = addUTCToTime(timeFromInMinutes, lineFrom.time.utc, bankUTC); - var timeToWithUTC = addUTCToTime(timeToInMinutes, lineTo.time.utc, bankUTC); + var minutesStartWithUTC = addUTCToMinutes(minutesStart, dateStart.utc, bankUTC); + var minutesEndWithUTC = addUTCToMinutes(minutesEnd, dateEnd.utc, bankUTC); - intervals.push({ - // from: correctTime(timeFromWithUTC), - // to: correctTime(timeToWithUTC) - from: timeFromWithUTC, - to: timeToWithUTC - }); - } + return { + from: minutesStartWithUTC, + to: minutesEndWithUTC + }; } -function addUTCToTime(time, utc, bankUTC) { - return time - 60 * (utc - bankUTC); +function addUTCToMinutes(minutes, banditUTC, bankUTC) { + return minutes - (banditUTC - bankUTC) * MINUTES_IN_HOUR; } -// function correctTime(time) { - // if (time < 0) { - // time = 0; - // } else if (time > 3 * 24 * 60) { - // time = 3 * 24 * 60; - // } - - // return time; -// } - function findNewStart(gangIntervals, start) { return gangIntervals.filter(function (interval) { return interval.from <= start && start <= interval.to; @@ -236,7 +232,7 @@ function exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyE while (robberyStart <= robberyEnd) { var badStart = findBadStart(gangIntervals, robberyStart, robberyEnd)[0]; - var from = badStart ? badStart.from : robberyEnd; + var from = badStart !== undefined ? badStart.from : robberyEnd; robberyIntervals.push({ from: robberyStart, @@ -260,6 +256,10 @@ function exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyE } function findRobberyIntervals(bankIntervals, gangIntervals) { + if (gangIntervals.length === 0) { + return bankIntervals; + } + var robberyIntervals = []; for (var i = 0; i < bankIntervals.length; i++) { @@ -290,7 +290,7 @@ function findRobberyStart(gangIntervals, bankInterval) { return a < b; })[0]; - if (newRobberyStart && newRobberyStart !== robberyStart) { + if (newRobberyStart !== undefined && newRobberyStart !== robberyStart) { var newInterval = { from: newRobberyStart, to: bankInterval.to @@ -314,7 +314,7 @@ function findRobberyEnd(gangIntervals, bankInterval) { return a > b; })[0]; - if (newRobberyEnd && newRobberyEnd !== robberyEnd) { + if (newRobberyEnd !== undefined && newRobberyEnd !== robberyEnd) { var newInterval = { from: bankInterval.from, to: newRobberyEnd From 138ebc3a103340aea26698410b763201a8940a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Mon, 31 Oct 2016 21:19:39 +0500 Subject: [PATCH 21/23] =?UTF-8?q?=D0=95=D1=89=D1=91=20=D1=80=D0=B5=D1=84?= =?UTF-8?q?=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/robbery.js b/robbery.js index 024492a..6b522ae 100644 --- a/robbery.js +++ b/robbery.js @@ -40,6 +40,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var robberyIntervals = findRobberyIntervals(bankIntervals, gangIntervals); var goodTimes = getGoodTimesForRobbery(robberyIntervals, duration); + var isExistsTime = goodTimes.length !== 0; var robberyTime = goodTimes.shift(); @@ -61,14 +62,16 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - if (!isExistsTime) { + if (!isExistsTime || !template) { return ''; } - var day = ['ПН', 'ВТ', 'СР'][Math.floor(robberyTime / (24 * 60))]; - var hour = Math.floor((robberyTime % (24 * 60)) / 60); + var day = ['ПН', 'ВТ', 'СР'][Math.floor(robberyTime / MINUTES_IN_DAY)]; + + var hour = Math.floor((robberyTime % MINUTES_IN_DAY) / MINUTES_IN_HOUR); hour = (hour < 10 ? '0' : '') + hour; - var minute = (robberyTime % (24 * 60)) % 60; + + var minute = (robberyTime % MINUTES_IN_DAY) % MINUTES_IN_HOUR; minute = (minute < 10 ? '0' : '') + minute; return template.replace('%DD', day) @@ -354,10 +357,12 @@ function findRobberyEnd(gangIntervals, bankInterval) { function getGoodTimesForRobbery(robberyIntervals, duration) { var goodTimes = []; + var stepTime = 30; + robberyIntervals.forEach(function (time) { while (time.from + duration <= time.to) { goodTimes.push(time.from); - time.from += 30; + time.from += stepTime; } }); From e6de2b2c5d360d18d7018a347ab9a302bfcccb0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Mon, 31 Oct 2016 22:33:52 +0500 Subject: [PATCH 22/23] =?UTF-8?q?=D0=9A=D0=BE=D1=80=D1=80=D0=B5=D0=BA?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D1=83=D1=81=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=D0=B8=D0=B9=20=D0=B2=D1=85=D0=BE=D0=B6=D0=B4=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index 6b522ae..4c31d85 100644 --- a/robbery.js +++ b/robbery.js @@ -227,7 +227,7 @@ function findNewStart(gangIntervals, start) { function findBadStart(gangIntervals, start, end) { return gangIntervals.filter(function (interval) { - return start < interval.from && interval.to <= end; + return start < interval.from && interval.to < end; }); } @@ -246,7 +246,7 @@ function exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyE break; } - robberyStart = badStart ? badStart.to : robberyEnd; + robberyStart = badStart !== undefined ? badStart.to : robberyEnd; robberyStart = findNewStart(gangIntervals, robberyStart) .map(function (elem) { From 94ef452641130dd2cd2d73c0b57f8697acfc6d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B5=D0=BB=D0=BE=D0=B2=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=D1=8F=20=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0?= Date: Mon, 31 Oct 2016 22:49:29 +0500 Subject: [PATCH 23/23] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=20=D0=BD?= =?UTF-8?q?=D0=B0=D1=87=D0=B0=D0=BB=D0=B0=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D0=B0=D0=BD?= =?UTF-8?q?=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/robbery.js b/robbery.js index 4c31d85..f074e3c 100644 --- a/robbery.js +++ b/robbery.js @@ -218,12 +218,11 @@ function getBanditInterval(elementOfShedule, bankUTC) { function addUTCToMinutes(minutes, banditUTC, bankUTC) { return minutes - (banditUTC - bankUTC) * MINUTES_IN_HOUR; } - -function findNewStart(gangIntervals, start) { - return gangIntervals.filter(function (interval) { - return interval.from <= start && start <= interval.to; - }); -} +// function findNewStart(gangIntervals, start) { + // return gangIntervals.filter(function (interval) { + // return interval.from <= start && start <= interval.to; + // }); +// } function findBadStart(gangIntervals, start, end) { return gangIntervals.filter(function (interval) { @@ -248,13 +247,14 @@ function exploreInterval(gangIntervals, robberyIntervals, robberyStart, robberyE robberyStart = badStart !== undefined ? badStart.to : robberyEnd; - robberyStart = findNewStart(gangIntervals, robberyStart) - .map(function (elem) { - return elem.to; - }) - .sort(function (a, b) { - return b > a; - })[0]; + // robberyStart = findNewStart(gangIntervals, robberyStart) + // .map(function (elem) { + // return elem.to; + // }) + // .sort(function (a, b) { + // return b > a; + // })[0]; + robberyStart = findRobberyStart(gangIntervals, { from: robberyStart, to: robberyEnd }); } }