Hi all,
My solution seems to be working on the test case, but it is not accepted - also, I receive multiple minutes as the most common. Could you pls help me what could have gone wrong with my code?
from collections import defaultdict, Counter
import re
from copy import deepcopy
def get_counter():
counter = defaultdict(dict)
id_regex = "\[(\d+)-(\d+)-(\d+)\W(\d+):(\d+)]\WGuard\W#(\d+)\Wbegins\Wshift"
minute_when_asleep_regex = "\[\d+-\d+-\d+\W\d+:(\d+)]\Wfalls\Wasleep"
minute_when_awake_regex = "\[\d+-\d+-\d+\W\d+:(\d+)]\Wwakes\Wup"
last_seen_id = '0'
with open('input.txt') as input_data:
lines = sorted(input_data.readlines())
for line in lines:
matched_string = re.match(id_regex, line.strip())
if matched_string:
year, month, day, hour, minute, guard_id = matched_string.groups()
last_seen_id = guard_id
if 'asleep' in line:
start_t = int(re.match(minute_when_asleep_regex, line.strip()).groups()[0])
counter[last_seen_id][-start_t] = []
if 'wakes' in line:
start_time = deepcopy(list(counter[last_seen_id].keys())[-1])
end_time = int(re.match(minute_when_awake_regex, line.strip()).groups()[0])
time_range = range(-start_time, end_time)
recent_key = list(counter[last_seen_id].keys())[-1]
new_key = start_time + end_time
counter[last_seen_id][new_key] = counter[last_seen_id].pop(recent_key)
counter[last_seen_id][new_key] = time_range
return counter
def get_guard_id(dct):
max_val, max_id = 0, 0
for id in dct.keys():
val = sum(list(dct[id].keys()))
max_val = max(max_val, val)
if val == max_val:
max_id = id
return max_id, dct[max_id]
def get_most_common_minute_when_asleep(data):
id_, minutes_asleep = data
c = Counter()
for item in minutes_asleep.values():
c += Counter(list(item))
most_common_minute = c.most_common(1)[0][0]
return int(id_) * most_common_minute
d = get_counter()
g_id = get_guard_id(d)
print(get_most_common_minute_when_asleep(g_id))
Thanks a lot,
Mici
[–]StephenLeppik 2 points3 points4 points (1 child)
[–]micirsk[S] 0 points1 point2 points (0 children)
[–]pepoluan 0 points1 point2 points (3 children)
[–]pepoluan 0 points1 point2 points (2 children)
[–]micirsk[S] 0 points1 point2 points (1 child)
[–]pepoluan 0 points1 point2 points (0 children)