Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Lightbulb Operating by mortonfox
from itertools import chain, groupby
from datetime import datetime, timedelta
from typing import List, Optional, Union, Tuple
# Process the bulb activity list so that all bulb numbers are specified
def process_activity(els):
result = []
for el in els:
try:
dt, bulb = el
except:
dt, bulb = el, 1
result.append((dt, bulb))
return result
# Get lit periods for each bulb.
def get_bulb_periods(els):
els.sort(key=lambda el: (el[1], el[0]))
bulb_periods = {}
for bulb, activity in groupby(els, key=lambda el: el[1]):
act_times = list(dt for dt, _ in activity)
if len(act_times) % 2:
# If there is an unclosed period, close it with the max datetime.
act_times.append(datetime(9999, 12, 31, 23, 59, 59))
bulb_periods[bulb] = [tuple(act_times[i:i+2]) for i in range(0, len(act_times), 2)]
return bulb_periods
# Cut off bulb lit periods at the operating time.
def trunc_bulbs(bulb_periods, operating):
for bulb, periods in bulb_periods.items():
periods2 = []
remaining = operating
for pstart, pend in periods:
tdelta = pend - pstart
if remaining <= tdelta:
periods2.append((pstart, pstart + remaining))
break
periods2.append((pstart, pend))
remaining -= tdelta
bulb_periods[bulb] = periods2
# Merge all the bulb lit periods into a single list of lit periods.
def merge_lit_periods(bulb_periods):
lit_periods = sorted(chain.from_iterable(bulb_periods.values()), key=lambda period: period[0])
merged = []
for pstart, pend in lit_periods:
if not merged or merged[-1][1] < pstart:
merged.append((pstart, pend))
else:
merged[-1] = (merged[-1][0], max(merged[-1][1], pend))
return merged
# Cut off list of lit periods at start_watching time.
def trunc_lit_start(lit_periods, start_watching):
lit2 = [(pstart, pend) for pstart, pend in lit_periods if pend > start_watching]
if lit2:
lit2[0] = (max(start_watching, lit2[0][0]), lit2[0][1])
return lit2
# Cut off list of lit periods at end_watching time.
def trunc_lit_end(lit_periods, end_watching):
lit2 = [(pstart, pend) for pstart, pend in lit_periods if pstart < end_watching]
if lit2:
lit2[-1] = (lit2[-1][0], min(end_watching, lit2[-1][1]))
return lit2
def sum_light(els: List[Union[datetime, Tuple[datetime, int]]],
start_watching: Optional[datetime] = None,
end_watching: Optional[datetime] = None,
operating: Optional[timedelta] = None) -> int:
"""
how long the light bulb has been turned on
"""
els = process_activity(els)
bulb_periods = get_bulb_periods(els)
if operating:
trunc_bulbs(bulb_periods, operating)
lit_periods = merge_lit_periods(bulb_periods)
if start_watching:
lit_periods = trunc_lit_start(lit_periods, start_watching)
if end_watching:
lit_periods = trunc_lit_end(lit_periods, end_watching)
# And after all that, it's now just a sum of the time intervals.
return sum((pend - pstart for pstart, pend in lit_periods), start=timedelta(seconds=0)).total_seconds()
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. 26, 2021