Skip to content

Commit 3b7d054

Browse files
committed
Day 4 - solved part 1
1 parent 864be35 commit 3b7d054

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

04/solution.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import datetime
2+
from dateutil import rrule
3+
from collections import Counter
4+
25
data = [x[1:].split("] ") for x in open("data.txt").read().split("\n")]
36

47
def 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

1013
data = [{"timestamp": epoch(x[0]), "cmd": x[1]} for x in data]
1114
data = sorted(data, key = lambda k: k["timestamp"])
@@ -14,9 +17,26 @@ def epoch(x):
1417
guards = {}
1518
pointer = 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

Comments
 (0)