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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
import numpy as np
def datecompact(log):
d = log[1:5] + log[6:8] + log[9:11] + log[12:14] + log[15:17];
return int(d);
def action(log):
if (log[19] == "f"):
return 1;
elif (log[19] == "w"):
return 2;
else:
#return 3;
return int(log[24:].strip("\n # begins shift"));
return (log[24:].strip("\n # begins shift"));
#return int(log[24:].strip("#beginshft\n"));
f = open("input");
#f = open("testinput");
myinput = [];
guards = {};
for i in f:
#print(datecompact(i));
myinput.append([datecompact(i),action(i)]);
if (action(i) != 1 and action(i) != 2 ):
try:
guards[action(i)] = 0;
except:
True;
for i in range(len(myinput)):
#print(i[0], " -- ", i[1]);
for j in range(len(myinput)):
if (myinput[i][0] < myinput[j][0]):
temp = myinput[i];
myinput[i] = myinput[j];
myinput[j] = temp;
#print(guards);
#for i in range(len(myinput) -1):
#if (myinput[i][0] > myinput[i+1][0]):
#print("fuck");
#for i in myinput:
# print(i[0], " -- ", i[1]);
x1 = 0;
for l in myinput:
if l[1] == 2:
guards[g] += l[0] - x1 ;
elif l[1] == 1:
x1 = l[0];
else:
g = l[1];
tmax = 0;
bestg = "";
for g in guards.keys():
print(g);
#'''
if (guards[g] >tmax):
tmax = guards[g];
bestg = g;
#'''
print("best guard ",bestg," - ", tmax);
#part1 = int(bestg)*tmax;
#print("part1", part1);
#269357 too high
#256332 too high
is_bestg = False;
#timestat = np.array(60);
#timestat.fill(0);
timestat = np.full(60,0, dtype=int);
#bestg = int(bestg);
for l in myinput:
print("iterating", l[0], " ", l[1], " ", l[0]%100);
if (is_bestg and (l[1] == 1 or l[1] == 2)):
print("yup");
if l[1] == 1:
t1 = l[0];
elif l[1] == 2:
t2 = l[0];
for t in range(t1%100,t2%100,1):
timestat[t] += 1;
elif l[1] == bestg:
is_bestg = True;
elif l[1] != bestg:
print("not bestg");
is_bestg = False;
'''
guards[g] += l[0] - x1 ;
elif l[1] == 1:
x1 = l[0];
else:
g = l[1];
'''
worsttime = (np.argmax(timestat));
part1 = int(bestg)*worsttime;
print("best guard ",bestg," - ", worsttime);
print("part1", part1);
|