Had a particularly long day at work, so got to the problem late and only just now finished. I'm with all of you that this one was less fun...I haven't refactored since I don't have much brain left for today, but here's my solution for now!
#!/usr/bin/env python
importoperatorimportrefromcollectionsimportdefaultdictRECORD_PATTERN=re.compile('\[.* \d\d:(\d\d)\] .*')GUARD_ID_PATTERN=re.compile('.* Guard #(\d+) begins shift')if__name__=='__main__':withopen('input.txt')asguard_records_file:guard_records=guard_records_file.read().splitlines()guard_records.sort()minute_data=defaultdict(lambda:defaultdict(int))guard_data=defaultdict(lambda:defaultdict(int))forguard_recordinguard_records:minute=int(RECORD_PATTERN.match(guard_record).group(1))if'begins shift'inguard_record:just_started_shift=Truecurrent_guard=int(GUARD_ID_PATTERN.match(guard_record).group(1))elif'wakes up'inguard_record:minutes_asleep=minute-fell_asleep_minuteforsleep_minuteinrange(fell_asleep_minute,minute):guard_data[current_guard][sleep_minute]+=1minute_data[sleep_minute][current_guard]+=1elif'falls asleep'inguard_record:fell_asleep_minute=minute# Part 1
guard_who_slept_most=max(guard_data,key=lambdakey:sum(guard_data[key].values()))sleepiest_minute_for_guard=max(minute_data,key=lambdakey:minute_data[key][guard_who_slept_most])print(guard_who_slept_most*sleepiest_minute_for_guard)# Part 2
guard_with_sleepiest_minute=max(guard_data,key=lambdakey:max(guard_data[key].values()))sleepiest_minute_for_guard=max(guard_data[guard_with_sleepiest_minute],key=lambdakey:guard_data[guard_with_sleepiest_minute][key])print(guard_with_sleepiest_minute*sleepiest_minute_for_guard)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Had a particularly long day at work, so got to the problem late and only just now finished. I'm with all of you that this one was less fun...I haven't refactored since I don't have much brain left for today, but here's my solution for now!