From 908e22b0d8453d59fff4f9f8ba9df1af89b5c9d9 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Fri, 21 Oct 2016 21:14:02 +0500 Subject: [PATCH 01/19] first try --- robbery.js | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 138 insertions(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 4a8309d..aba1b17 100644 --- a/robbery.js +++ b/robbery.js @@ -1,5 +1,100 @@ 'use strict'; +var DAYS = ['ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС']; +var BANK_DAYS = 3; +var MINUTE = 1; +var HOUR = 60 * MINUTE; +var DAY = 24 * HOUR; +var DAY_START = 0; +var DAY_END = 3 * DAY - MINUTE; +var HALF_HOUR = MINUTE * 30; + + +function isCorrectSchedule(schedule) { + var keys = Object.keys(schedule); + + if (keys.length !== 3) { + return false; + } + + var isCorrect = true; + for (var i = 0; i < keys.length; ++i) { + if (schedule[key[i]].length === 0) { + isCorrect = false; + break; + } + } + + return isCorrect; +} + +function getTimezone(time) { + time = /(\d{2}):(\d{2})\+(\d+)/.exec(time); + + return parseInt(time[3], 10) * HOUR; +} + +function convertTime(time, timezone) { + time = /(\d{2}):(\d{2})\+(\d+)/.exec(time); + + return parseInt(time[1], 10) * HOUR + + parseInt(time[2], 10) * MINUTE - + parseInt(time[3], 10) * HOUR + + timezone; +} + +function convertDate(date, timezone) { + date = date.split(" "); + + return DAYS.indexOf(date[0]) * DAY + + convertTime(date[1], timezone); +} + +function convertWorkingHours(workingHours, timezone) { + var newWorkingHours = []; + var timeFrom = convertDate(workingHours['from'], timezone); + var timeTo = convertDate(workingHours['to'], timezone); + + for (var i = 0; i < BANK_DAYS; ++i) { + newWorkingHours.push([ + timeFrom + i * DAY, + timeTo + i * DAY + ]); + } + + return newWorkingHours; +} + +function convertSchedule(schedule, timezone) { + var newSchedule = []; + for (var name in schedule) { + if (schedule.hasOwnProperty(name)) { + var persSchedule = schedule[name] + .map(function (item) { + return [ + convertDate(item['from'], timezone), + convertDate(item['to'], timezone) + ]; + }) + .sort(function (i, j) { + return i[0] > j[0] ? -1 : 1; + }); + + var start = DAY_START; + newSchedule.push(persSchedule + .map(function (item) { + var time = [start, item[0] - MINUTE]; + start = item[1] + MINUTE; + return time; + }) + .push([start, DAY_END]) + ); + } + } + + return newSchedule; +} + /** * Сделано задание на звездочку * Реализовано оба метода и tryLater @@ -16,6 +111,30 @@ exports.isStar = true; */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { console.info(schedule, duration, workingHours); + var roberyTime = []; + + if (isCorrectSchedule(schedule)) { + var timezone = getTimezone(workingHours['from']); + + workingHours = convertWorkingHours(workingHours, timezone); + schedule = convertSchedule(schedule, timezone); + + workingHours.forEach(function (bankTime) { + schedule[0].forEach(function (i) { + schedule[1].forEach(function (j) { + schedule[2].forEach(function (k) { + var start = Math.min(i[0], j[0], k[0]); + var finish = Math.min(i[1], j[1], k[1]); + + while (start + duration <= finish) { + roberyTime.push([start, finish]); + start += HALF_HOUR; + } + }); + }); + }); + }); + } return { @@ -24,6 +143,10 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { + if (roberyTime.length !== 0) { + return true; + } + return false; }, @@ -35,7 +158,16 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - return template; + var time = roberyTime[0][0]; + var day = Math.floor(time / DAY); + time %= DAY; + var hour = Math.floor(time / HOUR); + time %= HOUR; + var minute = Math.floor(time / MINUTE); + + return template.replace("%DD", DAYS[day]) + .replace("%HH", hour.toLocaleString(undefined, { 'minimumIntegerDigits': 2 })) + .replace("%MM", minute.toLocaleString(undefined, { 'minimumIntegerDigits': 2 })); }, /** @@ -44,6 +176,11 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { + roberyTime.shift(); + if (roberyTime && roberyTime.length !== 0) { + return true; + } + return false; } }; From 51b07972eeec95f9fb753675dd7dd601efd07ab9 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Fri, 21 Oct 2016 22:31:58 +0500 Subject: [PATCH 02/19] fix lint --- robbery.js | 55 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/robbery.js b/robbery.js index aba1b17..b22bf02 100644 --- a/robbery.js +++ b/robbery.js @@ -19,7 +19,7 @@ function isCorrectSchedule(schedule) { var isCorrect = true; for (var i = 0; i < keys.length; ++i) { - if (schedule[key[i]].length === 0) { + if (schedule[keys[i]].length === 0) { isCorrect = false; break; } @@ -44,7 +44,7 @@ function convertTime(time, timezone) { } function convertDate(date, timezone) { - date = date.split(" "); + date = date.split(' '); return DAYS.indexOf(date[0]) * DAY + convertTime(date[1], timezone); @@ -52,8 +52,8 @@ function convertDate(date, timezone) { function convertWorkingHours(workingHours, timezone) { var newWorkingHours = []; - var timeFrom = convertDate(workingHours['from'], timezone); - var timeTo = convertDate(workingHours['to'], timezone); + var timeFrom = convertDate(workingHours.from, timezone); + var timeTo = convertDate(workingHours.to, timezone); for (var i = 0; i < BANK_DAYS; ++i) { newWorkingHours.push([ @@ -65,6 +65,13 @@ function convertWorkingHours(workingHours, timezone) { return newWorkingHours; } +function getTimeInterval(times) { + var time = [start, times[0] - MINUTE]; + start = times[1] + MINUTE; + + return time; +} + function convertSchedule(schedule, timezone) { var newSchedule = []; for (var name in schedule) { @@ -72,8 +79,8 @@ function convertSchedule(schedule, timezone) { var persSchedule = schedule[name] .map(function (item) { return [ - convertDate(item['from'], timezone), - convertDate(item['to'], timezone) + convertDate(item.from, timezone), + convertDate(item.to, timezone) ]; }) .sort(function (i, j) { @@ -82,11 +89,7 @@ function convertSchedule(schedule, timezone) { var start = DAY_START; newSchedule.push(persSchedule - .map(function (item) { - var time = [start, item[0] - MINUTE]; - start = item[1] + MINUTE; - return time; - }) + .map(getTimeInterval) .push([start, DAY_END]) ); } @@ -105,8 +108,8 @@ exports.isStar = true; * @param {Object} schedule – Расписание Банды * @param {Number} duration - Время на ограбление в минутах * @param {Object} workingHours – Время работы банка - * @param {String} workingHours.from – Время открытия, например, "10:00+5" - * @param {String} workingHours.to – Время закрытия, например, "18:00+5" + * @param {String} workingHours.from – Время открытия, например, '10:00+5' + * @param {String} workingHours.to – Время закрытия, например, '18:00+5' * @returns {Object} */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { @@ -114,17 +117,17 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var roberyTime = []; if (isCorrectSchedule(schedule)) { - var timezone = getTimezone(workingHours['from']); + var timezone = getTimezone(workingHours.from); workingHours = convertWorkingHours(workingHours, timezone); schedule = convertSchedule(schedule, timezone); - workingHours.forEach(function (bankTime) { + for(var bankTime = 0; bankTime < workingHours.length; ++bankTime) { schedule[0].forEach(function (i) { schedule[1].forEach(function (j) { schedule[2].forEach(function (k) { - var start = Math.min(i[0], j[0], k[0]); - var finish = Math.min(i[1], j[1], k[1]); + var start = Math.max(workingHours[bankTime][0], i[0], j[0], k[0]); + var finish = Math.min(workingHours[bankTime][1],i[1], j[1], k[1]); while (start + duration <= finish) { roberyTime.push([start, finish]); @@ -133,7 +136,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { }); }); }); - }); + } } return { @@ -153,7 +156,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { /** * Возвращает отформатированную строку с часами для ограбления * Например, - * "Начинаем в %HH:%MM (%DD)" -> "Начинаем в 14:59 (СР)" + * 'Начинаем в %HH:%MM (%DD)' -> 'Начинаем в 14:59 (СР)' * @param {String} template * @returns {String} */ @@ -161,13 +164,17 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var time = roberyTime[0][0]; var day = Math.floor(time / DAY); time %= DAY; - var hour = Math.floor(time / HOUR); + + var hour = Math.floor(time / HOUR) + .toLocaleString(undefined, {'minimumIntegerDigits': 2}); time %= HOUR; - var minute = Math.floor(time / MINUTE); - return template.replace("%DD", DAYS[day]) - .replace("%HH", hour.toLocaleString(undefined, { 'minimumIntegerDigits': 2 })) - .replace("%MM", minute.toLocaleString(undefined, { 'minimumIntegerDigits': 2 })); + var minute = Math.floor(time / MINUTE) + .toLocaleString(undefined, { 'minimumIntegerDigits': 2 }); + + return template.replace('%DD', DAYS[day]) + .replace('%HH', hour) + .replace('%MM', minute); }, /** From bb5e25800f7a89de8d117e16a4dc40b13b8d13e5 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Fri, 21 Oct 2016 22:44:41 +0500 Subject: [PATCH 03/19] fix lint 2 --- robbery.js | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/robbery.js b/robbery.js index b22bf02..37ede1c 100644 --- a/robbery.js +++ b/robbery.js @@ -65,11 +65,18 @@ function convertWorkingHours(workingHours, timezone) { return newWorkingHours; } -function getTimeInterval(times) { - var time = [start, times[0] - MINUTE]; - start = times[1] + MINUTE; +function getTimeInterval(persSchedule) { + var start = DAY_START; + var result = persSchedule.map(function (item) { + var time = [start, item[0] - MINUTE]; + start = item[1] + MINUTE; - return time; + return time; + }); + + result.push([start, DAY_END]) + + return result; } function convertSchedule(schedule, timezone) { @@ -87,11 +94,7 @@ function convertSchedule(schedule, timezone) { return i[0] > j[0] ? -1 : 1; }); - var start = DAY_START; - newSchedule.push(persSchedule - .map(getTimeInterval) - .push([start, DAY_END]) - ); + newSchedule.push(getTimeInterval(persSchedule)); } } @@ -122,20 +125,20 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { workingHours = convertWorkingHours(workingHours, timezone); schedule = convertSchedule(schedule, timezone); - for(var bankTime = 0; bankTime < workingHours.length; ++bankTime) { - schedule[0].forEach(function (i) { - schedule[1].forEach(function (j) { - schedule[2].forEach(function (k) { + for (var bankTime = 0; bankTime < workingHours.length; ++bankTime) { + for (var i = 0; i < schedule[0].length; ++i) { + for (var j = 0; j < schedule[1].length; ++j) { + for (var k = 0; k < schedule[2].length; ++k) { var start = Math.max(workingHours[bankTime][0], i[0], j[0], k[0]); - var finish = Math.min(workingHours[bankTime][1],i[1], j[1], k[1]); + var finish = Math.min(workingHours[bankTime][1], i[1], j[1], k[1]); while (start + duration <= finish) { roberyTime.push([start, finish]); start += HALF_HOUR; } - }); - }); - }); + } + } + } } } @@ -166,7 +169,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { time %= DAY; var hour = Math.floor(time / HOUR) - .toLocaleString(undefined, {'minimumIntegerDigits': 2}); + .toLocaleString(undefined, { 'minimumIntegerDigits': 2 }); time %= HOUR; var minute = Math.floor(time / MINUTE) From 32a9660995440d6c1313cd1dde9dd8adf6bc7afb Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Fri, 21 Oct 2016 22:58:57 +0500 Subject: [PATCH 04/19] fix lint 3 --- robbery.js | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/robbery.js b/robbery.js index 37ede1c..d6fd85d 100644 --- a/robbery.js +++ b/robbery.js @@ -74,7 +74,7 @@ function getTimeInterval(persSchedule) { return time; }); - result.push([start, DAY_END]) + result.push([start, DAY_END]); return result; } @@ -125,17 +125,34 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { workingHours = convertWorkingHours(workingHours, timezone); schedule = convertSchedule(schedule, timezone); + var i = 0; + var j = 0; + var k = 0; for (var bankTime = 0; bankTime < workingHours.length; ++bankTime) { - for (var i = 0; i < schedule[0].length; ++i) { - for (var j = 0; j < schedule[1].length; ++j) { - for (var k = 0; k < schedule[2].length; ++k) { - var start = Math.max(workingHours[bankTime][0], i[0], j[0], k[0]); - var finish = Math.min(workingHours[bankTime][1], i[1], j[1], k[1]); - - while (start + duration <= finish) { - roberyTime.push([start, finish]); - start += HALF_HOUR; - } + while (i < schedule[0].length) { + var start = Math.max( + workingHours[bankTime][0], + schedule[0][0], + schedule[1][0], + schedule[2][0] + ); + var finish = Math.min( + workingHours[bankTime][1], + schedule[0][1], + schedule[1][1], + schedule[2][1] + ); + + while (start + duration <= finish) { + roberyTime.push([start, finish]); + start += HALF_HOUR; + } + + if (++k === schedule[2].length) { + k = 0; + if (++j === schedule[1].length) { + j = 0; + ++i; } } } From aa2de4f9edfafb5299aebcc8f72c301eb7cc09d8 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Fri, 21 Oct 2016 23:12:28 +0500 Subject: [PATCH 05/19] fix lint 4 --- robbery.js | 90 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/robbery.js b/robbery.js index d6fd85d..e605f52 100644 --- a/robbery.js +++ b/robbery.js @@ -101,6 +101,47 @@ function convertSchedule(schedule, timezone) { return newSchedule; } +function getrobberyTimes(schedule, workingHours) { + var robberyTimes = []; + var bankTime = 0; + var i = 0; + var j = 0; + var k = 0; + + while (bankTime < workingHours.length) { + var start = Math.max( + workingHours[bankTime][0], + schedule[0][0], + schedule[1][0], + schedule[2][0] + ); + var finish = Math.min( + workingHours[bankTime][1], + schedule[0][1], + schedule[1][1], + schedule[2][1] + ); + + while (start + duration <= finish) { + robberyTimes.push([start, finish]); + start += HALF_HOUR; + } + + if (++k === schedule[2].length) { + k = 0; + if (++j === schedule[1].length) { + j = 0; + if (++i === schedule[0].length) { + i = 0; + ++bankTime; + } + } + } + } + + return robberyTimes; +} + /** * Сделано задание на звездочку * Реализовано оба метода и tryLater @@ -117,46 +158,15 @@ exports.isStar = true; */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { console.info(schedule, duration, workingHours); - var roberyTime = []; + var robberyTimes = []; if (isCorrectSchedule(schedule)) { var timezone = getTimezone(workingHours.from); - workingHours = convertWorkingHours(workingHours, timezone); - schedule = convertSchedule(schedule, timezone); - - var i = 0; - var j = 0; - var k = 0; - for (var bankTime = 0; bankTime < workingHours.length; ++bankTime) { - while (i < schedule[0].length) { - var start = Math.max( - workingHours[bankTime][0], - schedule[0][0], - schedule[1][0], - schedule[2][0] - ); - var finish = Math.min( - workingHours[bankTime][1], - schedule[0][1], - schedule[1][1], - schedule[2][1] - ); - - while (start + duration <= finish) { - roberyTime.push([start, finish]); - start += HALF_HOUR; - } - - if (++k === schedule[2].length) { - k = 0; - if (++j === schedule[1].length) { - j = 0; - ++i; - } - } - } - } + robberyTimes = getrobberyTimes( + convertSchedule(schedule, timezone), + convertWorkingHours(workingHours, timezone) + ); } return { @@ -166,7 +176,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - if (roberyTime.length !== 0) { + if (robberyTimes.length !== 0) { return true; } @@ -181,7 +191,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - var time = roberyTime[0][0]; + var time = robberyTimes[0][0]; var day = Math.floor(time / DAY); time %= DAY; @@ -203,8 +213,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { - roberyTime.shift(); - if (roberyTime && roberyTime.length !== 0) { + robberyTimes.shift(); + if (robberyTimes && robberyTimes.length !== 0) { return true; } From 0270f42bcff921c593f00d3acacb5e28e7460504 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Fri, 21 Oct 2016 23:20:03 +0500 Subject: [PATCH 06/19] fix lint 5 --- robbery.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/robbery.js b/robbery.js index e605f52..18d27a0 100644 --- a/robbery.js +++ b/robbery.js @@ -129,13 +129,15 @@ function getrobberyTimes(schedule, workingHours) { if (++k === schedule[2].length) { k = 0; - if (++j === schedule[1].length) { - j = 0; - if (++i === schedule[0].length) { - i = 0; - ++bankTime; - } - } + ++j; + } + if (j === schedule[1].length) { + j = 0; + ++i; + } + if (i === schedule[0].length) { + i = 0; + ++bankTime; } } From 98bff0927ea7bb60363b4c30d4a7adaf0348f397 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Fri, 21 Oct 2016 23:37:18 +0500 Subject: [PATCH 07/19] fix lint 7 --- robbery.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/robbery.js b/robbery.js index 18d27a0..346f906 100644 --- a/robbery.js +++ b/robbery.js @@ -101,7 +101,18 @@ function convertSchedule(schedule, timezone) { return newSchedule; } -function getrobberyTimes(schedule, workingHours) { +function getRobberyInterval(start, finish, duration) { + var robberyInterval = []; + + while (start + duration <= finish) { + robberyInterval.push([start, finish]); + start += HALF_HOUR; + } + + return robberyInterval; +} + +function getRobberyTimes(schedule, duration, workingHours) { var robberyTimes = []; var bankTime = 0; var i = 0; @@ -122,10 +133,7 @@ function getrobberyTimes(schedule, workingHours) { schedule[2][1] ); - while (start + duration <= finish) { - robberyTimes.push([start, finish]); - start += HALF_HOUR; - } + robberyTimes.concat(getRobberyInterval(start, finish, duration)); if (++k === schedule[2].length) { k = 0; @@ -165,8 +173,9 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (isCorrectSchedule(schedule)) { var timezone = getTimezone(workingHours.from); - robberyTimes = getrobberyTimes( + robberyTimes = getRobberyTimes( convertSchedule(schedule, timezone), + duration, convertWorkingHours(workingHours, timezone) ); } From d1e263c254cb81df6b2fce1acba6e780754a5f5a Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Sat, 22 Oct 2016 00:14:31 +0500 Subject: [PATCH 08/19] fix lint 8 --- robbery.js | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/robbery.js b/robbery.js index 346f906..d8c353a 100644 --- a/robbery.js +++ b/robbery.js @@ -114,26 +114,24 @@ function getRobberyInterval(start, finish, duration) { function getRobberyTimes(schedule, duration, workingHours) { var robberyTimes = []; - var bankTime = 0; - var i = 0; - var j = 0; - var k = 0; + var bankTime = i = j = k = 0; while (bankTime < workingHours.length) { - var start = Math.max( - workingHours[bankTime][0], - schedule[0][0], - schedule[1][0], - schedule[2][0] - ); - var finish = Math.min( - workingHours[bankTime][1], - schedule[0][1], - schedule[1][1], - schedule[2][1] - ); - - robberyTimes.concat(getRobberyInterval(start, finish, duration)); + robberyTimes.concat(getRobberyInterval( + Math.max( + workingHours[bankTime][0], + schedule[i][0], + schedule[j][0], + schedule[k][0] + ), + Math.min( + workingHours[bankTime][1], + schedule[i][1], + schedule[j][1], + schedule[k][1] + ), + duration + )); if (++k === schedule[2].length) { k = 0; From 2b19a84cc5d79aab1b01f6ad43b18635441296cb Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Sat, 22 Oct 2016 00:16:39 +0500 Subject: [PATCH 09/19] fix lint 8 --- robbery.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index d8c353a..128d980 100644 --- a/robbery.js +++ b/robbery.js @@ -114,7 +114,10 @@ function getRobberyInterval(start, finish, duration) { function getRobberyTimes(schedule, duration, workingHours) { var robberyTimes = []; - var bankTime = i = j = k = 0; + var bankTime = 0; + var i = 0; + var j = 0; + var k = 0; while (bankTime < workingHours.length) { robberyTimes.concat(getRobberyInterval( From dce17088f491d401e011bd2b3d126bb504fef003 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Sat, 22 Oct 2016 00:29:47 +0500 Subject: [PATCH 10/19] fix lint 9 --- robbery.js | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/robbery.js b/robbery.js index 128d980..db38e58 100644 --- a/robbery.js +++ b/robbery.js @@ -112,40 +112,43 @@ function getRobberyInterval(start, finish, duration) { return robberyInterval; } +function incIter(iter, iterMax) { + for (var i = iter.length - 1; i >= 0; --i) { + if (++iter[i] === iterMax[i].length) { + iter[i] = 0; + } else { + break; + } + } + + return iter; +} + function getRobberyTimes(schedule, duration, workingHours) { var robberyTimes = []; var bankTime = 0; - var i = 0; - var j = 0; - var k = 0; + var iter = [0, 0, 0]; + var iterMax = [schedule[0].length, schedule[1].length, schedule[2].length]; while (bankTime < workingHours.length) { robberyTimes.concat(getRobberyInterval( Math.max( workingHours[bankTime][0], - schedule[i][0], - schedule[j][0], - schedule[k][0] + schedule[iter[0]][0], + schedule[iter[1]][0], + schedule[iter[2]][0] ), Math.min( workingHours[bankTime][1], - schedule[i][1], - schedule[j][1], - schedule[k][1] + schedule[iter[0]][1], + schedule[iter[1]][1], + schedule[iter[2]][1] ), duration )); - if (++k === schedule[2].length) { - k = 0; - ++j; - } - if (j === schedule[1].length) { - j = 0; - ++i; - } - if (i === schedule[0].length) { - i = 0; + iter = incIter(iter, iterMax); + if (iter[0] === 0) { ++bankTime; } } From 554a3059efec06a4bc276ef5dc15b58e5e915be2 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Sat, 22 Oct 2016 00:33:08 +0500 Subject: [PATCH 11/19] fix convertWorkingHours --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index db38e58..b2ae156 100644 --- a/robbery.js +++ b/robbery.js @@ -52,8 +52,8 @@ function convertDate(date, timezone) { function convertWorkingHours(workingHours, timezone) { var newWorkingHours = []; - var timeFrom = convertDate(workingHours.from, timezone); - var timeTo = convertDate(workingHours.to, timezone); + var timeFrom = convertTime(workingHours.from, timezone); + var timeTo = convertTime(workingHours.to, timezone); for (var i = 0; i < BANK_DAYS; ++i) { newWorkingHours.push([ From f5e7ea4de91fe1d5e5ee65ebbe1494b01bb78409 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Sat, 22 Oct 2016 00:37:52 +0500 Subject: [PATCH 12/19] test --- robbery.js | 1 + 1 file changed, 1 insertion(+) diff --git a/robbery.js b/robbery.js index b2ae156..937f1d5 100644 --- a/robbery.js +++ b/robbery.js @@ -191,6 +191,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { + console.log(robberyTimes); if (robberyTimes.length !== 0) { return true; } From 745cbc9b1b4633342074131757eac61f999b27bf Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Sat, 22 Oct 2016 00:41:05 +0500 Subject: [PATCH 13/19] test --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 937f1d5..97232e6 100644 --- a/robbery.js +++ b/robbery.js @@ -191,7 +191,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - console.log(robberyTimes); + console.info(robberyTimes); if (robberyTimes.length !== 0) { return true; } From 23e020bb774a2a19157763409241252a011d8b09 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Sat, 22 Oct 2016 00:50:28 +0500 Subject: [PATCH 14/19] fix format --- robbery.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index 97232e6..bed80d9 100644 --- a/robbery.js +++ b/robbery.js @@ -171,7 +171,6 @@ exports.isStar = true; * @returns {Object} */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - console.info(schedule, duration, workingHours); var robberyTimes = []; if (isCorrectSchedule(schedule)) { @@ -183,6 +182,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { convertWorkingHours(workingHours, timezone) ); } + console.info(robberyTimes); return { @@ -191,7 +191,6 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - console.info(robberyTimes); if (robberyTimes.length !== 0) { return true; } @@ -207,6 +206,10 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { + if (robberyTimes.length === 0) { + return ''; + } + var time = robberyTimes[0][0]; var day = Math.floor(time / DAY); time %= DAY; From 234ccc25e94522d322a94cae043991d5e148278b Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Sun, 23 Oct 2016 12:05:41 +0500 Subject: [PATCH 15/19] fix errors --- robbery.js | 60 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/robbery.js b/robbery.js index bed80d9..2fa9873 100644 --- a/robbery.js +++ b/robbery.js @@ -68,8 +68,8 @@ function convertWorkingHours(workingHours, timezone) { function getTimeInterval(persSchedule) { var start = DAY_START; var result = persSchedule.map(function (item) { - var time = [start, item[0] - MINUTE]; - start = item[1] + MINUTE; + var time = [start, item[0]]; + start = item[1]; return time; }); @@ -91,9 +91,8 @@ function convertSchedule(schedule, timezone) { ]; }) .sort(function (i, j) { - return i[0] > j[0] ? -1 : 1; + return i[0] < j[0] ? -1 : 1; }); - newSchedule.push(getTimeInterval(persSchedule)); } } @@ -105,7 +104,7 @@ function getRobberyInterval(start, finish, duration) { var robberyInterval = []; while (start + duration <= finish) { - robberyInterval.push([start, finish]); + robberyInterval.push([start, start + duration]); start += HALF_HOUR; } @@ -113,44 +112,51 @@ function getRobberyInterval(start, finish, duration) { } function incIter(iter, iterMax) { - for (var i = iter.length - 1; i >= 0; --i) { - if (++iter[i] === iterMax[i].length) { + var incFirst = true; + for (var i = iter.length - 1; i > 0; --i) { + if (++iter[i] === iterMax[i]) { iter[i] = 0; } else { + incFirst = false; break; } } + if (incFirst) { + ++iter[0]; + } + return iter; } function getRobberyTimes(schedule, duration, workingHours) { var robberyTimes = []; - var bankTime = 0; - var iter = [0, 0, 0]; - var iterMax = [schedule[0].length, schedule[1].length, schedule[2].length]; - - while (bankTime < workingHours.length) { - robberyTimes.concat(getRobberyInterval( + var iter = [0, 0, 0, 0]; + var iterMax = [ + workingHours.length, + schedule[0].length, + schedule[1].length, + schedule[2].length + ]; + + while (iter[0] < iterMax[0]) { + robberyTimes = robberyTimes.concat(getRobberyInterval( Math.max( - workingHours[bankTime][0], - schedule[iter[0]][0], - schedule[iter[1]][0], - schedule[iter[2]][0] + workingHours[iter[0]][0], + schedule[0][iter[1]][0], + schedule[1][iter[2]][0], + schedule[2][iter[3]][0] ), Math.min( - workingHours[bankTime][1], - schedule[iter[0]][1], - schedule[iter[1]][1], - schedule[iter[2]][1] + workingHours[iter[0]][1], + schedule[0][iter[1]][1], + schedule[1][iter[2]][1], + schedule[2][iter[3]][1] ), duration )); iter = incIter(iter, iterMax); - if (iter[0] === 0) { - ++bankTime; - } } return robberyTimes; @@ -182,7 +188,6 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { convertWorkingHours(workingHours, timezone) ); } - console.info(robberyTimes); return { @@ -232,8 +237,9 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { - robberyTimes.shift(); - if (robberyTimes && robberyTimes.length !== 0) { + if (robberyTimes.length > 1) { + robberyTimes.shift(); + return true; } From 2616f7adc5b44395757dafa31523bf88d936b69b Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Sun, 23 Oct 2016 12:12:09 +0500 Subject: [PATCH 16/19] fix time borders --- robbery.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/robbery.js b/robbery.js index 2fa9873..7c5c70c 100644 --- a/robbery.js +++ b/robbery.js @@ -68,8 +68,8 @@ function convertWorkingHours(workingHours, timezone) { function getTimeInterval(persSchedule) { var start = DAY_START; var result = persSchedule.map(function (item) { - var time = [start, item[0]]; - start = item[1]; + var time = [start, item[0] - MINUTE]; + start = item[1] + MINUTE; return time; }); @@ -211,7 +211,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - if (robberyTimes.length === 0) { + if (robberyTimes.length === 0 && !template) { return ''; } From 9e1940afee14be21039b26ab8c7cbbf01eba1b59 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Mon, 24 Oct 2016 12:24:31 +0500 Subject: [PATCH 17/19] fix for lt 3 persons --- robbery.js | 112 ++++++++++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 62 deletions(-) diff --git a/robbery.js b/robbery.js index 7c5c70c..756d0bb 100644 --- a/robbery.js +++ b/robbery.js @@ -10,24 +10,6 @@ var DAY_END = 3 * DAY - MINUTE; var HALF_HOUR = MINUTE * 30; -function isCorrectSchedule(schedule) { - var keys = Object.keys(schedule); - - if (keys.length !== 3) { - return false; - } - - var isCorrect = true; - for (var i = 0; i < keys.length; ++i) { - if (schedule[keys[i]].length === 0) { - isCorrect = false; - break; - } - } - - return isCorrect; -} - function getTimezone(time) { time = /(\d{2}):(\d{2})\+(\d+)/.exec(time); @@ -68,8 +50,8 @@ function convertWorkingHours(workingHours, timezone) { function getTimeInterval(persSchedule) { var start = DAY_START; var result = persSchedule.map(function (item) { - var time = [start, item[0] - MINUTE]; - start = item[1] + MINUTE; + var time = [start, item[0]]; + start = item[1]; return time; }); @@ -100,23 +82,13 @@ function convertSchedule(schedule, timezone) { return newSchedule; } -function getRobberyInterval(start, finish, duration) { - var robberyInterval = []; - - while (start + duration <= finish) { - robberyInterval.push([start, start + duration]); - start += HALF_HOUR; - } - - return robberyInterval; -} - function incIter(iter, iterMax) { var incFirst = true; for (var i = iter.length - 1; i > 0; --i) { if (++iter[i] === iterMax[i]) { iter[i] = 0; - } else { + } + else { incFirst = false; break; } @@ -129,30 +101,50 @@ function incIter(iter, iterMax) { return iter; } -function getRobberyTimes(schedule, duration, workingHours) { - var robberyTimes = []; - var iter = [0, 0, 0, 0]; - var iterMax = [ - workingHours.length, - schedule[0].length, - schedule[1].length, - schedule[2].length +function getRobberyInterval(start, finish, duration) { + var robberyInterval = []; + + while (start + duration <= finish) { + robberyInterval.push([start, start + duration]); + start += HALF_HOUR; + } + + return robberyInterval; +} + +function getMinMax(fullSchedule, iter) { + var result = [ + fullSchedule[0][iter[0]][0], + fullSchedule[0][iter[0]][1] ]; + for (var i = 1; i < fullSchedule.length; ++i) { + result[0] = Math.max(result[0], fullSchedule[i][iter[i]][0]); + result[1] = Math.min(result[1], fullSchedule[i][iter[i]][1]); + } + + return result; +} + +function getIters(iter, iterMax, fullSchedule) { + for (var i = 0; i < fullSchedule.length; ++i) { + iter.push(0); + iterMax.push(fullSchedule[i].length); + } +} + +function getRobberyTimes(fullSchedule, duration) { + var robberyTimes = []; + var iter = []; + var iterMax = []; + + getIters(iter, iterMax, fullSchedule); + while (iter[0] < iterMax[0]) { + var minMax = getMinMax(fullSchedule, iter); robberyTimes = robberyTimes.concat(getRobberyInterval( - Math.max( - workingHours[iter[0]][0], - schedule[0][iter[1]][0], - schedule[1][iter[2]][0], - schedule[2][iter[3]][0] - ), - Math.min( - workingHours[iter[0]][1], - schedule[0][iter[1]][1], - schedule[1][iter[2]][1], - schedule[2][iter[3]][1] - ), + minMax[0], + minMax[1], duration )); @@ -176,18 +168,14 @@ exports.isStar = true; * @param {String} workingHours.to – Время закрытия, например, '18:00+5' * @returns {Object} */ -exports.getAppropriateMoment = function (schedule, duration, workingHours) { +exports.getAppropriateMoment = function sad(schedule, duration, workingHours) { var robberyTimes = []; - if (isCorrectSchedule(schedule)) { - var timezone = getTimezone(workingHours.from); + var timezone = getTimezone(workingHours.from); + var fullSchedule = [convertWorkingHours(workingHours, timezone)]; + fullSchedule = fullSchedule.concat(convertSchedule(schedule, timezone)); - robberyTimes = getRobberyTimes( - convertSchedule(schedule, timezone), - duration, - convertWorkingHours(workingHours, timezone) - ); - } + robberyTimes = getRobberyTimes(fullSchedule, duration); return { @@ -211,7 +199,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - if (robberyTimes.length === 0 && !template) { + if (robberyTimes.length === 0 || !template) { return ''; } From dbc4f9c5abf33a9c2282ed15d8263aee43dd8f8c Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Mon, 24 Oct 2016 12:27:13 +0500 Subject: [PATCH 18/19] fix lint --- robbery.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index 756d0bb..f8d7203 100644 --- a/robbery.js +++ b/robbery.js @@ -87,8 +87,7 @@ function incIter(iter, iterMax) { for (var i = iter.length - 1; i > 0; --i) { if (++iter[i] === iterMax[i]) { iter[i] = 0; - } - else { + } else { incFirst = false; break; } From 08d4787735e448201386f8907e56adfd4a2ba036 Mon Sep 17 00:00:00 2001 From: Nikolay Makhalin Date: Thu, 27 Oct 2016 15:06:05 +0500 Subject: [PATCH 19/19] test --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index f8d7203..336dbb8 100644 --- a/robbery.js +++ b/robbery.js @@ -141,12 +141,12 @@ function getRobberyTimes(fullSchedule, duration) { while (iter[0] < iterMax[0]) { var minMax = getMinMax(fullSchedule, iter); + robberyTimes = robberyTimes.concat(getRobberyInterval( minMax[0], minMax[1], duration )); - iter = incIter(iter, iterMax); }