-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
67 lines (56 loc) · 1.45 KB
/
builder.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
package main
import (
"fmt"
"sort"
"strings"
)
func getConfig(line string) (books int, libraries int, days int) {
confParts := strings.Split(line, " ")
books = toint(confParts[0])
libraries = toint(confParts[1])
days = toint(confParts[2])
return
}
func buildBooks(scoreLine string, nBooks int) []*Book {
scores := strings.Split(scoreLine, " ")
books := make([]*Book, nBooks)
for i := 0; i < nBooks; i++ {
books[i] = &Book{
id: i,
score: toint(scores[i]),
}
}
return books
}
func buildLibraries(lines []string, nLibraries int, availableBooks []*Book) []*Library {
libraries := make([]*Library, nLibraries)
for i := 0; i < nLibraries*2; i += 2 {
line1 := lines[i]
line2 := lines[i+1]
library := buildLibrary(line1, line2, availableBooks)
library.id = fmt.Sprintf("%d", i/2)
libraries[i/2] = library
}
return libraries
}
func buildLibrary(line1, line2 string, availableBooks []*Book) *Library {
libraryConfig := strings.Split(line1, " ")
nBooks := toint(libraryConfig[0])
library := Library{
nBooks: nBooks,
signup: toint(libraryConfig[1]),
bookShippable: toint(libraryConfig[2]),
}
books := make([]*Book, nBooks)
for i, stringBookid := range strings.Split(line2, " ") {
intBookID := toint(stringBookid)
books[i] = availableBooks[intBookID]
}
sort.Slice(books, func(i, j int) bool {
bookA := books[i]
bookB := books[j]
return bookA.score > bookB.score
})
library.books = books
return &library
}