From b1379ce18a317877723a8c44a4f64338eb38b627 Mon Sep 17 00:00:00 2001 From: reindeermur Date: Thu, 19 Oct 2017 12:14:49 +0500 Subject: [PATCH 01/10] one --- robbery.js | 243 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 240 insertions(+), 3 deletions(-) diff --git a/robbery.js b/robbery.js index 4a8309d..41faf13 100644 --- a/robbery.js +++ b/robbery.js @@ -6,6 +6,15 @@ */ exports.isStar = true; +const MIN_IN_HOUR = 60; +const HOUR_IN_DAY = 24; +const MIN_IN_DAY = MIN_IN_HOUR * HOUR_IN_DAY; +const DAYS = { + 'ПН': MIN_IN_DAY, + 'ВТ': MIN_IN_DAY * 2, + 'СР': MIN_IN_DAY * 3 +}; + /** * @param {Object} schedule – Расписание Банды * @param {Number} duration - Время на ограбление в минутах @@ -17,6 +26,230 @@ exports.isStar = true; exports.getAppropriateMoment = function (schedule, duration, workingHours) { console.info(schedule, duration, workingHours); + function getTimeZone (time) { + return parseInt((time.slice(6))); + } + + function getMinutes (time) { + return parseInt(time.slice(3,5)); + } + + function getHours (time) { + return parseInt(time.slice(0,2)); + } + + var workingZone = getTimeZone(workingHours.from); + + function convertToMinutes (time) { + let minutes = 0; + minutes += getHours(time) * MIN_IN_HOUR; + minutes += getMinutes(time); + minutes += (workingZone - getTimeZone(time)) * MIN_IN_HOUR; + + return minutes; + } + + function getTimeInterval (record) { + let interval = []; + let startTime = (record.from).split(' '); + let endTime = (record.to).split(' '); + let start = convertToMinutes(startTime[1]); + let end = convertToMinutes(endTime[1]); + if (startTime[0] === 'ВТ') { + start += DAYS.ПН; + } + if (endTime[0] === 'ВТ') { + end += DAYS.ПН; + } + if (startTime[0] === 'СР') { + start += DAYS.ВТ; + } + if (endTime[0] === 'СР') { + end += DAYS.ВТ; + } + interval.push(start); + interval.push(end); + + return interval; + } + + function compareElements(a, b) { + return a[0] - b[0]; + } + + function getSchedule () { + let reversedSchedule = []; + for (let man of Object.keys(schedule)) { + for (let record of schedule[man] ) { + reversedSchedule.push(getTimeInterval(record)); + } + } + + return reversedSchedule.sort(compareElements); + } + + function getMax(a, b) { + if (a > b){ + return a; + } + + return b; + } + + function getWorkingSchedule () { + let oldSchedule = getSchedule(); + let resultSchedule = []; + if (oldSchedule[0][0] >= 0) { + resultSchedule.push([0, oldSchedule[0][0]]); + } + let currentMax = oldSchedule[0][1]; + for (let i=1; i bank[0])) { + if(((gang[1] < bank[1]) && (gang[1] - bank[0] >= min)) || + ((gang[1] >= bank[1]) && (bank[1] - bank[0] >= min))) { + + result.push(bank[0]); + result.push(getMin(bank[1], gang[1])); + } + } + if ((bank[0] <= gang[0]) && (bank[1] > gang[0])) { + if(((gang[1] < bank[1]) && (gang[1] - gang[0] >= min)) || + ((gang[1] >= bank[1]) && (bank[1] - gang[0] >= min))) { + + result.push(gang[0]); + result.push(getMin(bank[1], gang[1])); + } + } + + return result; + } + + function tryToGetTime () { + gangSchedule = getWorkingSchedule(); + var startCrime =[]; + for (let i = 0; i < gangSchedule.length; i++) { + for(let j = 0; j < bankSchedule.length; j++) { + if (checkInterval(gangSchedule[i], bankSchedule[j], duration).length !== 0) { + startCrime.push(checkInterval( + gangSchedule[i], bankSchedule[j], duration)); + } + } + } + + return startCrime; + } + + function changeFormate (number){ + let time = []; + let oldTime = number; + if (oldTime > DAYS.ВТ && oldTime <= DAYS.СР){ + time.push('СР'); + oldTime-= DAYS.ВТ; + } + if (oldTime > DAYS.ПН && oldTime <= DAYS.ВТ){ + time.push('ВТ'); + oldTime-= DAYS.ПН; + } + else { + time.push('ПН'); + } + hours = (oldTime - (oldTime % MIN_IN_HOUR)) / MIN_IN_HOUR; + minutes = oldTime % MIN_IN_HOUR + if (hours < 10) { + time.push('0'+ hours); + } + else { + time.push(hours); + } + if (minutes < 10) { + time.push('0'+ minutes); + } + else { + time.push(minutes); + } + + return time; + } + + function check() { + if (tryToGetTime().length !== 0) { + return true; + } + + return false; + } + + function createTimeList () { + let timelist = []; + if(check()){ + let timeInterval = tryToGetTime(); + let n; + for (let i = 0; i= duration)){ + timelist.push(n); + n += 30; + n += duration; + } + } + } + + return timelist; + } + + var topList = createTimeList(); + const lastChance = parseInt(topList.slice(-1)); + + function changeTemplate (template) { + if (topList.length === 0) { + time = changeFormate(lastChance); + } + else { + time = changeFormate(topList[0]); + topList.splice(0,1); + } + return template + .replace('%HH', time[1]) + .replace('%MM', time[2]) + .replace('%DD', time[0]); + } + return { /** @@ -24,7 +257,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - return false; + return check(); }, /** @@ -35,7 +268,11 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - return template; + if (check()){ + return(changeTemplate(template)); + } + + return ''; }, /** @@ -44,7 +281,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { - return false; + return (topList.length === 0); } }; }; From f3bf8230508bed34ecb2f3c85d8dc192fb2c878f Mon Sep 17 00:00:00 2001 From: reindeermur Date: Thu, 19 Oct 2017 13:15:09 +0500 Subject: [PATCH 02/10] again --- robbery.js | 172 ++++++++++++++++++++++++++--------------------------- 1 file changed, 83 insertions(+), 89 deletions(-) diff --git a/robbery.js b/robbery.js index 41faf13..094b834 100644 --- a/robbery.js +++ b/robbery.js @@ -26,21 +26,21 @@ const DAYS = { exports.getAppropriateMoment = function (schedule, duration, workingHours) { console.info(schedule, duration, workingHours); - function getTimeZone (time) { + function getTimeZone(time) { return parseInt((time.slice(6))); } - function getMinutes (time) { - return parseInt(time.slice(3,5)); + function getMinutes(time) { + return parseInt(time.slice(3, 5)); } - function getHours (time) { - return parseInt(time.slice(0,2)); + function getHours(time) { + return parseInt(time.slice(0, 2)); } var workingZone = getTimeZone(workingHours.from); - function convertToMinutes (time) { + function convertToMinutes(time) { let minutes = 0; minutes += getHours(time) * MIN_IN_HOUR; minutes += getMinutes(time); @@ -49,24 +49,23 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { return minutes; } - function getTimeInterval (record) { + function getDay(day) { + if (day === 'ВТ') { + return (DAYS.ПН); + } + if (day === 'СР') { + return (DAYS.ВТ); + } + } + + function getTimeInterval(record) { let interval = []; let startTime = (record.from).split(' '); let endTime = (record.to).split(' '); let start = convertToMinutes(startTime[1]); let end = convertToMinutes(endTime[1]); - if (startTime[0] === 'ВТ') { - start += DAYS.ПН; - } - if (endTime[0] === 'ВТ') { - end += DAYS.ПН; - } - if (startTime[0] === 'СР') { - start += DAYS.ВТ; - } - if (endTime[0] === 'СР') { - end += DAYS.ВТ; - } + start += getDay(startTime[0]); + end += getDay(endTime[0]); interval.push(start); interval.push(end); @@ -77,10 +76,10 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { return a[0] - b[0]; } - function getSchedule () { + function getSchedule() { let reversedSchedule = []; for (let man of Object.keys(schedule)) { - for (let record of schedule[man] ) { + for (let record of schedule[man]) { reversedSchedule.push(getTimeInterval(record)); } } @@ -89,30 +88,29 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { } function getMax(a, b) { - if (a > b){ + if (a > b) { return a; } return b; } - function getWorkingSchedule () { + function getWorkingSchedule() { let oldSchedule = getSchedule(); let resultSchedule = []; if (oldSchedule[0][0] >= 0) { resultSchedule.push([0, oldSchedule[0][0]]); } let currentMax = oldSchedule[0][1]; - for (let i=1; i bank[0])) { - if(((gang[1] < bank[1]) && (gang[1] - bank[0] >= min)) || + if (((gang[1] < bank[1]) && (gang[1] - bank[0] >= min)) || ((gang[1] >= bank[1]) && (bank[1] - bank[0] >= min))) { result.push(bank[0]); @@ -148,61 +146,54 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { } } if ((bank[0] <= gang[0]) && (bank[1] > gang[0])) { - if(((gang[1] < bank[1]) && (gang[1] - gang[0] >= min)) || + if (((gang[1] < bank[1]) && (gang[1] - gang[0] >= min)) || ((gang[1] >= bank[1]) && (bank[1] - gang[0] >= min))) { result.push(gang[0]); result.push(getMin(bank[1], gang[1])); } } - return result; } - function tryToGetTime () { - gangSchedule = getWorkingSchedule(); - var startCrime =[]; + function tryToGetTime() { + let gangSchedule = getWorkingSchedule(); + var startCrime = []; for (let i = 0; i < gangSchedule.length; i++) { - for(let j = 0; j < bankSchedule.length; j++) { - if (checkInterval(gangSchedule[i], bankSchedule[j], duration).length !== 0) { + for (let j = 0; j < bankSchedule.length; j++) { startCrime.push(checkInterval( gangSchedule[i], bankSchedule[j], duration)); - } } } - return startCrime; } - function changeFormate (number){ + function transform (number) { + result = ''; + if (number < 10) { + result = ('0'+ number); + } else { + result = number; + } + return result; + } + function changeFormate(number) { let time = []; let oldTime = number; - if (oldTime > DAYS.ВТ && oldTime <= DAYS.СР){ + if (oldTime > DAYS.ВТ && oldTime <= DAYS.СР) { time.push('СР'); - oldTime-= DAYS.ВТ; + oldTime -= DAYS.ВТ; } - if (oldTime > DAYS.ПН && oldTime <= DAYS.ВТ){ + if (oldTime > DAYS.ПН && oldTime <= DAYS.ВТ) { time.push('ВТ'); - oldTime-= DAYS.ПН; - } - else { + oldTime -= DAYS.ПН; + } else { time.push('ПН'); } - hours = (oldTime - (oldTime % MIN_IN_HOUR)) / MIN_IN_HOUR; - minutes = oldTime % MIN_IN_HOUR - if (hours < 10) { - time.push('0'+ hours); - } - else { - time.push(hours); - } - if (minutes < 10) { - time.push('0'+ minutes); - } - else { - time.push(minutes); - } - + let hours = (oldTime - (oldTime % MIN_IN_HOUR)) / MIN_IN_HOUR; + let minutes = oldTime % MIN_IN_HOUR; + time.push(transform (hours)); + time.pudh(transform(minutes)); return time; } @@ -210,46 +201,50 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (tryToGetTime().length !== 0) { return true; } - return false; } - function createTimeList () { + function showList () { let timelist = []; - if(check()){ - let timeInterval = tryToGetTime(); - let n; - for (let i = 0; i= duration)){ - timelist.push(n); - n += 30; - n += duration; - } + let timeInterval = tryToGetTime(); + let n; + for (let i = 0; i < timeInterval.length; i++) { + n = (timeInterval[i][0]); + while ((n < timeInterval[i][1]) && + (timeInterval[i][1] - n >= duration)){ + timelist.push(n); + n += 30; + n += duration; } } - + return timelist; + } + + function createTimeList() { + timelist = [] + if (check()) { + timelist = showList(); + } return timelist; } var topList = createTimeList(); const lastChance = parseInt(topList.slice(-1)); - function changeTemplate (template) { + function changeTemplate(template) { + let time; if (topList.length === 0) { time = changeFormate(lastChance); - } - else { + } else { time = changeFormate(topList[0]); - topList.splice(0,1); + topList.splice(0, 1); } + return template - .replace('%HH', time[1]) - .replace('%MM', time[2]) - .replace('%DD', time[0]); + .replace('%HH', time[1]) + .replace('%MM', time[2]) + .replace('%DD', time[0]); } - return { /** @@ -268,10 +263,9 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - if (check()){ - return(changeTemplate(template)); + if (check()) { + return (changeTemplate(template)); } - return ''; }, From d038b5f4d8f0240ae48733c6397be6c6471bc65a Mon Sep 17 00:00:00 2001 From: reindeermur Date: Thu, 19 Oct 2017 13:34:52 +0500 Subject: [PATCH 03/10] and again --- robbery.js | 66 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/robbery.js b/robbery.js index 094b834..b4472e1 100644 --- a/robbery.js +++ b/robbery.js @@ -135,24 +135,45 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { return b; } - function checkInterval(gang, bank, min) { + function checkLeft(a, b, c, d, min) { let result = []; - if ((gang[0] <= bank[0]) && (gang[1] > bank[0])) { - if (((gang[1] < bank[1]) && (gang[1] - bank[0] >= min)) || - ((gang[1] >= bank[1]) && (bank[1] - bank[0] >= min))) { + if ((a <= c) && (b > c)) { + if(((b < d) && (b - c >= min)) || + ((b >= d) && (d - c >= min))) { - result.push(bank[0]); - result.push(getMin(bank[1], gang[1])); + result.push(c); + result.push(getMin(d, b)); } } - if ((bank[0] <= gang[0]) && (bank[1] > gang[0])) { - if (((gang[1] < bank[1]) && (gang[1] - gang[0] >= min)) || - ((gang[1] >= bank[1]) && (bank[1] - gang[0] >= min))) { - result.push(gang[0]); - result.push(getMin(bank[1], gang[1])); + return result; + } + + function checkRight(a, b, c, d, min) { + let result = []; + if ((c <= a) && (d > a)) { + if(((b < d) && (b - a >= min)) || + ((b >= d) && (d - a >= min))) { + + result.push(a); + result.push(getMin(d, b)); } } + + return result; + } + + function checkInterval(gang, bank, min) { + let result = []; + let left = checkLeft(gang[0], gang[1], bank[0], bank[1], min); + let right = checkRight(gang[0], gang[1], bank[0], bank[1], min); + if (left.length !== 0) { + result = left; + } + if (right.length !== 0) { + result = right; + } + return result; } @@ -161,17 +182,18 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var startCrime = []; for (let i = 0; i < gangSchedule.length; i++) { for (let j = 0; j < bankSchedule.length; j++) { - startCrime.push(checkInterval( - gangSchedule[i], bankSchedule[j], duration)); + startCrime.push(checkInterval( + gangSchedule[i], bankSchedule[j], duration)); } } + return startCrime; } - function transform (number) { - result = ''; + function transform(number) { + let result = ''; if (number < 10) { - result = ('0'+ number); + result = ('0' + number); } else { result = number; } @@ -194,6 +216,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { let minutes = oldTime % MIN_IN_HOUR; time.push(transform (hours)); time.pudh(transform(minutes)); + return time; } @@ -201,30 +224,33 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (tryToGetTime().length !== 0) { return true; } + return false; } - function showList () { + function showList() { let timelist = []; let timeInterval = tryToGetTime(); let n; for (let i = 0; i < timeInterval.length; i++) { n = (timeInterval[i][0]); while ((n < timeInterval[i][1]) && - (timeInterval[i][1] - n >= duration)){ + (timeInterval[i][1] - n >= duration)) { timelist.push(n); n += 30; n += duration; } } + return timelist; } function createTimeList() { - timelist = [] + let timelist = []; if (check()) { timelist = showList(); } + return timelist; } @@ -245,6 +271,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { .replace('%MM', time[2]) .replace('%DD', time[0]); } + return { /** @@ -266,6 +293,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (check()) { return (changeTemplate(template)); } + return ''; }, From b79b175f8312d48da6d76389524a7abe213cc118 Mon Sep 17 00:00:00 2001 From: reindeermur Date: Thu, 19 Oct 2017 13:56:34 +0500 Subject: [PATCH 04/10] aaand agaain --- robbery.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/robbery.js b/robbery.js index b4472e1..753f1d1 100644 --- a/robbery.js +++ b/robbery.js @@ -56,6 +56,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (day === 'СР') { return (DAYS.ВТ); } + + return 0; } function getTimeInterval(record) { @@ -135,11 +137,11 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { return b; } - function checkLeft(a, b, c, d, min) { + function checkLeft(a, b, c, d) { let result = []; if ((a <= c) && (b > c)) { - if(((b < d) && (b - c >= min)) || - ((b >= d) && (d - c >= min))) { + if (((b < d) && (b - c >= duration)) || + ((b >= d) && (d - c >= duration))) { result.push(c); result.push(getMin(d, b)); @@ -149,11 +151,11 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { return result; } - function checkRight(a, b, c, d, min) { + function checkRight(a, b, c, d) { let result = []; if ((c <= a) && (d > a)) { - if(((b < d) && (b - a >= min)) || - ((b >= d) && (d - a >= min))) { + if(((b < d) && (b - a >= duration)) || + ((b >= d) && (d - a >= duration))) { result.push(a); result.push(getMin(d, b)); @@ -173,7 +175,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (right.length !== 0) { result = right; } - + return result; } @@ -186,7 +188,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { gangSchedule[i], bankSchedule[j], duration)); } } - + return startCrime; } @@ -197,6 +199,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { } else { result = number; } + return result; } function changeFormate(number) { @@ -224,7 +227,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (tryToGetTime().length !== 0) { return true; } - + return false; } @@ -241,7 +244,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { n += duration; } } - + return timelist; } @@ -250,7 +253,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (check()) { timelist = showList(); } - + return timelist; } @@ -271,7 +274,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { .replace('%MM', time[2]) .replace('%DD', time[0]); } - + return { /** From 764ba1da2e8fe76e479be09b533d86eeb2dde400 Mon Sep 17 00:00:00 2001 From: reindeermur Date: Thu, 19 Oct 2017 14:03:16 +0500 Subject: [PATCH 05/10] no space --- robbery.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/robbery.js b/robbery.js index 753f1d1..d53fa36 100644 --- a/robbery.js +++ b/robbery.js @@ -56,10 +56,10 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (day === 'СР') { return (DAYS.ВТ); } - + return 0; } - + function getTimeInterval(record) { let interval = []; let startTime = (record.from).split(' '); @@ -154,7 +154,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { function checkRight(a, b, c, d) { let result = []; if ((c <= a) && (d > a)) { - if(((b < d) && (b - a >= duration)) || + if (((b < d) && (b - a >= duration)) || ((b >= d) && (d - a >= duration))) { result.push(a); @@ -164,7 +164,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { return result; } - + function checkInterval(gang, bank, min) { let result = []; let left = checkLeft(gang[0], gang[1], bank[0], bank[1], min); @@ -219,7 +219,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { let minutes = oldTime % MIN_IN_HOUR; time.push(transform (hours)); time.pudh(transform(minutes)); - + return time; } @@ -247,7 +247,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { return timelist; } - + function createTimeList() { let timelist = []; if (check()) { @@ -268,7 +268,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { time = changeFormate(topList[0]); topList.splice(0, 1); } - + return template .replace('%HH', time[1]) .replace('%MM', time[2]) From 12d6bd912559fefcdec8953dd3b52e584b92e090 Mon Sep 17 00:00:00 2001 From: reindeermur Date: Thu, 19 Oct 2017 14:41:12 +0500 Subject: [PATCH 06/10] rr --- robbery.js | 421 ++++++++++++++++++++++++++--------------------------- 1 file changed, 210 insertions(+), 211 deletions(-) diff --git a/robbery.js b/robbery.js index d53fa36..8ddb267 100644 --- a/robbery.js +++ b/robbery.js @@ -15,266 +15,265 @@ const DAYS = { 'СР': MIN_IN_DAY * 3 }; -/** - * @param {Object} schedule – Расписание Банды - * @param {Number} duration - Время на ограбление в минутах - * @param {Object} workingHours – Время работы банка - * @param {String} workingHours.from – Время открытия, например, "10:00+5" - * @param {String} workingHours.to – Время закрытия, например, "18:00+5" - * @returns {Object} - */ -exports.getAppropriateMoment = function (schedule, duration, workingHours) { - console.info(schedule, duration, workingHours); - - function getTimeZone(time) { - return parseInt((time.slice(6))); +function getTimeZone(time) { + return parseInt((time.slice(6))); +} + +function getMinutes(time) { + return parseInt(time.slice(3, 5)); +} + +function getHours(time) { + return parseInt(time.slice(0, 2)); +} + +function convertToMinutes(time) { + let minutes = 0; + minutes += getHours(time) * MIN_IN_HOUR; + minutes += getMinutes(time); + minutes += (workingZone - getTimeZone(time)) * MIN_IN_HOUR; + + return minutes; +} + +function getDay(day) { + if (day === 'ВТ') { + return (DAYS.ПН); } - - function getMinutes(time) { - return parseInt(time.slice(3, 5)); + if (day === 'СР') { + return (DAYS.ВТ); } - function getHours(time) { - return parseInt(time.slice(0, 2)); + return 0; +} + +function getTimeInterval(record) { + let interval = []; + let startTime = (record.from).split(' '); + let endTime = (record.to).split(' '); + let start = convertToMinutes(startTime[1]); + let end = convertToMinutes(endTime[1]); + start += getDay(startTime[0]); + end += getDay(endTime[0]); + interval.push(start); + interval.push(end); + + return interval; +} + +function compareElements(a, b) { + return a[0] - b[0]; +} + +function getSchedule() { + let reversedSchedule = []; + for (let man of Object.keys(schedule)) { + for (let record of schedule[man]) { + reversedSchedule.push(getTimeInterval(record)); + } } - var workingZone = getTimeZone(workingHours.from); - - function convertToMinutes(time) { - let minutes = 0; - minutes += getHours(time) * MIN_IN_HOUR; - minutes += getMinutes(time); - minutes += (workingZone - getTimeZone(time)) * MIN_IN_HOUR; + return reversedSchedule.sort(compareElements); +} - return minutes; +function getMax(a, b) { + if (a > b) { + return a; } - function getDay(day) { - if (day === 'ВТ') { - return (DAYS.ПН); - } - if (day === 'СР') { - return (DAYS.ВТ); - } + return b; +} - return 0; +function getWorkingSchedule() { + let oldSchedule = getSchedule(); + let resultSchedule = []; + if (oldSchedule[0][0] >= 0) { + resultSchedule.push([0, oldSchedule[0][0]]); } - - function getTimeInterval(record) { - let interval = []; - let startTime = (record.from).split(' '); - let endTime = (record.to).split(' '); - let start = convertToMinutes(startTime[1]); - let end = convertToMinutes(endTime[1]); - start += getDay(startTime[0]); - end += getDay(endTime[0]); - interval.push(start); - interval.push(end); - - return interval; + let currentMax = oldSchedule[0][1]; + for (let i = 1; i < oldSchedule.length; i++) { + if (oldSchedule[i][0] + 1 <= currentMax) { + currentMax = getMax(currentMax, oldSchedule[i][1]); + } else { + resultSchedule.push([currentMax, oldSchedule[i][0]]); + currentMax = oldSchedule[i][1]; + } } - - function compareElements(a, b) { - return a[0] - b[0]; + if (currentMax < DAYS.СР) { + resultSchedule.push([currentMax, DAYS.СР]); } - function getSchedule() { - let reversedSchedule = []; - for (let man of Object.keys(schedule)) { - for (let record of schedule[man]) { - reversedSchedule.push(getTimeInterval(record)); - } - } + return resultSchedule; - return reversedSchedule.sort(compareElements); - } - - function getMax(a, b) { - if (a > b) { - return a; - } +} - return b; +function getMin(a, b) { + if (a < b) { + return a; } - function getWorkingSchedule() { - let oldSchedule = getSchedule(); - let resultSchedule = []; - if (oldSchedule[0][0] >= 0) { - resultSchedule.push([0, oldSchedule[0][0]]); - } - let currentMax = oldSchedule[0][1]; - for (let i = 1; i < oldSchedule.length; i++) { - if (oldSchedule[i][0] + 1 <= currentMax) { - currentMax = getMax(currentMax, oldSchedule[i][1]); - } else { - resultSchedule.push([currentMax, oldSchedule[i][0]]); - currentMax = oldSchedule[i][1]; - } - } - if (currentMax < DAYS.СР) { - resultSchedule.push([currentMax, DAYS.СР]); - } + return b; +} - return resultSchedule; +function checkLeft(a, b, c, d) { + let result = []; + if ((a <= c) && (b > c)) { + if (((b < d) && (b - c >= duration)) || + ((b >= d) && (d - c >= duration))) { + result.push(c); + result.push(getMin(d, b)); + } } - var startBank = convertToMinutes(workingHours.from); - var endBank = convertToMinutes(workingHours.to); + return result; +} - var bankSchedule = [ - [startBank, endBank], - [startBank + DAYS.ПН, endBank + DAYS.ПН], - [startBank + DAYS.ВТ, endBank + DAYS.ВТ] - ]; +function checkRight(a, b, c, d) { + let result = []; + if ((c <= a) && (d > a)) { + if (((b < d) && (b - a >= duration)) || + ((b >= d) && (d - a >= duration))) { - function getMin(a, b) { - if (a < b) { - return a; + result.push(a); + result.push(getMin(d, b)); } - - return b; } - function checkLeft(a, b, c, d) { - let result = []; - if ((a <= c) && (b > c)) { - if (((b < d) && (b - c >= duration)) || - ((b >= d) && (d - c >= duration))) { + return result; +} - result.push(c); - result.push(getMin(d, b)); - } - } - - return result; +function checkInterval(gang, bank, min) { + let result = []; + let left = checkLeft(gang[0], gang[1], bank[0], bank[1], min); + let right = checkRight(gang[0], gang[1], bank[0], bank[1], min); + if (left.length !== 0) { + result = left; + } + if (right.length !== 0) { + result = right; } - function checkRight(a, b, c, d) { - let result = []; - if ((c <= a) && (d > a)) { - if (((b < d) && (b - a >= duration)) || - ((b >= d) && (d - a >= duration))) { + return result; +} - result.push(a); - result.push(getMin(d, b)); - } +function tryToGetTime() { + let gangSchedule = getWorkingSchedule(); + var startCrime = []; + for (let i = 0; i < gangSchedule.length; i++) { + for (let j = 0; j < bankSchedule.length; j++) { + startCrime.push(checkInterval( + gangSchedule[i], bankSchedule[j], duration)); } - - return result; } - function checkInterval(gang, bank, min) { - let result = []; - let left = checkLeft(gang[0], gang[1], bank[0], bank[1], min); - let right = checkRight(gang[0], gang[1], bank[0], bank[1], min); - if (left.length !== 0) { - result = left; - } - if (right.length !== 0) { - result = right; - } + return startCrime; +} - return result; +function transform(number) { + let result = ''; + if (number < 10) { + result = ('0' + number); + } else { + result = number; } - function tryToGetTime() { - let gangSchedule = getWorkingSchedule(); - var startCrime = []; - for (let i = 0; i < gangSchedule.length; i++) { - for (let j = 0; j < bankSchedule.length; j++) { - startCrime.push(checkInterval( - gangSchedule[i], bankSchedule[j], duration)); - } - } - - return startCrime; + return result; +} +function changeFormate(number) { + let time = []; + let oldTime = number; + if (oldTime > DAYS.ВТ && oldTime <= DAYS.СР) { + time.push('СР'); + oldTime -= DAYS.ВТ; } - - function transform(number) { - let result = ''; - if (number < 10) { - result = ('0' + number); - } else { - result = number; - } - - return result; + if (oldTime > DAYS.ПН && oldTime <= DAYS.ВТ) { + time.push('ВТ'); + oldTime -= DAYS.ПН; + } else { + time.push('ПН'); } - function changeFormate(number) { - let time = []; - let oldTime = number; - if (oldTime > DAYS.ВТ && oldTime <= DAYS.СР) { - time.push('СР'); - oldTime -= DAYS.ВТ; - } - if (oldTime > DAYS.ПН && oldTime <= DAYS.ВТ) { - time.push('ВТ'); - oldTime -= DAYS.ПН; - } else { - time.push('ПН'); - } - let hours = (oldTime - (oldTime % MIN_IN_HOUR)) / MIN_IN_HOUR; - let minutes = oldTime % MIN_IN_HOUR; - time.push(transform (hours)); - time.pudh(transform(minutes)); + let hours = (oldTime - (oldTime % MIN_IN_HOUR)) / MIN_IN_HOUR; + let minutes = oldTime % MIN_IN_HOUR; + time.push(transform (hours)); + time.pudh(transform(minutes)); + + return time; +} - return time; +function check() { + if (tryToGetTime().length !== 0) { + return true; } - function check() { - if (tryToGetTime().length !== 0) { - return true; + return false; +} + +function showList() { + let timelist = []; + let timeInterval = tryToGetTime(); + let n; + for (let i = 0; i < timeInterval.length; i++) { + n = (timeInterval[i][0]); + while ((n < timeInterval[i][1]) && + (timeInterval[i][1] - n >= duration)) { + timelist.push(n); + n += 30; + n += duration; } - - return false; } - function showList() { - let timelist = []; - let timeInterval = tryToGetTime(); - let n; - for (let i = 0; i < timeInterval.length; i++) { - n = (timeInterval[i][0]); - while ((n < timeInterval[i][1]) && - (timeInterval[i][1] - n >= duration)) { - timelist.push(n); - n += 30; - n += duration; - } - } + return timelist; +} - return timelist; +function createTimeList() { + let timelist = []; + if (check()) { + timelist = showList(); } - function createTimeList() { - let timelist = []; - if (check()) { - timelist = showList(); - } + return timelist; +} - return timelist; +function changeTemplate(template) { + let time; + if (topList.length === 0) { + time = changeFormate(lastChance); + } else { + time = changeFormate(topList[0]); + topList.splice(0, 1); } + return template + .replace('%HH', time[1]) + .replace('%MM', time[2]) + .replace('%DD', time[0]); +} + +/** + * @param {Object} schedule – Расписание Банды + * @param {Number} duration - Время на ограбление в минутах + * @param {Object} workingHours – Время работы банка + * @param {String} workingHours.from – Время открытия, например, "10:00+5" + * @param {String} workingHours.to – Время закрытия, например, "18:00+5" + * @returns {Object} + */ +exports.getAppropriateMoment = function (schedule, duration, workingHours) { + //console.info(schedule, duration, workingHours); + var workingZone = getTimeZone(workingHours.from); + var startBank = convertToMinutes(workingHours.from); + var endBank = convertToMinutes(workingHours.to); + + var bankSchedule = [ + [startBank, endBank], + [startBank + DAYS.ПН, endBank + DAYS.ПН], + [startBank + DAYS.ВТ, endBank + DAYS.ВТ] + ]; var topList = createTimeList(); const lastChance = parseInt(topList.slice(-1)); - - function changeTemplate(template) { - let time; - if (topList.length === 0) { - time = changeFormate(lastChance); - } else { - time = changeFormate(topList[0]); - topList.splice(0, 1); - } - - return template - .replace('%HH', time[1]) - .replace('%MM', time[2]) - .replace('%DD', time[0]); - } - + var ok = check(); + var letter = changeTemplate(template); + return { /** @@ -282,7 +281,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - return check(); + return ok; }, /** @@ -293,8 +292,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - if (check()) { - return (changeTemplate(template)); + if (ok) { + return (letter); } return ''; From 0cd2a7f2e2d9b07170708c2c40cf334a816ad719 Mon Sep 17 00:00:00 2001 From: reindeermur Date: Thu, 19 Oct 2017 20:45:14 +0500 Subject: [PATCH 07/10] Update robbery.js --- robbery.js | 261 ++++++++++++++++++++++++++--------------------------- 1 file changed, 129 insertions(+), 132 deletions(-) diff --git a/robbery.js b/robbery.js index 8ddb267..b4edaa3 100644 --- a/robbery.js +++ b/robbery.js @@ -15,6 +15,79 @@ const DAYS = { 'СР': MIN_IN_DAY * 3 }; +/** + * @param {Object} schedule – Расписание Банды + * @param {Number} duration - Время на ограбление в минутах + * @param {Object} workingHours – Время работы банка + * @param {String} workingHours.from – Время открытия, например, "10:00+5" + * @param {String} workingHours.to – Время закрытия, например, "18:00+5" + * @returns {Object} + */ +exports.getAppropriateMoment = function (schedule, duration, workingHours) { + //console.info(schedule, duration, workingHours); + var TIME_ZONE = getTimeZone(workingHours.from); + var bankWorkingHours = getTimeInterval(workingHours); + var gangSchedule = getWorkingSchedule(schedule, TIME_ZONE); + var possibleStarts = tryToGetTime(gangSchedule, bankWorkingHours, duration); + + var timeForCrime = formatList(createTimeList(possibleStarts, duration)); + var lastChance = timeForCrime.slice(-1); + + + function changeTemplate(template) { + let time; + if (isEmpty()) { + time = lastChance; + } else { + time = timeForCrime[0]; + timeForCrime.splice(0, 1); + } + return time; + } + + function isEmpty() { + return timeForCrime.length === 0; + } + + return { + + /** + * Найдено ли время + * @returns {Boolean} + */ + exists: function () { + return true; + }, + + /** + * Возвращает отформатированную строку с часами для ограбления + * Например, + * "Начинаем в %HH:%MM (%DD)" -> "Начинаем в 14:59 (СР)" + * @param {String} template + * @returns {String} + */ + format: function (template) { + let time = changeTemplate(template); + if (!isEmpty()) { + return template + .replace('%HH', time[1]) + .replace('%MM', time[2]) + .replace('%DD', time[0]); + } + return ''; + }, + + /** + * Попробовать найти часы для ограбления позже [*] + * @star + * @returns {Boolean} + */ + tryLater: function () { + return (!isEmpty); + } + }; +}; + function getTimeZone(time) { return parseInt((time.slice(6))); } @@ -27,11 +100,11 @@ function getHours(time) { return parseInt(time.slice(0, 2)); } -function convertToMinutes(time) { +function convertToMinutes(time, timeZone) { let minutes = 0; minutes += getHours(time) * MIN_IN_HOUR; minutes += getMinutes(time); - minutes += (workingZone - getTimeZone(time)) * MIN_IN_HOUR; + minutes += (timeZone - getTimeZone(time)) * MIN_IN_HOUR; return minutes; } @@ -47,12 +120,12 @@ function getDay(day) { return 0; } -function getTimeInterval(record) { +function getTimeInterval(record, timeZone) { let interval = []; let startTime = (record.from).split(' '); let endTime = (record.to).split(' '); - let start = convertToMinutes(startTime[1]); - let end = convertToMinutes(endTime[1]); + let start = convertToMinutes(startTime[1], timeZone); + let end = convertToMinutes(endTime[1], timeZone); start += getDay(startTime[0]); end += getDay(endTime[0]); interval.push(start); @@ -65,27 +138,19 @@ function compareElements(a, b) { return a[0] - b[0]; } -function getSchedule() { +function getSchedule(schedule, timeZone) { let reversedSchedule = []; for (let man of Object.keys(schedule)) { for (let record of schedule[man]) { - reversedSchedule.push(getTimeInterval(record)); + reversedSchedule.push(getTimeInterval(record, timeZone)); } } return reversedSchedule.sort(compareElements); } -function getMax(a, b) { - if (a > b) { - return a; - } - - return b; -} - -function getWorkingSchedule() { - let oldSchedule = getSchedule(); +function getWorkingSchedule(schedule, timeZone) { + let oldSchedule = getSchedule(schedule, timeZone); let resultSchedule = []; if (oldSchedule[0][0] >= 0) { resultSchedule.push([0, oldSchedule[0][0]]); @@ -107,6 +172,18 @@ function getWorkingSchedule() { } +function tryToGetTime(gangSchedule, bankSchedule, duration) { + var startCrime = []; + for (let i = 0; i < gangSchedule.length; i++) { + for (let j = 0; j < bankSchedule.length; j++) { + startCrime.push(checkInterval( + gangSchedule[i], bankSchedule[j], duration)); + } + } + + return startCrime; +} + function getMin(a, b) { if (a < b) { return a; @@ -157,17 +234,37 @@ function checkInterval(gang, bank, min) { return result; } -function tryToGetTime() { - let gangSchedule = getWorkingSchedule(); - var startCrime = []; - for (let i = 0; i < gangSchedule.length; i++) { - for (let j = 0; j < bankSchedule.length; j++) { - startCrime.push(checkInterval( - gangSchedule[i], bankSchedule[j], duration)); +function check(records) { + if (records.length !== 0) { + return true; + } + + return false; +} + +function showList(records, duration) { + let timelist = [];; + let n; + for (let i = 0; i < records.length; i++) { + n = (records[i][0]); + while ((n < records[i][1]) && + (records[i][1] - n >= duration)) { + timelist.push(n); + n += 30; + n += duration; } } - return startCrime; + return timelist; +} + +function createTimeList(records, duration) { + let timelist = []; + if (check(records)) { + timelist = showList(records, duration); + } + + return timelist; } function transform(number) { @@ -180,6 +277,7 @@ function transform(number) { return result; } + function changeFormate(number) { let time = []; let oldTime = number; @@ -196,116 +294,15 @@ function changeFormate(number) { let hours = (oldTime - (oldTime % MIN_IN_HOUR)) / MIN_IN_HOUR; let minutes = oldTime % MIN_IN_HOUR; time.push(transform (hours)); - time.pudh(transform(minutes)); + time.push(transform(minutes)); return time; } -function check() { - if (tryToGetTime().length !== 0) { - return true; - } - - return false; -} - -function showList() { - let timelist = []; - let timeInterval = tryToGetTime(); - let n; - for (let i = 0; i < timeInterval.length; i++) { - n = (timeInterval[i][0]); - while ((n < timeInterval[i][1]) && - (timeInterval[i][1] - n >= duration)) { - timelist.push(n); - n += 30; - n += duration; - } - } - - return timelist; -} - -function createTimeList() { - let timelist = []; - if (check()) { - timelist = showList(); - } - - return timelist; -} - -function changeTemplate(template) { - let time; - if (topList.length === 0) { - time = changeFormate(lastChance); - } else { - time = changeFormate(topList[0]); - topList.splice(0, 1); +function formatList(timeList) { + let result = []; + for (let i = 0; i < timeList.length; i++) { + result.push(changeFormate(timeList[i])); } - - return template - .replace('%HH', time[1]) - .replace('%MM', time[2]) - .replace('%DD', time[0]); + return result; } - -/** - * @param {Object} schedule – Расписание Банды - * @param {Number} duration - Время на ограбление в минутах - * @param {Object} workingHours – Время работы банка - * @param {String} workingHours.from – Время открытия, например, "10:00+5" - * @param {String} workingHours.to – Время закрытия, например, "18:00+5" - * @returns {Object} - */ -exports.getAppropriateMoment = function (schedule, duration, workingHours) { - //console.info(schedule, duration, workingHours); - var workingZone = getTimeZone(workingHours.from); - var startBank = convertToMinutes(workingHours.from); - var endBank = convertToMinutes(workingHours.to); - - var bankSchedule = [ - [startBank, endBank], - [startBank + DAYS.ПН, endBank + DAYS.ПН], - [startBank + DAYS.ВТ, endBank + DAYS.ВТ] - ]; - var topList = createTimeList(); - const lastChance = parseInt(topList.slice(-1)); - var ok = check(); - var letter = changeTemplate(template); - - return { - - /** - * Найдено ли время - * @returns {Boolean} - */ - exists: function () { - return ok; - }, - - /** - * Возвращает отформатированную строку с часами для ограбления - * Например, - * "Начинаем в %HH:%MM (%DD)" -> "Начинаем в 14:59 (СР)" - * @param {String} template - * @returns {String} - */ - format: function (template) { - if (ok) { - return (letter); - } - - return ''; - }, - - /** - * Попробовать найти часы для ограбления позже [*] - * @star - * @returns {Boolean} - */ - tryLater: function () { - return (topList.length === 0); - } - }; -}; From d37715e0754c2cc6d1706fcb55b8fe48dbbd68bf Mon Sep 17 00:00:00 2001 From: reindeermur Date: Thu, 19 Oct 2017 20:47:20 +0500 Subject: [PATCH 08/10] Update robbery.js --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index b4edaa3..a995e07 100644 --- a/robbery.js +++ b/robbery.js @@ -56,7 +56,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - return true; + return (!isEmpty); }, /** From ba55806d68afc19cc42c72a29c71247351cfe7a8 Mon Sep 17 00:00:00 2001 From: reindeermur Date: Thu, 19 Oct 2017 21:02:07 +0500 Subject: [PATCH 09/10] Update robbery.js --- robbery.js | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/robbery.js b/robbery.js index a995e07..b53060e 100644 --- a/robbery.js +++ b/robbery.js @@ -29,12 +29,10 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var bankWorkingHours = getTimeInterval(workingHours); var gangSchedule = getWorkingSchedule(schedule, TIME_ZONE); var possibleStarts = tryToGetTime(gangSchedule, bankWorkingHours, duration); - var timeForCrime = formatList(createTimeList(possibleStarts, duration)); var lastChance = timeForCrime.slice(-1); - - function changeTemplate(template) { + function changeTemplate() { let time; if (isEmpty()) { time = lastChance; @@ -42,6 +40,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { time = timeForCrime[0]; timeForCrime.splice(0, 1); } + return time; } @@ -67,13 +66,14 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - let time = changeTemplate(template); + let time = changeTemplate(); if (!isEmpty()) { return template .replace('%HH', time[1]) .replace('%MM', time[2]) .replace('%DD', time[0]); } + return ''; }, @@ -149,6 +149,14 @@ function getSchedule(schedule, timeZone) { return reversedSchedule.sort(compareElements); } +function getMax(a, b) { + if (a > b) { + return a; + } + + return b; +} + function getWorkingSchedule(schedule, timeZone) { let oldSchedule = getSchedule(schedule, timeZone); let resultSchedule = []; @@ -192,28 +200,28 @@ function getMin(a, b) { return b; } -function checkLeft(a, b, c, d) { +function checkLeft(a, b, duration) { let result = []; - if ((a <= c) && (b > c)) { - if (((b < d) && (b - c >= duration)) || - ((b >= d) && (d - c >= duration))) { + if ((a[0] <= b[0]) && (a[1] > b[0])) { + if (((a[1] < b[1]) && (a[1] - b[0] >= duration)) || + ((a[1] >= b[1]) && (b[1] - b[0] >= duration))) { - result.push(c); - result.push(getMin(d, b)); + result.push(b[0]); + result.push(getMin(a[1], b[1])); } } return result; } -function checkRight(a, b, c, d) { +function checkRight(a, b, duration) { let result = []; - if ((c <= a) && (d > a)) { - if (((b < d) && (b - a >= duration)) || - ((b >= d) && (d - a >= duration))) { + if ((b[0] <= a[0]) && (b[1] > a[0])) { + if (((a[1] < d) && (a[1] - a[0] >= duration)) || + ((a[1] >= b[1]) && (b[1] - a[0] >= duration))) { - result.push(a); - result.push(getMin(d, b)); + result.push(a[0]); + result.push(getMin(a[1], b[1])); } } @@ -222,8 +230,8 @@ function checkRight(a, b, c, d) { function checkInterval(gang, bank, min) { let result = []; - let left = checkLeft(gang[0], gang[1], bank[0], bank[1], min); - let right = checkRight(gang[0], gang[1], bank[0], bank[1], min); + let left = checkLeft(gang, bank, min); + let right = checkRight(gang, bank, min); if (left.length !== 0) { result = left; } @@ -243,7 +251,7 @@ function check(records) { } function showList(records, duration) { - let timelist = [];; + let timelist = []; let n; for (let i = 0; i < records.length; i++) { n = (records[i][0]); @@ -304,5 +312,6 @@ function formatList(timeList) { for (let i = 0; i < timeList.length; i++) { result.push(changeFormate(timeList[i])); } + return result; } From 81b48c2055e66ebe62900e556ff109b984a9882c Mon Sep 17 00:00:00 2001 From: reindeermur Date: Thu, 19 Oct 2017 21:04:50 +0500 Subject: [PATCH 10/10] Update robbery.js --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index b53060e..7a9e963 100644 --- a/robbery.js +++ b/robbery.js @@ -24,7 +24,7 @@ const DAYS = { * @returns {Object} */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - //console.info(schedule, duration, workingHours); + // console.info(schedule, duration, workingHours); var TIME_ZONE = getTimeZone(workingHours.from); var bankWorkingHours = getTimeInterval(workingHours); var gangSchedule = getWorkingSchedule(schedule, TIME_ZONE); @@ -217,7 +217,7 @@ function checkLeft(a, b, duration) { function checkRight(a, b, duration) { let result = []; if ((b[0] <= a[0]) && (b[1] > a[0])) { - if (((a[1] < d) && (a[1] - a[0] >= duration)) || + if (((a[1] < b[1]) && (a[1] - a[0] >= duration)) || ((a[1] >= b[1]) && (b[1] - a[0] >= duration))) { result.push(a[0]);