Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
4 lines, weird solution in Creative category for Multiple Lightbulbs by CDG.Axel
from datetime import datetime
from typing import List, Optional, Union, Tuple
def sum_light(els: List[Union[datetime, Tuple[datetime, int]]],
start_watching: Optional[datetime] = datetime(1,1,1),
end_watching: Optional[datetime] = datetime(9999,12,31)) -> int:
"""
how long the light bulb has been turned on
"""
sw, on = set(), []
for t, s in sorted(e if type(e) is tuple else (e, 0) for e in els):
on.extend([min(end_watching, max(t, start_watching))] * (not all([sw, (sw := sw ^ {s})])))
return sum((b-a).total_seconds() for a, b in zip(on[0::2], on[1::2]+[end_watching]))
Nov. 27, 2021
Comments: