Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Bit Manipulation solution in Clear category for Multiple Lightbulbs by libertypi
from datetime import datetime
from typing import List, Optional, Union, Tuple
def _index_after(combine: List[datetime], t: datetime):
return next((i for i, o in enumerate(combine) if o > t), None)
def sum_light(els: List[Union[datetime, Tuple[datetime, int]]],
start_watching: Optional[datetime] = None,
end_watching: Optional[datetime] = None) -> int:
"""
how long the light bulb has been turned on
"""
for i, obj in enumerate(els):
if isinstance(obj, datetime):
els[i] = obj, 1
els.sort()
combine = []
stats = 0
for dt, i in els:
# checking the bit
mask = 1 << i
stat = stats & mask
if stat:
# turning off by clearing the bit
stats &= ~mask
if not stats:
# all lights are off, this is the "off"
combine.append(dt)
else:
if not stats:
# all lights are off, this is the "on"
combine.append(dt)
# turning on by setting the bit
stats |= mask
if end_watching:
i = _index_after(combine, end_watching)
if i is not None:
combine = combine[:i]
if len(combine) % 2:
combine.append(end_watching)
if start_watching:
i = _index_after(combine, start_watching)
if i is None:
# starts after the last datetime
return 0
if i == 0:
# starts before the first datetime
pass
elif i % 2:
# starts in "on" period
combine = combine[i - 1:]
combine[0] = start_watching
else:
# starts in "off" period
combine = combine[i:]
return sum((combine[i + 1] - combine[i]).total_seconds()
for i in range(0, len(combine) - 1, 2))
if __name__ == '__main__':
print("Example:")
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),
]) == 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, 10),
(datetime(2015, 1, 12, 10, 0, 0), 2),
(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, 10),
(datetime(2015, 1, 12, 10, 0, 0), 2),
(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, 10),
(datetime(2015, 1, 12, 10, 0, 0), 2),
(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, 10),
(datetime(2015, 1, 12, 10, 0, 0), 2),
], 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
print("The forth mission in series is completed? Click 'Check' to earn cool rewards!")
Jan. 16, 2021
Comments: