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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
class elf:
#HasWork = False;
#WorkTime = 0;
#WorkStep = '';
def WorkAssign(self,s):
AocOffset = 60; #correct value
#AocOffset = 0; #example only
self.WorkStep = s;
self.WorkTime = ord(s) -64 + AocOffset;
self.HasWork = True;
def WorkReset(self):
self.HasWork = False;
#self.WorkTime = 0;
self.WorkStep = '';
def DoWork(self):
self.WorkTime += -1;
#if
# return
def __init__(self):#, name, unlocks):
self.HasWork = False;
self.WorkTime = 1;
self.WorkStep = '';
def __str__(self):
return f"{self.WorkStep}\t{self.WorkTime}\t{self.HasWork}";
def WorkInProgress(elflist):
w = False;
for e in elflist:
w += e.HasWork;
return w;
def TransInstr (instruction):
return [instruction[5], instruction[36]];
myinput = {};
currentfilename = "input.txt";
numberofworkers = 5;
#currentfilename = "testinput.txt"; #example only
#numberofworkers = 2;#example only
f = open(currentfilename, 'r');
#list1 = [];
#list2 = {};
list2 = set();
for line in f:
nowinstr = TransInstr(line);
#myinput[nowinstr[1]].append(nowinstr[0]);
list2.add(nowinstr[0]);
#'''
try:
myinput[nowinstr[1]].append(nowinstr[0]);
#myinput[nowinstr[1]] = nowinstr[0];
except:
myinput[nowinstr[1]] = [];
myinput[nowinstr[1]].append(nowinstr[0]);
#'''
print(myinput.keys());
print("##############");
print(list2);
print("##############");
pr1 = list(myinput.keys());
pr1.sort();
pr2 = list(list2);
pr2.sort();
print(pr1);
print(pr2);
print("##############");
print("##############");
print("finding the first step");
CurrentStep = '';
workers = [];
for i in range(numberofworkers):
workers.append(elf());
AvailableSteps = [];
#AvailableSteps.append(CurrentStep);
for l in list2:
IsUnique = True;
for k in myinput.keys():
#print("L ", l, "\tK ",k, "\t",l == k);
if (l == k):
IsUnique = False;
break;
if IsUnique:
#CurrentStep = l;
#break;
AvailableSteps.append(l);
print("Unique values are ", AvailableSteps);
print("##############");
print(myinput);
print("##############");
part1 = ""; #CurrentStep;
#list1 = myinput.keys();
#list1.append(CurrentStep);
#print(list1);
part2 = -1;
IsWork = False;
print("whileloop begins");
while (len(AvailableSteps) != 0 or IsWork):
part2 += 1;#input();
#IsWork = False;
AvailableSteps.sort();
#CurrentStep = AvailableSteps[0];
print(part2, " - steps sorted:",AvailableSteps);
for worker in workers:
#print(worker);
#print(worker.WorkTime);
worker.DoWork();
if (worker.WorkTime == 0): # and worker.WorkStep != ''):
part1 += worker.WorkStep;
#if (worker.WorkSte
if (worker.WorkStep != ''):
for step in myinput:
try:
myinput[step].pop(myinput[step].index(worker.WorkStep));
except:
pass;
CurrentStepsCheck = "";# workers[0].WorkStep + workers[1].WorkStep + workers[2].WorkStep + workers[3].WorkStep;
for w in workers:
CurrentStepsCheck += w.WorkStep;
if (len(myinput[step]) == 0 and part1.find(step) == -1 and CurrentStepsCheck.find(step) == -1 ): #and AvailableSteps.find(step) == -1):
AvailableSteps.append(step);
worker.WorkReset();
#worker.WorkStep() = '';
#worker.HasWork() = False;
AvailableSteps = list(dict.fromkeys(AvailableSteps));
for worker in workers:
if not worker.HasWork:
if (len(AvailableSteps) > 0):
worker.WorkAssign(AvailableSteps[0]);
print("\tWorker begins step", AvailableSteps[0], "at second ", part2);
AvailableSteps.pop(0);
#else:
#worker.DoWork();
#worker.WorkTime() += -1;
#part1 += CurrentStep;
#print("now ", CurrentStep);
'''
for step in myinput:
try:
myinput[step].pop(myinput[step].index(CurrentStep));
except:
pass;
if (len(myinput[step]) == 0 and part1.find(step) == -1): #and AvailableSteps.find(step) == -1):
AvailableSteps.append(step);
'''
IsWork = WorkInProgress(workers);
#AvailableSteps.pop(0);
print("whileloop finished");
print("##################################");
def RemoveDupsOrder(TheString):
a = "";
for letter in TheString:
if not (letter in a):
a += letter;
return a;
print(part1);
part1 = RemoveDupsOrder(part1);
print(part1);
print("total time part2 = ", part2);
#207 too low (because I forgot to add 60secs per step, fixed now
#987 correct
|