-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.py
58 lines (50 loc) · 2.03 KB
/
day04.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import time
with open('./inputs/input4.txt') as f:
start_time = time.time()
lines = f.readlines()
# Sort the array by date
lines.sort()
Guards = dict()
for line in lines:
action = line.split(" ")[3].strip()
#If it has a hashtag its a guard shift , so it gets the new guard
if(action[0]=="#"):
currentguard= action.replace("#","")
#If the word is asleep , the guard just fell asleep, so i store the time
if(action=="asleep"):
timeasleep= int(line.split(" ")[1].split(":")[1].replace("]",""))
#If the word is up, the guard has woken up, so I increment the times
if(action=="up"):
timewakeup = int(line.split(" ")[1].split(":")[1].replace("]",""))
for t in range(timeasleep,timewakeup):
if currentguard in Guards:
if t in Guards[currentguard]:
Guards[currentguard][t]+=1
else:
Guards[currentguard][t]=1
else:
Hours= dict()
Guards[currentguard]= Hours
mostMinutes= 0
partOneChecksum=0
#( which guard , which minute, numberoftimesslept)
mostminutesleptbyguard=(0,0,0)
#For all guards
for currentguard in Guards:
bestMinuteCycle = 0
totalMinutesCycle = 0
mostMinuteAmountCycle =0
#For all minutes they slept
for currentminute in Guards[currentguard]:
if Guards[currentguard][currentminute] > mostminutesleptbyguard[2]:
mostminutesleptbyguard = (currentguard,currentminute,Guards[currentguard][currentminute])
totalMinutesCycle+=Guards[currentguard][currentminute]
if Guards[currentguard][currentminute] > mostMinuteAmountCycle:
mostMinuteAmountCycle = Guards[currentguard][currentminute]
bestMinuteCycle = currentminute
if totalMinutesCycle >mostMinutes:
mostMinutes=totalMinutesCycle
partOneChecksum = int(bestMinuteCycle)*int(currentguard)
print "Part one: "+ str(partOneChecksum)
print "Part two: "+ str(int(mostminutesleptbyguard[0])*int(mostminutesleptbyguard[1]))
print "Time: "+ str(time.time()-start_time)