-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash_primary.py
211 lines (175 loc) · 6.57 KB
/
hash_primary.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from operator import itemgetter
# Please start with smaller files first because the more te pizzas the slower it will process
# Just change 'input 5' to whatever name of the other input files
file = open('input5', 'r', encoding='utf-8') # Open input file
linear_problem = [line.replace("\n", "").replace("\t", "").split(' ') for line in file] # Parse input file
# Remove potential empty strings
i = 0
while i < len(linear_problem):
j = 0
while True:
if j == len(linear_problem[i]):
break
elif linear_problem[i][j] == "":
linear_problem[i].remove("")
j -= 1
j += 1
i += 1
# Parse pizza count and party count for the 3 different party sizes
pizzas = int(linear_problem[0][0])
partySize = []
for i in range(1, 4):
partySize.append(int(linear_problem[0][i]))
# Create list with pizzas
pizzaList = list()
class pizza:
ID = int()
given = bool() # If certain pizza is given to at least one team this becomes True, else is False
ingredients = list()
# Parse pizzas from input array
for i in range(1, pizzas + 1):
p = pizza()
p.ID = i
p.given = False
p.ingredients = list()
for j in range(1, len(linear_problem[i])):
p.ingredients.append(linear_problem[i][j])
pizzaList.append(p)
# Combinations for the groups of 2
combinations = []
combinationsOf2 = []
for i in range(len(pizzaList)):
for j in range(i + 1, len(pizzaList)):
ings = list()
for a in pizzaList[i].ingredients:
if a not in ings:
ings.append(a)
for a in pizzaList[j].ingredients:
if a not in ings:
ings.append(a)
combinationsOf2.append([len(ings) ** 2, i, j])
combinationsOf2.sort(key=itemgetter(0))
combinations += combinationsOf2
del combinationsOf2 # Free up some RAM
# Combinations for the groups of 3
combinationsOf3 = []
for i in range(len(pizzaList)):
for j in range(i + 1, len(pizzaList)):
for k in range(j + 1, len(pizzaList)):
ings = list()
for a in pizzaList[i].ingredients:
if a not in ings:
ings.append(a)
for a in pizzaList[j].ingredients:
if a not in ings:
ings.append(a)
for a in pizzaList[k].ingredients:
if a not in ings:
ings.append(a)
combinationsOf3.append([len(ings) ** 2, i, j, k])
combinationsOf3.sort(key=itemgetter(0))
combinations += combinationsOf3
del combinationsOf3 # Free up some RAM
# Combinations for the groups of 4
combinationsOf4 = []
for i in range(len(pizzaList)):
for j in range(i + 1, len(pizzaList)):
for k in range(j + 1, len(pizzaList)):
for l in range(k + 1, len(pizzaList)):
ings = list()
for a in pizzaList[i].ingredients:
if a not in ings:
ings.append(a)
for a in pizzaList[j].ingredients:
if a not in ings:
ings.append(a)
for a in pizzaList[k].ingredients:
if a not in ings:
ings.append(a)
for a in pizzaList[l].ingredients:
if a not in ings:
ings.append(a)
combinationsOf4.append([len(ings) ** 2, i, j, k, l])
combinationsOf4.sort(key=itemgetter(0))
combinations += combinationsOf4
del combinationsOf4 # Free up some RAM
combinations.reverse()
# Select from highest to lowest score to give every pizza
# For every potential remaining pizzas there is a combination that covers them
# Attention for 5 remaining pizzas(can contain 2-3, 4, only 4 2 or 3)
bestCombs = list() # List to put best combinations
availCount = pizzas # It counts available pizzas
for comb in combinations:
# If the remaining available pizzas are 5, the algorithm must check whether or not there is a team of 3 and a team of 2
if availCount == 5:
# Seek valid comb of 3 pizzas
for combof3 in combinations:
validCombof3 = True
if len(combof3) == 4:
for p in combof3[1:]:
if pizzaList[p].given or not partySize[1] > 0:
validCombof3 = False
break
else:
validCombof3 = False
# if still valid combo of 3
if validCombof3:
# Pretend to give the combo of 3 to correctly check for valid combo of 2
for i in combof3[1:]:
pizzaList[i].given = True
# Seek valid comb of 2 pizzas
for combof2 in combinations:
validCombof2 = True
if len(combof2) == 3:
for p in combof2[1:]:
if pizzaList[p].given or not partySize[0] > 0:
validCombof2 = False
break
else:
validCombof2 = False
if validCombof2:
# Give both comb of 3 and of 2 and add them to list
for i in combof2[1:]:
pizzaList[i].given = True
bestCombs.append(combof3)
bestCombs.append(combof2)
partySize[0] -= 1
partySize[1] -= 1
availCount -= 5
break
# if there is no valid
if availCount != 0:
for i in combof3[1:]:
pizzaList[i].given = False
pizCount = len(comb[1:])
validComb = True
# Check wehter on not the combination is valid
for p in comb[1:]:
if pizzaList[p].given or not partySize[pizCount - 2] > 0:
validComb = False
break
# If it is valid give the pizzas
if validComb:
partySize[pizCount - 2] -= 1
bestCombs.append(comb)
availCount -= pizCount
for i in comb[1:]:
pizzaList[i].given = True
Teams = len(bestCombs)
bestCombs.reverse()
print('-----------Combinations-----------')
print(bestCombs)
print('----------Remaining Teams-----------')
print(partySize)
print('-------------Points-------------')
points = 0
for c in bestCombs:
points += c[0]
print(points)
# Print the results into a file
f = open("output", "w")
f.write(str(Teams) + "\n")
for c in bestCombs:
f.write(str(len(c[1:])) + " " + str(c[1:]).replace("[", "").replace("]", "").replace(",", "") + "\n")
f.close()
file.close()