Repository navigation
Expand file tree
/
Copy pathsolution.py
More file actions
138 lines (108 loc) · 3.7 KB
/
Copy pathsolution.py
File metadata and controls
138 lines (108 loc) · 3.7 KB
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
from collections import deque
from typing import Deque
class Library:
gID = 0
def __init__(self, bpd, signUpT):
self.bpd = bpd
self.signUpT = signUpT
self.bookArr = deque()
self.ID = Library.gID
Library.gID += 1
def addToBookArr(self, bookID, books):
# add such that sorted in descending order
if len(self.bookArr) == 0:
self.bookArr.append(bookID)
return
for i in range(len(self.bookArr)):
if books[bookID] >= books[self.bookArr[i]]:
self.bookArr.insert(i, bookID)
return
self.bookArr.append(bookID)
return
def refactorBookArr(self, unique):
# refactor bookArr based on which books have been processed.
toBeDeleted = []
for b in self.bookArr:
if unique[b] == 1:
toBeDeleted.append(b)
for b in toBeDeleted:
self.bookArr.remove(b)
return
def calculateScore(self, daysRemaining, books) -> int:
# calculate max potential of the lib
effectiveDays = daysRemaining - self.signUpT
totalBooks = self.bpd * effectiveDays
cumsum = 0
if totalBooks < len(self.bookArr):
for i in range(totalBooks):
cumsum += books[self.bookArr[i]]
else:
for b in self.bookArr:
cumsum += books[b]
return cumsum
class Main:
def getBestScore(self, libraries: Deque[Library], books, unique, daysRem):
bestScore = 0
bestLib = None
for lib in libraries:
lib.refactorBookArr(unique)
score = lib.calculateScore(daysRem, books)
if score > bestScore:
bestScore = score
bestLib = lib
if bestLib == None:
return 0, None
for b in bestLib.bookArr:
unique[b] = 1
return bestScore, bestLib
def printOut(self, libOut, file):
with open("./output/" + file + "_out.txt", "w") as f:
f.write(str(len(libOut)) + "\n")
lines = []
for l in libOut:
lines.append(str(l.ID) + " " + str(len(l.bookArr)) + "\n")
lines.append(" ".join([str(x) for x in l.bookArr]) + "\n")
f.writelines(lines)
return
def main(self, file):
libraries = deque()
totalScore = 0
libOut = []
with open("./input/" + file + "_in.txt", "r") as f:
b, l, D = [int(x) for x in f.readline().split()]
books = [int(x) for x in f.readline().split()]
unique = [0] * b
for i in range(l):
lb, sd, bpd = [int(x) for x in f.readline().split()]
lbArr = [int(x) for x in f.readline().split()]
lib = Library(bpd, sd)
for book in lbArr:
lib.addToBookArr(book, books)
libraries.append(lib)
# exit(0)
while D > 0 and len(libraries) > 0:
bestScore, library = self.getBestScore(libraries, books, unique, D)
if bestScore == 0:
break
libOut.append(library)
libraries.remove(library)
totalScore += bestScore
D -= library.signUpT
print("File " + file + " total score = " + str(totalScore))
self.printOut(libOut, file)
return totalScore
if __name__ == '__main__':
m = Main()
t = m.main("a")
Library.gID = 0
t += m.main("b")
Library.gID = 0
t += m.main("c")
Library.gID = 0
t += m.main("d")
Library.gID = 0
t += m.main("e")
Library.gID = 0
t += m.main("f")
print("Final score = " + str(t))
exit(0)