11import datetime
2+ from dateutil import rrule
3+ from collections import Counter
4+
25data = [x [1 :].split ("] " ) for x in open ("data.txt" ).read ().split ("\n " )]
36
47def epoch (x ):
58 x = x .split (" " )
69 d = list (map (int ,x [0 ].split ("-" )))
710 t = list (map (int ,x [1 ].split (":" )))
8- return datetime .datetime (d [0 ], d [1 ], d [2 ], t [0 ], t [1 ]).timestamp ()
11+ return datetime .datetime (d [0 ], d [1 ], d [2 ], t [0 ], t [1 ])# .timestamp()
912
1013data = [{"timestamp" : epoch (x [0 ]), "cmd" : x [1 ]} for x in data ]
1114data = sorted (data , key = lambda k : k ["timestamp" ])
@@ -14,9 +17,26 @@ def epoch(x):
1417guards = {}
1518pointer = None
1619
17- print (data [:20 ])
20+ for row in data :
21+ if (row ["cmd" ][:5 ] == "Guard" ):
22+ pointer = int (row ["cmd" ].split (" " )[1 ][1 :])
23+ elif (row ["cmd" ] == "falls asleep" ):
24+ guards [pointer ] = guards .get (pointer , []) + [[row ["timestamp" ]]]
25+ elif (row ["cmd" ] == "wakes up" ):
26+ guards [pointer ][- 1 ].append (row ["timestamp" ])
27+
28+ naps = {}
29+
30+ for g ,n in guards .items ():
31+ naps [g ] = sum ([(x [- 1 ]- x [0 ]).seconds // 60 for x in n ])
32+
33+ sleepyhead = max (naps , key = naps .get )
34+
35+ minutes = [[z .minute for z in rrule .rrule (rrule .MINUTELY , dtstart = x [0 ], until = x [- 1 ])] for x in guards [sleepyhead ]]
36+
37+ all_minutes = []
38+
39+ for x in minutes :
40+ all_minutes += x
1841
19- #for row in data:
20- # if(row["cmd"][:5] == "Guard"):
21- # pointer = row["cmd"].split(" ")[1][1:]
22- # elif(row["cmd"] == "wakesup")
42+ print (Counter (all_minutes ).most_common (1 )[0 ][0 ] * sleepyhead )
0 commit comments