Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Lightbulb Operating by oduvan
from datetime import datetime, timedelta
from typing import List, Optional, Union
from itertools import zip_longest
SECONDS_IN_DAY = 60 * 60 * 24
def get_periods(start_watching, end_watching, els, operating):
for start, end in zip_longest(els[::2], els[1::2], fillvalue=end_watching):
if start_watching > end:
continue
period = (min(max(start, start_watching), end_watching), min(end, end_watching))
if operating is None:
yield period
else:
period = (period[0], min(period[1], period[0] + operating))
yield period
operating -= period[1] - period[0]
if not operating:
break
def unite_timelines(timelines):
periods = sum(timelines, [])
periods = sorted(periods, key=lambda a: a[0])
central = periods.pop(0)
while periods:
cur = periods.pop(0)
if cur[0] > central[1]:
yield central
central = cur
continue
central = (min(central[0], cur[0]), max(central[1], cur[1]))
yield central
def periods_to_seconds(periods):
return sum([(t[1] - t[0]).seconds + (t[1] - t[0]).days * SECONDS_IN_DAY for t in periods])
def get_timelines(els):
from collections import defaultdict
timelines = defaultdict(list)
for el in els:
if isinstance(el, datetime):
timelines[0].append(el)
else:
timelines[el[1]].append(el[0])
return timelines.values()
def sum_light(els: List[Union[datetime,tuple[datetime, int]]],
start_watching: datetime = datetime.min,
end_watching: datetime = datetime.max,
operating: Optional[timedelta] = None) -> int:
timelines = get_timelines(els)
periods = [list(get_periods(start_watching, end_watching, els, operating)) for els in timelines]
periods = unite_timelines(periods)
return periods_to_seconds(periods)
if __name__ == '__main__':
print("Example:")
print(sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
],
start_watching=datetime(2015, 1, 12, 10, 0, 10),
end_watching=datetime(2015, 1, 12, 10, 0, 30),
operating=timedelta(seconds=5)))
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
(datetime(2015, 1, 12, 10, 0, 0), 2),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 10, 1, 0), 2),
]) == 60
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 11, 0, 0), 2),
(datetime(2015, 1, 12, 11, 1, 0), 2),
]) == 70
assert sum_light([
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
]) == 30
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
]) == 40
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
(datetime(2015, 1, 12, 10, 1, 0), 3),
(datetime(2015, 1, 12, 10, 1, 20), 3),
]) == 60
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
(datetime(2015, 1, 12, 10, 0, 0), 2),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 10, 1, 0), 2),
], datetime(2015, 1, 12, 10, 0, 50)) == 10
assert sum_light([
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 30)) == 20
assert sum_light([
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 20)) == 30
assert sum_light([
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 10)) == 30
assert sum_light([
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 50)) == 0
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 30)) == 20
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 20)) == 30
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
(datetime(2015, 1, 12, 10, 1, 20), 2),
(datetime(2015, 1, 12, 10, 1, 40), 2),
], datetime(2015, 1, 12, 10, 0, 20)) == 50
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
(datetime(2015, 1, 12, 10, 0, 0), 2),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 10, 1, 0), 2),
], datetime(2015, 1, 12, 10, 0, 30), datetime(2015, 1, 12, 10, 1, 0)) == 30
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
(datetime(2015, 1, 12, 10, 0, 0), 2),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 10, 1, 0), 2),
], datetime(2015, 1, 12, 10, 0, 20), datetime(2015, 1, 12, 10, 1, 0)) == 40
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
(datetime(2015, 1, 12, 10, 0, 0), 2),
datetime(2015, 1, 12, 10, 0, 10),
], datetime(2015, 1, 12, 10, 0, 0), datetime(2015, 1, 12, 10, 0, 30)) == 30
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 0), datetime(2015, 1, 12, 10, 1, 0)) == 40
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 0), datetime(2015, 1, 12, 10, 0, 10)) == 0
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
], datetime(2015, 1, 12, 10, 0, 10), datetime(2015, 1, 12, 10, 0, 20)) == 10
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
], datetime(2015, 1, 12, 10, 0, 10), datetime(2015, 1, 12, 10, 0, 20)) == 10
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
], datetime(2015, 1, 12, 10, 0, 10), datetime(2015, 1, 12, 10, 0, 30)) == 20
assert sum_light(els=[
(datetime(2015, 1, 11, 0, 0, 0), 3),
datetime(2015, 1, 12, 0, 0, 0),
(datetime(2015, 1, 13, 0, 0, 0), 3),
(datetime(2015, 1, 13, 0, 0, 0), 2),
datetime(2015, 1, 14, 0, 0, 0),
(datetime(2015, 1, 15, 0, 0, 0), 2),
], start_watching=datetime(2015, 1, 10, 0, 0, 0), end_watching=datetime(2015, 1, 16, 0, 0, 0)) == 345600
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
], operating=timedelta(seconds=100)) == 10
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
], operating=timedelta(seconds=5)) == 5
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 10),
(datetime(2015, 1, 12, 10, 0, 0), 2),
(datetime(2015, 1, 12, 10, 1, 0), 2),
], operating=timedelta(seconds=100)) == 60
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 30),
(datetime(2015, 1, 12, 10, 0, 30), 2),
(datetime(2015, 1, 12, 10, 1, 0), 2),
], operating=timedelta(seconds=100)) == 60
assert sum_light([
datetime(2015, 1, 12, 10, 0, 0),
datetime(2015, 1, 12, 10, 0, 30),
(datetime(2015, 1, 12, 10, 0, 30), 2),
(datetime(2015, 1, 12, 10, 1, 0), 2),
], operating=timedelta(seconds=20)) == 40
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
(datetime(2015, 1, 12, 10, 1, 0), 3),
(datetime(2015, 1, 12, 10, 1, 20), 3),
], operating=timedelta(seconds=10)) == 30
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
(datetime(2015, 1, 12, 10, 1, 20), 2),
(datetime(2015, 1, 12, 10, 1, 40), 2),
], start_watching=datetime(2015, 1, 12, 10, 0, 20), operating=timedelta(seconds=100)) == 50
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
datetime(2015, 1, 12, 10, 0, 40),
(datetime(2015, 1, 12, 10, 0, 50), 2),
(datetime(2015, 1, 12, 10, 1, 20), 2),
(datetime(2015, 1, 12, 10, 1, 40), 2),
], start_watching=datetime(2015, 1, 12, 10, 0, 20), operating=timedelta(seconds=10)) == 20
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
], start_watching=datetime(2015, 1, 12, 10, 0, 10), end_watching=datetime(2015, 1, 12, 10, 0, 30),
operating=timedelta(seconds=20)) == 20
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
], start_watching=datetime(2015, 1, 12, 10, 0, 10), end_watching=datetime(2015, 1, 12, 10, 0, 30),
operating=timedelta(seconds=10)) == 20
assert sum_light([
(datetime(2015, 1, 12, 10, 0, 10), 3),
datetime(2015, 1, 12, 10, 0, 20),
(datetime(2015, 1, 12, 10, 0, 30), 3),
(datetime(2015, 1, 12, 10, 0, 30), 2),
], start_watching=datetime(2015, 1, 12, 10, 0, 10), end_watching=datetime(2015, 1, 12, 10, 0, 30),
operating=timedelta(seconds=5)) == 10
print("The forth mission in series is completed? Click 'Check' to earn cool rewards!")
Jan. 18, 2021
Comments: