Created
December 5, 2018 20:56
-
-
Save danellis/60b4e8d7e739e119ef393023d66dc1f8 to your computer and use it in GitHub Desktop.
AoC 2018 4 part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
# [1518-02-19 00:58] wakes up | |
# [1518-04-08 00:00] Guard #2423 begins shift | |
# [1518-06-10 00:05] falls asleep | |
regex = re.compile(r'\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.+)') | |
class Time: | |
def __init__(self, year, month, day, hour, minute): | |
self.year = int(year) | |
self.month = int(month) | |
self.day = int(day) | |
self.hour = int(hour) | |
self.minute = int(minute) | |
def numeric(self): | |
return ( | |
self.year * 60 * 24 * 30 * 12 + | |
self.month * 60 * 24 * 30 + | |
self.day * 60 * 24 + | |
self.hour * 60 + | |
self.minute | |
) | |
class Event: | |
def __init__(self, time, guard): | |
self.time = time | |
self.guard = guard | |
class Begin(Event): | |
def __str__(self): | |
return "<Event: [{}-{}-{} {}:{}] guard {} begins>".format( | |
self.time.year, self.time.month, self.time.day, | |
self.time.hour, self.time.minute, | |
self.guard | |
) | |
class Sleep(Event): | |
def __str__(self): | |
return "<Event: [{}-{}-{} {}:{}] guard {} sleeps>".format( | |
self.time.year, self.time.month, self.time.day, | |
self.time.hour, self.time.minute, | |
self.guard | |
) | |
class Wake(Event): | |
def __str__(self): | |
return "<Event: [{}-{}-{} {}:{}] guard {} wakes>".format( | |
self.time.year, self.time.month, self.time.day, | |
self.time.hour, self.time.minute, | |
self.guard | |
) | |
def parse_event(log_entry, guard): | |
year, month, day, hour, minute, event_string = regex.match(log_entry).groups() | |
time = Time(year, month, day, hour, minute) | |
if event_string == 'wakes up': | |
event = Wake(time, guard) | |
elif event_string == 'falls asleep': | |
event = Sleep(time, guard) | |
else: | |
guard = event_string.split(' ')[1][1:] | |
event = Begin(time, guard) | |
return event | |
input = open('input', 'r') | |
guard = None | |
raw_events = list(input) | |
raw_events.sort(key=lambda e: e[1:18]) | |
events = [] | |
sleep_times = {} | |
sleep_minutes = {} | |
sleep_start = None | |
for entry in raw_events: | |
event = parse_event(entry, guard) | |
guard = event.guard | |
if isinstance(event, Sleep): | |
sleep_start = event.time.minute | |
elif isinstance(event, Wake): | |
previous_sleep_time = sleep_times.get(guard, 0) | |
sleep_times[guard] = previous_sleep_time + event.time.minute - sleep_start | |
if guard not in sleep_minutes: | |
sleep_minutes[guard] = [0] * 60 | |
minutes = sleep_minutes[guard] | |
for minute in range(sleep_start, event.time.minute): | |
minutes[minute] += 1 | |
sleep_minutes[guard] = minutes | |
sleepiest_guard = max(sleep_times.keys(), key=(lambda key: sleep_times[key])) | |
sleepiest_minute = max(range(0, 60), key=(lambda key: sleep_minutes[sleepiest_guard][key])) | |
print(int(sleepiest_guard) * sleepiest_minute) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment