Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Simple solution solution in Clear category for Lightbulb More by khudr
from collections import defaultdict
from datetime import datetime, timedelta
from typing import List, Optional, Union, Tuple
def count_sum_light(els: List[datetime]) -> int:
return sum((els[i + 1] - els[i]).total_seconds() for i in range(0, len(els), 2))
def reduce_on_end(els: List[datetime], end_watching: Optional[datetime] = None) -> int:
if not end_watching:
return count_sum_light(els)
for i in range(0, len(els), 2):
if els[i] >= end_watching:
return count_sum_light(els[:i])
if i + 1 == len(els):
return int(count_sum_light(els[:i]) + (end_watching - els[i]).total_seconds())
if els[i] < end_watching < els[i + 1]:
return int((end_watching - els[i]).total_seconds() + count_sum_light(els[:i]))
return count_sum_light(els)
def reduce(
els: List[datetime],
start_watching: Optional[datetime] = None,
end_watching: Optional[datetime] = None,
) -> int:
if not start_watching:
return reduce_on_end(els, end_watching)
for i in range(0, len(els), 2):
if els[i] >= start_watching:
return reduce_on_end(els[i:], end_watching)
if i + 1 == len(els):
return reduce_on_end([start_watching], end_watching)
if els[i + 1] > start_watching:
return int((els[i + 1] - start_watching).total_seconds() + reduce_on_end(els[i + 2:], end_watching))
return 0
def sum_lamp_value(
els: List[Tuple[datetime, int]],
start_watching: Optional[datetime] = None,
end_watching: Optional[datetime] = None,
req: Optional[int] = None
) -> int:
if req is None:
req = 1
active = []
total_light = []
for el in els:
if el[1] in active:
active.remove(el[1])
# проверяем выключился ли свет
if len(active) == req - 1:
total_light.append(el[0])
else:
active.append(el[1])
# включение света
if len(active) == req:
total_light.append(el[0])
return reduce(total_light, start_watching, end_watching)
def group_by_second_el(items):
grouped_items = defaultdict(list)
for date, value in items:
grouped_items[value].append(date)
return [(value, dates) for value, dates in grouped_items.items()]
def sum_light(
els: List[Union[datetime, Tuple[datetime, int]]],
start_watching: Optional[datetime] = None,
end_watching: Optional[datetime] = None,
operating: Optional[timedelta] = None,
req: Optional[int] = None
) -> int:
for el in els:
if isinstance(el, datetime):
els[els.index(el)] = (el, 1)
if not operating:
els.sort(key=lambda el: el[0])
return sum_lamp_value(els, start_watching, end_watching, req)
grouped = group_by_second_el(els)
for lamp in grouped:
times = lamp[1]
cur_time_delta = timedelta()
is_went_out = False
for i in range(0, len(times), 2):
if is_went_out:
els.remove((times[i], lamp[0]))
if i + 1 != len(times):
els.remove((times[i + 1], lamp[0]))
elif i + 1 != len(times):
local_time_delta = (times[i + 1] - times[i])
if local_time_delta > operating - cur_time_delta:
delt = (operating - cur_time_delta)
els[els.index((times[i + 1], lamp[0]))] = (times[i] + delt, lamp[0])
is_went_out = True
cur_time_delta += local_time_delta
else:
els.append((times[i] + (operating - cur_time_delta), lamp[0]))
els.sort(key=lambda el: el[0])
return sum_lamp_value(els, start_watching, end_watching, req)
March 3, 2024
Comments: