-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoln1.py
168 lines (99 loc) · 3.67 KB
/
soln1.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
import operator
class soln:
def __init__(self):
self.numBooks = None
self.numLib = None
self.numDays = None
self.scores = []
self.libs = []
self.libSorted = dict()
self.libSorted2 = []
self.usedBooks = set()
self.librariesUsed = []
self.booksUsed = []
def importFile(self, filename):
f = open(filename, "r")
line = f.readline()
self.numBooks = line.split()[0]
self.numLib = line.split()[1]
self.numDays = line.split()[2]
line = f.readline()
for i in line.split():
self.scores.append(int(i))
counter = 0
books = 0
while True:
line = f.readline()
if line == "" or line is None:
break
try:
if counter % 2 == 0:
self.libs.append(Library(line.split()[0], line.split()[1], line.split()[2], books))
else:
self.libs[books].addBooks(line.split())
self.libs[books].calcScore(self.scores)
books += 1
counter += 1
except:
print("An exception occurred")
f.close()
#print(self.numBooks, self.numLib, self.numDays)
#print(self.scores)
#for i in self.libs:
# print(i.numBooks, i.signup, i.ship, i.whichBooks, i.totalScore, i.number)
def sortByScore(self):
for library in self.libs:
self.libSorted[library.number] = library.totalScore
self.libSorted2 = sorted(self.libSorted.items(), key=operator.itemgetter(1))
def countMin(self):
daysUsed = 0
daysLeft = int(self.numDays)
check = True
while True:
for index, i in enumerate(self.libSorted2):
daysUsed += self.incrementD(self.libs[index].signup, self.libs[index].whichBooks, self.libs[index].ship)
daysLeft -= daysUsed
if daysLeft <= 0:
check = False
break
self.librariesUsed.append(index)
if check == False:
break
f = open("cout.txt", "w")
f.write(str(len(self.librariesUsed)) + "\n")
for lib in self.librariesUsed:
f.write(str(lib) + " " +str(len(self.libs[lib].whichBooks)) + "\n")
f.write(str(self.libs[lib].whichBooks).strip("[],").replace(',', ' ') + "\n")
f.close()
def incrementD(self, signUp, libBooks, eachBooks):
daysUsed = 0
daysUsed += int(signUp)
bookCount = 0
for book in libBooks:
bookCount += int(eachBooks)
return (int(daysUsed) + int(len(libBooks) / bookCount))
class Library:
def __init__(self, books, signup, ship, number):
self.numBooks = books
self.signup = signup
self.ship = ship
self.whichBooks = []
self.number = number
self.totalScore = 0
def addBooks(self, stringbooks):
for i in stringbooks:
self.whichBooks.append(int(i))
def calcScore(self, scores):
for i in self.whichBooks:
self.totalScore += scores[i]
def main():
s1 = soln()
s1.importFile("c_incunabula.txt")
s1.sortByScore()
#for i in s1.libSorted:
#print(i, s1.libSorted[i])
#for i in s1.libSorted2:
# print(i)
s1.countMin()
if __name__ == "__main__":
main()