File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ ** /input
Original file line number Diff line number Diff line change
1
+ with open ('input' , 'r' ) as raw :
2
+ i = sorted (raw .read ().strip ().split ('\n ' ))
3
+
4
+ guard = None
5
+ sleeping = False
6
+ start = None
7
+ change = False
8
+
9
+ guards = {}
10
+
11
+ for line in i :
12
+ minute = int (line [15 :17 ])
13
+
14
+ if line [19 ] == 'f' :
15
+ sleeping = True
16
+ start = minute
17
+ elif line [19 ] == 'w' :
18
+ sleeping = False
19
+ change = True
20
+ else :
21
+ if sleeping :
22
+ sleeping = False
23
+ change = True
24
+
25
+ guard = line [26 :].split (' ' )[0 ]
26
+
27
+ if change :
28
+ sleeptime = minute - start
29
+ try :
30
+ guards [guard ][0 ] += sleeptime
31
+ except KeyError :
32
+ guards [guard ] = [sleeptime , {}]
33
+
34
+ for i in range (start , minute ):
35
+ try :
36
+ guards [guard ][1 ][i ] += 1
37
+ except KeyError :
38
+ guards [guard ][1 ][i ] = 1
39
+ change = False
40
+
41
+ guard = sorted (((guards [x ][0 ],x ) for x in guards ))[- 1 ][1 ]
42
+ sleep = guards [guard ][1 ]
43
+ hour = list (zip (* sorted (zip (sleep .values (), sleep .keys ()))))[1 ][- 1 ]
44
+
45
+ print (int (guard )* hour )
You can’t perform that action at this time.
0 commit comments