-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (147 loc) · 4.39 KB
/
main.go
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
package main
import (
"fmt"
"io/ioutil"
"sort"
"strings"
)
// Library .
type Library struct {
id string
nBooks int
signup int
bookShippable int
books []*Book
firstDayAvailable int
sendAttempts int
sentBookIDs []string
sentBooks []*Book
libraryScore int
}
// Book .
type Book struct {
id int
score int
}
func main() {
files := []string{
// "a", // base
// "b", // 100k books | 100 libraries | 1000 days
//"c", // 100k books | 10k libraries | 100k days
"d", // 78600 books | 30k libraries | 30001 days
// "e", // 100k books | 1k libraries | 200 days
// "f", // 100k books | 1k libraries | 700 days
}
for _, fileName := range files {
fmt.Printf("****************** INPUT: %s\n", fileName)
inputSet := readFile(fmt.Sprintf("./inputFiles/%s.in", fileName))
configLines := strings.Split(inputSet, "\n")
nBooks, nLibraries, nDays := getConfig(configLines[0])
books := buildBooks(configLines[1], nBooks)
libraries := buildLibraries(configLines[2:], nLibraries, books)
sortedLibraries := sortLibraries(libraries)
outLibraries := algorithm(nDays, sortedLibraries, books)
scannedLibraries := findLibrariesScanned(outLibraries)
fmt.Printf("Scanned libraries: %d - Total libraries: %d\n", len(scannedLibraries), len(libraries))
result := fmt.Sprintf("%d\n", len(scannedLibraries))
mean := 0.0
for _, lib := range scannedLibraries {
// fmt.Printf("Sent books for library %s: %d\n", lib.id, len(lib.sentBooks))
total := lib.nBooks
sent := len(lib.sentBooks)
percent := sent / total
mean = (mean + float64(percent)) / float64(len(scannedLibraries))
result += fmt.Sprintf("%s %d\n", lib.id, len((lib.sentBooks)))
result += fmt.Sprintf("%s\n", strings.Join(lib.sentBookIDs, " "))
}
fmt.Printf("Sent book mean: %.000f\n", mean)
result = strings.TrimSpace(result)
ioutil.WriteFile(fmt.Sprintf("./result/%s.out", fileName), []byte(result), 0644)
}
}
func sortLibraries(libraries []*Library) []*Library {
// Sorting:
// - n giorni signup
// - n libri unici in libreria,
// - libri inviabili al giorno
// - score dei libri
for _, lib := range libraries {
bookShippable := lib.bookShippable
nbooks := lib.nBooks
libraryBooksScore := calcLibBookScore(lib.books) / nbooks
signupDays := lib.signup
bookShippableCoef := 100
libraryBooksScoreCoef := 100
signupDaysCoef := 1
lib.libraryScore = ((bookShippable * bookShippableCoef) *
(nbooks * 1) *
(libraryBooksScore * libraryBooksScoreCoef)) *
(signupDays * signupDaysCoef)
}
sort.Slice(libraries, func(i, j int) bool {
libA := libraries[i]
libB := libraries[j]
return libA.libraryScore > libB.libraryScore
})
return libraries
}
func updateLibraryScores(libraries []*Library, sentbooks map[int]bool) []*Library {
uniqueBooksAvailableCoef := 10
for _, lib := range libraries {
uniqueBooksAvailable := 0
for _, book := range lib.books {
if sent, ok := sentbooks[book.id]; !sent || !ok {
uniqueBooksAvailable++
}
}
lib.libraryScore += uniqueBooksAvailable * uniqueBooksAvailableCoef
}
sort.Slice(libraries, func(i, j int) bool {
libA := libraries[i]
libB := libraries[j]
return libA.libraryScore < libB.libraryScore
})
return libraries
}
func algorithm(nDays int, libraries []*Library, books []*Book) []*Library {
sentbooks := make(map[int]bool)
startingDay := 0
for _, library := range libraries {
library.firstDayAvailable = startingDay + library.signup
startingDay += library.signup
}
for day := 0; day < nDays; day++ {
libraries = updateLibraryScores(libraries, sentbooks)
for _, library := range libraries {
if library.firstDayAvailable < day {
continue
}
if library.sendAttempts == library.nBooks {
continue
}
shippablePerLibrary := library.bookShippable
for _, book := range library.books {
if shippablePerLibrary < 0 {
break
}
library.sendAttempts++
if sent, ok := sentbooks[book.id]; !sent || !ok {
library.sentBooks = append(library.sentBooks, book)
library.sentBookIDs = append(library.sentBookIDs, fmt.Sprintf("%d", book.id))
sentbooks[book.id] = true
shippablePerLibrary--
}
}
}
}
return libraries
}
func findLibrariesScanned(libraries []*Library) []*Library {
newLibraries := make([]*Library, 0)
for _, lib := range libraries {
if len(lib.sentBooks) > 0 {
newLibraries = append(newLibraries, lib)
}
}
return newLibraries
}