Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Speedy category for Lightbulb End Watching by flatline
# Taken from mission Lightbulb Start Watching
# Taken from mission Lightbulb Intro
from datetime import datetime
from typing import List, Optional
def light_time(on, off, start_watching=None, end_watching=None):
if start_watching:
if off < start_watching:
return off - off
if on < start_watching:
on = start_watching
if end_watching:
if on > end_watching:
return on - on
if off > end_watching:
off = end_watching
return off - on
def sum_light(els: List[datetime], start_watching: Optional[datetime] = None, end_watching: Optional[datetime] = None) -> int:
"""
how long the light bulb has been turned on
"""
start_watching = start_watching or els[0]
els = els + [datetime.max]
return sum(
light_time(on, off,
start_watching=start_watching,
end_watching=end_watching,
).total_seconds()
for on, off
in zip(
els[::2],
els[1::2],
)
)
Feb. 5, 2021
Comments: