-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday04.py
37 lines (30 loc) · 940 Bytes
/
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
#!/usr/bin/env python
import sys, itertools, collections
mins = collections.defaultdict(lambda: [0]*60)
lines = []
current_id = None
for line in sys.stdin:
line = line.strip()
lines.append(line)
lines.sort()
for line in lines:
if 'begins shift' in line:
a, b = line.split('#')
c = b.split(' ')
current_id = int(c[0])
else:
t = int(line[15:17])
if 'falls asleep' in line:
sleep = t
elif 'wakes up' in line:
wake = t
print current_id, "sleeping", sleep, wake
for i in xrange(sleep, wake):
mins[current_id][i] += 1
guard_id = max(mins, key = lambda x: sum(mins[x]))
counts = mins[guard_id]
minute = max(xrange(len(counts)), key = lambda x: counts[x])
print guard_id, minute, guard_id*minute
a = max([(mins[guard_id][i], guard_id, i) for guard_id in mins for i in xrange(60)])
print a
print a[1]*a[2]