-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.py
88 lines (64 loc) · 2.32 KB
/
day04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from collections import defaultdict, Counter
import datetime
import re
a = [
# DAY 4 puzzle input snipped
]
def get_time(x):
time_field = ' '.join(x.split(' ')[0:2])
return datetime.datetime.strptime(time_field, '[%Y-%m-%d %H:%M]')
sorted_a = sorted(a, key=get_time)
starters = filter(lambda x: x.endswith('begins shift'), sorted_a)
sleeping_events = filter(lambda x: not x.endswith('begins shift'), sorted_a)
guards = defaultdict(dict)
shifts = {}
for starter in starters:
pattern = '(#[\d]+)'
x = re.search(pattern, starter)
guard = x.group(0)
the_datetime = get_time(starter)
the_date = datetime.datetime.date(the_datetime)
if the_datetime.hour >= 20:
the_date += datetime.timedelta(days=1)
guards[guard][the_date] = []
shifts[the_date] = guard
sleeping_events_iter = iter(sleeping_events)
for sleeping_event in sleeping_events_iter:
waking_event = next(sleeping_events_iter)
time_of_guard = get_time(sleeping_event)
sleep_min = time_of_guard.minute
waking_min = get_time(waking_event).minute
event_date = datetime.datetime.date(time_of_guard)
guard = shifts[event_date]
guards[guard][event_date].append((sleep_min, waking_min))
min_asleep = defaultdict(lambda: 0)
for guard in guards:
for date_ in guards[guard]:
for interval in guards[guard][date_]:
min_asleep[guard] += (interval[1] - interval[0])
def get_most_common_minute(guard):
all_minutes = []
for date_datum in guards[guard]:
for interval_datum in guards[guard][date_datum]:
all_minutes.extend(list(range(interval_datum[0], interval_datum[1])))
count = Counter(all_minutes)
try:
returning_value = count.most_common(1)[0]
except IndexError:
returning_value = (None, 0)
return returning_value
# PART 1
max_guard = Counter(min_asleep).most_common(1)[0][0]
most_common = get_most_common_minute(max_guard)[0]
print(int(max_guard.lstrip('#')) * most_common)
# PART 2
all_guard_minutes = {}
for guard in guards:
all_guard_minutes[guard] = get_most_common_minute(guard)
max_guard = None
max_minutes = 0
for guard in all_guard_minutes:
if all_guard_minutes[guard][1] > max_minutes:
max_minutes = all_guard_minutes[guard][1]
max_guard = guard
print(int(max_guard.lstrip('#')) * all_guard_minutes[max_guard][0])