-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
115 lines (104 loc) · 2.69 KB
/
main.go
File metadata and controls
115 lines (104 loc) · 2.69 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
package main
type Dataset struct {
books int
days int
numLibraries int
libraries []Library
}
type Library struct {
libraryID int
numBooks int
signupTime int
booksPerDay int
books []Book
sumScore int
sortedBooks []Book
timeSignedUp int
}
type Book struct {
bookID int
score int
}
type libraryScan struct {
libraryID int
numBooks int
books []int
}
type Output struct {
numLibraries int
libraryScans []libraryScan
}
func main() {
data := parsetext(gettext("a_example.txt"))
writeOutput(formOutput(data), "A")
data = parsetext(gettext("b_read_on.txt"))
writeOutput(formOutput(data), "B")
data = parsetext(gettext("c_incunabula.txt"))
writeOutput(formOutput(data), "C")
data = parsetext(gettext("d_tough_choices.txt"))
writeOutput(formOutput(data), "D")
data = parsetext(gettext("e_so_many_books.txt"))
writeOutput(formOutput(data), "E")
data = parsetext(gettext("f_libraries_of_the_world.txt"))
writeOutput(formOutput(data), "F")
}
func formOutput(data Dataset) Output {
//data := readIn()
ls := data.libraries
//scannedBooks := make([]Book, 0)
//signupOrder := make([]Library, len(ls))
channels := make([]chan []Book, len(ls))
for i, l := range ls {
c := make(chan []Book)
channels[i] = c
go radixSortBooks(l.books, 14, c)
}
for i := 0; i < len(ls); i++ {
booksSorted := <-channels[i]
ls[i].sortedBooks = append(ls[i].sortedBooks, booksSorted...)
//fmt.Println(ls[i])
}
for _, l := range ls {
//fmt.Println(l)
constraint := 50
if l.numBooks < constraint {
constraint = l.numBooks
for i := 0; i < constraint; i++ {
l.sumScore += l.sortedBooks[i].score
}
} else if l.numBooks == 0 {
l.sumScore = 0
} else {
for i := 0; i < constraint; i++ {
l.sumScore += l.sortedBooks[i].score
}
}
}
sortedLibraries := radixSortLibrariesNoGo(ls, 14)
currentday := 0
out := Output{numLibraries: 0, libraryScans: make([]libraryScan, 0)}
for _, l := range sortedLibraries {
currentday += l.signupTime
if currentday > data.days {
break
}
out.numLibraries++
l.timeSignedUp = currentday
perday := l.booksPerDay
numBooksToAdd := (data.days - currentday) * perday
var booksToAdd []Book
if numBooksToAdd > len(l.sortedBooks) {
booksToAdd = append(booksToAdd, l.sortedBooks[:len(l.sortedBooks)]...)
} else {
booksToAdd = append(booksToAdd, l.sortedBooks[:numBooksToAdd]...)
}
l.numBooks = len(booksToAdd)
bookstoaddids := make([]int, 0)
for _, b := range booksToAdd {
bookstoaddids = append(bookstoaddids, b.bookID)
}
scanoutput := libraryScan{libraryID: l.libraryID, books: bookstoaddids, numBooks: len(booksToAdd)}
out.libraryScans = append(out.libraryScans, scanoutput)
}
return out
}