File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed 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
+ for i in range (start , minute ):
29
+ try :
30
+ guards [guard ][i ] += 1
31
+ except KeyError :
32
+ try :
33
+ guards [guard ].update ({i : 1 })
34
+ except KeyError :
35
+ guards [guard ] = {i : 1 }
36
+ change = False
37
+
38
+ h = []
39
+
40
+ for guard in guards :
41
+ sleep = guards [guard ]
42
+ hour = list (zip (* sorted (zip (sleep .values (), sleep .keys ()))))[1 ][- 1 ]
43
+
44
+ h .append ((sleep [hour ], (hour , int (guard ))))
45
+
46
+ guard ,minute = list (zip (* sorted (h )))[- 1 ][- 1 ]
47
+ print (guard * minute )
You can’t perform that action at this time.
0 commit comments