Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Lightbulb End Watching by pokosasa
import assert from "assert";
function sumLight(els: Date[], startWatching?: Date, endWatching?: Date): number {
if (els.length % 2 == 1) els.push(new Date(9999,11,31));
if (startWatching) {
els = els.filter(e => startWatching <= e);
if (els.length % 2 == 1) els.unshift(startWatching);
}
if (endWatching) {
els = els.filter(e => e <= endWatching);
if (els.length % 2 == 1) els.push(endWatching);
}
let res = 0;
for (let i = 0; i < els.length - 1; i+=2) res += +els[i + 1] - +els[i];
return Math.floor(res / 1000)
}
console.log("Example:");
console.log(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 0, 10),
],
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 0, 10)));
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 0, 10),
],
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 0, 10)), 10);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 0, 10),
],
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 0, 7)), 7);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 0, 10),
],
new Date(2015, 1, 12, 10, 0, 3),
new Date(2015, 1, 12, 10, 0, 10)), 7);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 0, 10),
],
new Date(2015, 1, 12, 10, 0, 10),
new Date(2015, 1, 12, 10, 0, 20)), 0);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 10, 10),
new Date(2015, 1, 12, 11, 0, 0),
new Date(2015, 1, 12, 11, 10, 10),
],
new Date(2015, 1, 12, 10, 30, 0),
new Date(2015, 1, 12, 11, 0, 0)), 0);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 10, 10),
new Date(2015, 1, 12, 11, 0, 0),
new Date(2015, 1, 12, 11, 10, 10),
],
new Date(2015, 1, 12, 10, 10, 0),
new Date(2015, 1, 12, 11, 0, 0)), 10);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 10, 10),
new Date(2015, 1, 12, 11, 0, 0),
new Date(2015, 1, 12, 11, 10, 10),
],
new Date(2015, 1, 12, 10, 10, 0),
new Date(2015, 1, 12, 11, 0, 10)), 20);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 10, 10),
new Date(2015, 1, 12, 11, 0, 0),
new Date(2015, 1, 12, 11, 10, 10),
],
new Date(2015, 1, 12, 9, 50, 0),
new Date(2015, 1, 12, 10, 0, 10)), 10);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 10, 10),
new Date(2015, 1, 12, 11, 0, 0),
new Date(2015, 1, 12, 11, 10, 10),
],
new Date(2015, 1, 12, 9, 0, 0),
new Date(2015, 1, 12, 10, 5, 0)), 300);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 10, 10),
new Date(2015, 1, 12, 11, 0, 0),
new Date(2015, 1, 12, 11, 10, 10),
],
new Date(2015, 1, 12, 11, 5, 0),
new Date(2015, 1, 12, 12, 0, 0)), 310);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 10, 10),
new Date(2015, 1, 12, 11, 0, 0),
],
new Date(2015, 1, 12, 11, 5, 0),
new Date(2015, 1, 12, 11, 10, 0)), 300);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 10, 10),
new Date(2015, 1, 12, 11, 0, 0),
],
new Date(2015, 1, 12, 10, 10, 0),
new Date(2015, 1, 12, 11, 0, 10)), 20);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
new Date(2015, 1, 12, 10, 10, 10),
new Date(2015, 1, 12, 11, 0, 0),
],
new Date(2015, 1, 12, 9, 10, 0),
new Date(2015, 1, 12, 10, 20, 20)), 610);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
],
new Date(2015, 1, 12, 9, 10, 0),
new Date(2015, 1, 12, 10, 20, 20)), 1220);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
],
new Date(2015, 1, 12, 9, 9, 0),
new Date(2015, 1, 12, 10, 0, 0)), 0);
assert.equal(sumLight([
new Date(2015, 1, 12, 10, 0, 0),
],
new Date(2015, 1, 12, 9, 9, 0),
new Date(2015, 1, 12, 10, 0, 10)), 10);
console.log("The third mission in series is completed? Click 'Check' to earn cool rewards!");
March 28, 2021
Comments: