|
@@ -0,0 +1,21 @@
|
|
1
|
+from collections import defaultdict, Counter
|
|
2
|
+
|
|
3
|
+sleeps = defaultdict(Counter)
|
|
4
|
+
|
|
5
|
+with open('data/04.txt', 'r') as file:
|
|
6
|
+ guard = sleep = 0
|
|
7
|
+
|
|
8
|
+ for log in sorted(line.strip() for line in file):
|
|
9
|
+ action = log[-5:]
|
|
10
|
+ if action == 'shift':
|
|
11
|
+ guard = log[26:-13]
|
|
12
|
+ elif action == 'sleep':
|
|
13
|
+ sleep = int(log[15:17])
|
|
14
|
+ else:
|
|
15
|
+ sleeps[guard].update(range(sleep, int(log[15:17])))
|
|
16
|
+
|
|
17
|
+most_sleep = max(sleeps.items(), key=lambda p: sum(p[1].values()))
|
|
18
|
+print(int(most_sleep[0]) * most_sleep[1].most_common(1)[0][0])
|
|
19
|
+
|
|
20
|
+sleepiest_minute = max([(guard, times.most_common(1)[0]) for (guard, times) in sleeps.items()], key=lambda p: p[1][1])
|
|
21
|
+print(int(sleepiest_minute[0]) * sleepiest_minute[1][0])
|